/**
 * ae_events_stores__leads_defaults.ts
 *
 * Default state for the leads (exhibitor lead retrieval) section of ae_events_stores.ts.
 *   leads_loc_defaults  → events_loc.leads  (persisted to localStorage)
 *   leads_sess_defaults → events_sess.leads (in-memory, resets on page load)
 */

export interface LeadsLocState {
    __version: number;
    show_details: boolean; // Show extra metadata/details in the leads tab (was events_loc.show_details)
    show_option__paid_tab: boolean;
    show_content__scan_alert: boolean;
    show_content__scan_requirements: boolean;
    show_content__custom_question_descriptions: boolean;
    show_content__email_link_warning: boolean;
    default_to_scan: boolean;
    default__external_registration_id: string;
    auto_view: boolean;
    auto_hide_on_sign_in: boolean;
    show_hidden: boolean;
    show_not_enabled: boolean;
    refresh_interval__tracking_li: number;
    refresh_interval_sec: number; // Auto-refresh interval for the lead list (seconds)
    search_version: number;
    qry__remote_first: boolean;
    qry__search_text: string;
    qry__sort_order: string;
    tracking__search_version: number;
    tracking__qry__remote_first: boolean;
    tracking__qry__search_text: string;
    tracking__qry__sort_order: string;
    tracking__qry__licensee_email: string;
    entered_passcode: string | null;
    // Value shape: key = passcode/email used; type = 'shared' | 'licensed'; entries expire after a configurable max age.
    auth_exhibit_kv: Record<
        string,
        { key: string; type: string; updated_on: string }
    >;
    edit_license_li: boolean;
    // Key = exhibit ID (random), value = last-used tab name.
    tab: Record<string, string>;
    // Per-exhibit Add tab input mode: 'qr' | 'search'. Persisted so operator preference survives navigation.
    tab_add_mode: Record<string, string>;
    // Per-exhibit scan qualify mode:
    //   'rapid'   — confirm tap → auto-reset → scan next
    //   'qualify' — confirm tap → navigate to lead detail
    //   'auto'    — no confirm, auto-add immediately → auto-reset
    //   'multi'   — BarcodeDetector batch scan, grid of confirm cards
    tab_scan_qualify: Record<string, 'rapid' | 'qualify' | 'auto' | 'multi'>;
}

export interface TmpLicense {
    index: number | null;
    email: string;
    full_name: string;
    passcode: string;
    session_count: number;
    updated_on: string;
}

export interface LeadsSessState {
    show_form__license: boolean;
    show_form__search: boolean;
    show_form__scan: boolean;
    // false when no lead is active; string (event_exhibit_tracking_id) when one is open.
    show_form__view_lead: boolean | string;
    show_confirm__add_lead: string[];
    submit_status__license: string | null; // 'saving' | 'created' | 'updated'
    submit_status__search: string | null; // 'searching' | 'complete'
    last_refresh_time: string | null;
    next_refresh_countdown: number;
    entered_passcode: string | null;
    tmp_license: TmpLicense;
    entered_search_str: string | null;
    lead_data_changed: any;
    qr_scan_start: boolean;
    qr_scan_result: string | null;
}

// Persisted leads config — survives browser sessions.
export const leads_loc_defaults: LeadsLocState = {
    __version: 1,
    show_details: false, // Show extra metadata/details in the leads tab (was events_loc.show_details)
    show_option__paid_tab: true,
    show_content__scan_alert: true, // Workaround for QR scanner edge-case bug.
    show_content__scan_requirements: true,
    show_content__custom_question_descriptions: true,
    show_content__email_link_warning: true,

    default_to_scan: true,
    // Fallback external registration ID used when building lead export data.
    // Should be overridden by the active event's external_event_id in cfg_json.
    default__external_registration_id: '2024_Annual Meeting',

    auto_view: true, // Show the new lead immediately after adding by scan or search.
    auto_hide_on_sign_in: true,

    show_hidden: false, // Hidden (archived) leads are excluded from the main list.
    show_not_enabled: false,

    refresh_interval__tracking_li: 30000, // 30 seconds.
    refresh_interval_sec: 25, // Auto-refresh interval for the lead list (seconds, default 25).

    // Standardized Search Pattern 2026-01-28
    search_version: 0,
    qry__remote_first: false,
    qry__search_text: '',
    qry__sort_order: 'name_asc',

    // Standardized Search Pattern (Tracking) 2026-01-28
    tracking__search_version: 0,
    tracking__qry__remote_first: false,
    tracking__qry__search_text: '',
    tracking__qry__sort_order: 'created_desc',
    tracking__qry__licensee_email: 'all',

    // Exhibitor's shared passcode for staff booth access. Used for initial lead retrieval auth.
    entered_passcode: null,

    // auth_exhibit_kv: key-value of exhibit IDs this browser client is authorized for,
    // mapped to { key, updated_on }. Entries expire after a configurable max age.
    auth_exhibit_kv: {},

    edit_license_li: false,

    // Per-exhibit current tab. Key = exhibit ID (random), value = tab name.
    // Intentionally persisted so each exhibit's last-used tab is remembered across sessions.
    // Example: {'LNDF-67-89-92': 'start', 'OFLN-32-38-14': 'add_scan'}
    tab: {},
    tab_add_mode: {},
    tab_scan_qualify: {}
};

// In-memory leads state — resets on page load.
export const leads_sess_defaults: LeadsSessState = {
    show_form__license: false,
    show_form__search: false,
    show_form__scan: false,
    show_form__view_lead: false, // Set to event_exhibit_tracking_id when a lead is active.

    show_confirm__add_lead: [],

    submit_status__license: null, // 'saving' | 'created' | 'updated'
    submit_status__search: null, // 'searching' | 'complete'

    last_refresh_time: null as string | null,
    next_refresh_countdown: 0,

    entered_passcode: null,

    tmp_license: {
        index: null,
        email: '',
        full_name: '',
        passcode: '',
        session_count: 0,
        updated_on: new Date().toISOString()
    },

    entered_search_str: null,

    lead_data_changed: null,

    qr_scan_start: true,
    qr_scan_result: null
};
