/**
 * ae_events_stores__badges_defaults.ts
 *
 * Type definitions and defaults for the Badges stores.
 *
 *   BadgesRemoteCfg   → shape of event.mod_badges_json (server-side admin config)
 *   BadgesLocState    → badges_loc (PersistedState, localStorage key 'ae_badges_loc')
 *   BadgesSessState   → events_sess.badges (in-memory, resets on page load)
 */

// ---------------------------------------------------------------------------
// Remote config — shape of event.mod_badges_json
// This is the admin-controlled, server-side config for an event.
// ---------------------------------------------------------------------------
export interface BadgesRemoteCfg {
    // Search & UI behaviour
    enable_mass_print: boolean;          // show the mass-print controls
    enable_add_badge_btn: boolean;       // show the "Add Badge" button
    enable_upload_badge_li_btn: boolean; // show the "Upload Badge List" button
    enable_search_qr: boolean;           // enable QR scan search

    // Per-access-tier search limits.
    // anonymous  — not signed in at all
    // auth       — public passcode or identity-verified (Authenticated/Public)
    // trusted    — onsite staff (Trusted) and above; above-trusted inherits trusted limits
    anon_search_result_limit: number;
    anon_search_min_chars: number;
    auth_search_result_limit: number;
    auth_search_min_chars: number;
    trusted_search_result_limit: number;
    trusted_search_min_chars: number;

    // QR code configuration
    qr_type: string | null; // QR payload format (e.g. 'badge_id', 'url')

    // Access control — passcodes for attendee / staff tiered access.
    // Only expose to administrator_access. Never render for lower access levels.
    trusted_passcode: string | null;
    administrator_passcode: string | null;

    // Field-level edit permissions per access tier.
    // can_edit is a string[] of field keys, or '*' for all fields.
    edit_permissions: {
        authenticated?: { can_edit: string[] | '*' };
        trusted?: { can_edit: string[] | '*' };
        administrator?: { can_edit: string[] | '*' };
    };
}

// Default field lists — used when edit_permissions is absent from mod_badges_json.
export const default_authenticated_can_edit: string[] = [
    'pronouns_override',
    'full_name_override',
    'professional_title_override',
    'affiliations_override',
    'phone_override',
    'location_override',
    'allow_tracking',
    'agree_to_tc'
];

export const default_trusted_can_edit: string[] = [
    'pronouns_override',
    'full_name_override',
    'professional_title_override',
    'affiliations_override',
    'email_override',
    'phone_override',
    'location_override',
    'badge_type_code_override',
    'registration_type_code_override',
    'other_1_code',
    'other_2_code',
    'other_3_code',
    'other_4_code',
    'other_5_code',
    'other_6_code',
    'other_7_code',
    'other_8_code',
    'ticket_1_code',
    'ticket_2_code',
    'ticket_3_code',
    'ticket_4_code',
    'ticket_5_code',
    'ticket_6_code',
    'ticket_7_code',
    'ticket_8_code',
    'allow_tracking',
    'agree_to_tc',
    'hide',
    'priority',
    'notes'
];

// ---------------------------------------------------------------------------
// Local state — persisted to localStorage via PersistedState ('ae_badges_loc')
// ---------------------------------------------------------------------------
export interface BadgesLocState {
    auto_view: boolean;
    // 'default' = hide hidden+disabled | 'show_hidden' = show hidden | 'show_all' = show hidden+disabled
    visibility_filter: 'default' | 'show_hidden' | 'show_all';
    show_printed: boolean;
    allow_reprint: boolean;
    show_element__cfg: boolean;
    show_element__cfg_detail: boolean;
    fulltext_search_qry_str: string | null;
    search_badge_type_code: string | null;
    search_version: number;
    qry__remote_first: boolean;
    qry_printed_status: string; // 'all' | 'printed' | 'not_printed'
    qry_affiliations: string | null;
    qry_sort_order: string;
    qry_result_limit: number; // UI override for max results (edit mode only; tier max enforced by stepper)
    status_qry__search: string | null;
    use_id_li: boolean;
    search_status: string | null;
    search_complete: boolean;
    classes__form: string;
    // Server-mirrored flags (copied from event.mod_badges_json)
    enable_mass_print: boolean;
    enable_add_badge_btn: boolean;
    enable_upload_badge_li_btn: boolean;
    enable_search_qr: boolean;
    qr_type: string | null;
    trusted_passcode: string | null;
    administrator_passcode: string | null;
    edit_permissions: {
        authenticated?: { can_edit: string[] | '*' };
        trusted?: { can_edit: string[] | '*' };
        administrator?: { can_edit: string[] | '*' };
    } | null;
    // Per-tier search constraints (mirrored from mod_badges_json)
    anon_search_result_limit: number;
    anon_search_min_chars: number;
    auth_search_result_limit: number;
    auth_search_min_chars: number;
    trusted_search_result_limit: number;
    trusted_search_min_chars: number;
    // Timestamp when the remote config was last mirrored locally
    remote_cfg_last_synced_on: string | null;
    // After-print navigation method. true (default) = SvelteKit goto() — faster, no full reload.
    // false = window.location.href — full page reload, use as a fallback if goto causes issues.
    // Per-device: stored in localStorage, not synced to the event config.
    print_nav_use_goto: boolean;
}

export interface BadgesSessState {
    fulltext_search_qry_str: string | null;
    search_badge_type_code: string | null;
    status_qry__search: string | null;
    use_id_li: boolean;
    search_status: string | null;
    search_complete: boolean;
    show_form__search: boolean;
    show_form__search_results: boolean;
    show_form__scan: boolean;
    qr_scan_start: boolean;
    qr_scan_result: string | null;
}

// Persisted badge UI state — survives browser sessions.
// Stored in localStorage as 'ae_badges_loc' via PersistedState (runed).
export const badges_loc_defaults: BadgesLocState = {
    auto_view: true,

    visibility_filter: 'default', // 'default' | 'show_hidden' | 'show_all'

    show_printed: false,
    allow_reprint: false,

    show_element__cfg: true,
    show_element__cfg_detail: false,

    fulltext_search_qry_str: null,
    search_badge_type_code: null,

    // Search stabilization — version bump clears cache and re-runs (Standardized Pattern 2026-01-27).
    search_version: 0,
    qry__remote_first: false,

    // Filter states
    qry_printed_status: 'all', // 'all' | 'printed' | 'not_printed'
    qry_affiliations: null, // null = no affiliation filter
    qry_sort_order: '', // '' = default sort order
    qry_result_limit: 25,

    status_qry__search: null,
    use_id_li: true,
    search_status: null,
    search_complete: false,

    classes__form: 'border border-surface-200 p-4 space-y-4 rounded-container'
    ,
    // Default values for server-mirrored flags
    enable_mass_print: true,
    enable_add_badge_btn: true,
    enable_upload_badge_li_btn: true,
    enable_search_qr: true,
    qr_type: null,
    trusted_passcode: null,
    administrator_passcode: null,
    edit_permissions: null,
    // Per-tier search constraints — conservative defaults; overridden per event via mod_badges_json.
    anon_search_result_limit: 15,
    anon_search_min_chars: 3,
    auth_search_result_limit: 25,
    auth_search_min_chars: 2,
    trusted_search_result_limit: 150,
    trusted_search_min_chars: 1,
    remote_cfg_last_synced_on: null,
    print_nav_use_goto: true
};

// In-memory badge state — resets on page load.
export const badges_sess_defaults: BadgesSessState = {
    fulltext_search_qry_str: null,
    search_badge_type_code: null,
    status_qry__search: null,
    use_id_li: true,
    search_status: null,
    search_complete: true,

    show_form__search: true,
    show_form__search_results: true,
    show_form__scan: false,

    qr_scan_start: true,
    qr_scan_result: null
};
