import { persisted } from 'svelte-persisted-store';
import { writable } from 'svelte/store';
import type { Writable } from 'svelte/store';

import type { key_val } from '$lib/stores/ae_stores';

// Set the version for the app data. Changing this should force a notification and ask the user to clear and reload the page.
const ver = '2025-10-16_2139';
const ver_idb = '2025-10-16_2139';

/* *** BEGIN *** Initialize events_local_data_struct */
// Longer-term app data. This should be stored to *local* storage.
// Updated 2024-03-06
const events_local_data_struct: key_val = {
    ver: ver,
    ver_idb: ver_idb,

    // Shared
    name: 'Aether - Events (SvelteKit 2.x Svelte 4.x)',
    title: `OSIT's Æ Events`, // - Dev SvelteKit`, // &AElig;

    ds: {},

    events_cfg_json: {},

    event_id: null,

    // all, disabled, enabled
    qry__enabled: 'enabled',
    // all, hidden, not_hidden
    qry__hidden: 'not_hidden',
    qry__limit: 20,
    qry__offset: 0,

    // The show details is intended for things like meta data and additional details that are not always needed.
    show_details: false,

    auth__person: {}, // allow, id, name, email, passcode, etc
    // The auth__entered_key (usually email or person_id) and auth__entered_passcode is found under events_sess.entered_key and events_sess.entered_passcode because it should be temporary.
    // auth__entered_passcode: null,

    // The auth__kv (key value pairs) is used to store the xyz IDs that the browser client can access. This is a key value list of xyz ID and created datetime stamp (or just true). These should not be more than X days old.
    auth__kv: {
        event: {
            // 'LNDF-67-89-92': true
        },
        exhibit: {
            // 'LNDF-67-89-92': true
        },
        location: {
            // 'LNDF-67-89-92': true
        },
        session: {
            // 'LNDF-67-89-92': true, false, 'read', 'write'
        },
        presentation: {
            // 'LNDF-67-89-92': true
        },
        presenter: {
            // 'LNDF-67-89-92': true
        },
        person: {
            // 'LNDF-67-89-92': true
        }
    },

    // auth__session_kv: {
    //     // {'LNDF-67-89-92': true}
    // },
    // auth__presentation_kv: {
    //     // {'LNDF-67-89-92': true}
    // },
    // auth__presenter_kv: {
    //     // {'LNDF-67-89-92': true}
    // },

    // Badge Printing
    badges: {
        auto_view: true,

        show_hidden: false, // These are hidden (archived) leads so the list is not as long.
        show_not_enabled: false,

        show_printed: false,
        allow_reprint: false,

        show_element__cfg: true,
        show_element__cfg_detail: false,
        // 'theme_mode': 'dark',
        // 'theme_name': 'wintry',

        fulltext_search_qry_str: null,
        search_badge_type_code: null,

        // New additions for search stabilization (Standardized Pattern 2026-01-27)
        search_version: 0,
        qry__remote_first: false,

        // New additions for filter states
        qry_printed_status: 'all', // Default to all
        qry_affiliations: null, // Default to null for no filter
        qry_sort_order: '', // Default to empty string for default sort

        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'
    },

    // Event Files - uploads for sessions, presenters, etc
    // 'files': {
    // },

    // Event Presentation Launcher (and native Electron app)
    launcher: {
        // default - browser, onsite - browser onsite, native - Electron app onsite
        app_mode: 'default', // 'default', 'native', 'onsite'
        ws_connect: false,

        qry_limit__files: 25,
        qry_limit__presentations: 25,
        qry_limit__presenters: 75,
        qry_limit__sessions: 50,

        hide__launcher_header: false,
        hide__launcher_menu: false,
        hide__launcher_footer: false,

        hide__modal_header_title: false,

        hide_drawer__debug: true,
        hide__ws_element: true,
        hide__ws_form: true,
        hide__ws_messages: true,
        hide__ws_commands: true,

        hide_content__draft_files: true,

        show_content__disabled_files: false,
        show_content__hidden_files: false,
        show_content__hidden_presentations: false,
        show_content__hidden_presenters: false,
        show_content__hidden_sessions: false,
        show_content__draft_files: false,

        // These should be renamed to match the pres_mgmt section. Use "hide" instead of "show".
        show_content__session_code: true,
        show_content__presentation_code: true,
        show_content__presenter_code: true,

        show_section__controller: false,

        // 3-Way Section States (V5 Overhaul)
        // Values: 'collapsed' | 'auto' | 'pinned'
        section_state__health: 'auto',
        section_state__native_os: 'collapsed',
        section_state__sync_timers: 'collapsed',
        section_state__updates: 'collapsed',
        section_state__controller: 'auto',
        section_state__screen_saver: 'collapsed',
        section_state__app_modes: 'collapsed',
        section_state__local_actions: 'collapsed',

        datetime_format: 'datetime_12_long',
        time_format: 'time_12_short',
        time_hours: 12, // 12 or 24

        sync_intervals: {
            event: 90000,
            device: 60000,
            location: 30000,
            session: 15000,
            presentation: 45000,
            presenter: 60000,
            file_sync: 10000
        },

        slct: {
            event_id: null,
            event_location_id: null,
            event_session_id: null,
            event_presentation_id: null,
            event_presenter_id: null,
            event_file_id: null // event_file_id
        },

        native: {
            // 'local_file_cache_path': aether_cfg_data.app.local_file_cache_path,
            // 'host_file_temp_path': aether_cfg_data.app.host_file_temp_path,
            host_file_config_path: 'device_configs/ae_native_app_config.default.json'
        },

        idle_timer: 7 * 60 * 1000, // How many seconds until idle
        idle_cycle: 5 * 1000, // How frequently the idle status is checked
        idle_loop_period: 3 * 60 * 1000, // How frequently the loop runs for the screen saver and similar

        screen_saver_img_kv: {}, // This key value list is generate when the launcher_file_cont is loaded. It only adds image file types.
        modal__title: '-- Not Set --',
        modal__open: null,
        modal__open_filename: null,
        modal_img_src: null,

        controller: 'local',
        controller_group_code: 'launcher-00',
        controller_client_id: null
        // controller_cmd: null,
        // controller_trigger_send: null,
    },

    // Lead Retrievals (Exhibit)
    leads: {
        show_option__paid_tab: true,
        show_content__scan_alert: true, // For QR scanner bug...
        show_content__scan_requirements: true,
        show_content__custom_question_descriptions: true,
        show_content__email_link_warning: true,

        default_to_scan: true,

        // For ISHLT 2024 Annual Meeting only!
        default__external_registration_id: '2024_Annual Meeting',

        auto_view: true, // Show the new lead after added by scan or search
        auto_hide_on_sign_in: true,

        show_hidden: false, // These are hidden (archived) leads so the list is not as long.
        show_not_enabled: false,

        refresh_interval__tracking_li: 30000, // 30 seconds

        // 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',

        // The entered_passcode is the exhibit booths shared passcode for staff. This is used to initially access the lead retrieval service.
        entered_passcode: null,

        // The auth_exhibit_kv (key value pairs) is used to store the exhibit IDs that the browser client can access. This is a key value list of exhibit ID and created datetime stamp. These should not be more than X days old. The entered_passcode for events_sess.leads is what they are entering to log in.
        auth_exhibit_kv: {
            // {'LNDF-67-89-92': {key: 'example@oneskyit.com', updated_on: '2024-03-13T08:05:29Z}}
        },

        // The auth_exhibit_license_li is used to store the exhibit license(s) being used on the browser client. There can be multiple exhibit IDs, but only one license per exhibit ID for the browser client. This is used to determine who can actually access and use the lead retrieval service. This is a key value list of key (email address) and created datetime stamp. These should not be more than X days old.
        // auth_exhibit_license_li: {
        //     // 'LNDF-67-89-92': { 'key': 'example@oneskyit.com', 'updated_on': '2024-03-13T08:05:29Z'}
        // },
        edit_license_li: false,

        // The "tab" is a key value list of exhibit ID and tab name. This is intentionally using local storage to store the current tab for each exhibit.
        // example: {'LNDF-67-89-92': 'start', 'OFLN-32-38-14': 'add_scan'}
        tab: {}
    },

    // Presentation Management
    pres_mgmt: {
        sync_local_config: false,
        lock_config: true,

        datetime_format: 'datetime_12_long',
        time_format: 'time_12_short',
        time_hours: 12, // 12 or 24

        qry_enabled: 'enabled', // all, disabled, enabled
        qry_hidden: 'not_hidden', // all, hidden, not_hidden
        qry_limit__files: 75,
        qry_limit__presentations: 25,
        qry_limit__presenters: 500,
        qry_limit__sessions: 100,
        qry_max: 500, // This is the max number the limit is allowed to be set to.
        qry__files_offset_seconds: null,
        qry__files_sort: 'created_on',

        // New additions for search stabilization (Standardized Pattern 2026-01-27)
        search_version: 0,
        qry__remote_first: false,

        qry_and__file_count: true, // Essentially it should be greater than 0

        save_search_text: true,
        saved_search__session: null,

        require__presenter_agree: false,
        require__session_agree: false,

        // show_content__agree_text: false,
        show_content__event_view: null,
        // show__launcher_link: false,
        // show__location_link: false,
        show_content__location_qr: false,
        show_content__presentation_description: false, // Note that this is for *all* presentations in the user interface. It is a global setting.
        show_content__presenter_page_help: true,
        // show_content__presenter_start: false,
        show_content__presenter_view: null,
        show_content__presenter_qr: false,
        show_content__session_description: false,
        show_content__session_files: false,
        show_content__session_help: true,
        show_content__session_presentations: false,
        show_content__session_search_view: null,
        show_content__session_search_help: true,
        show_content__session_search_room_name: false,
        show_content__session_view: null,
        show_content__session_qr: false,
        hide__session_msg: true,
        hide__session_poc: true,
        hide__session_poc_biography: true,
        hide__presenter_biography: true,

        // Only specific to lists and tables:
        hide__session_li_location_field: false,
        hide__session_li_poc_field: false,
        hide__launcher_link_legacy: true, // Flask version
        hide__launcher_link: true, // New Svelte version
        hide__location_link: true,

        show_content__disabled_files: false,
        show_content__hidden_files: false,
        show_content__hidden_presentations: false,
        show_content__hidden_presenters: false,
        show_content__hidden_sessions: false,

        show__direct_download: false,

        // No longer used. Use "hide" instead of "show". Now it is initially set in the remote event config sync.
        // show_content__presentation_code: true,
        // show_content__presenter_code: true,
        // show_content__session_code: true,

        show_menu__presenter: null,
        show_menu__session: null,
        show_menu__session_search: null,
        show_menu__event_reports: null,

        show_report: null,
        // show_report__presenters_agree: false,
        // show_report__recent_files: false,

        // time_format: 'time_12_short', // 'time_short', 'time_12_short'

        disable_submit__opt_out: true,
        submit_status__opt_out: null,

        device_kv: {
            // 'LNDF-67-89-92': {'collapse': true},
        },
        location_kv: {
            // 'LNDF-67-89-92': {'collapse': true},
        }
    }

    // Speakers Management (Collection)
    // 'speakers': {
    // },

    // other
};
// console.log(`AE Stores - App Events Local Storage Data:`, events_local_data_struct);

// This works, but does not uses local storage:
// export let ae_loc = writable(events_local_data_struct);

// This works and uses *local* storage:
export const events_loc: Writable<key_val> = persisted('ae_events_loc', events_local_data_struct);
// console.log(`AE Stores - App Local Storage Data:`, get(ae_loc));

/* *** BEGIN *** Initialize events_session_data_struct */
// Temporary app data. This should be stored to session storage.
// Updated 2024-03-06
const events_session_data_struct: key_val = {
    ver: ver,
    ver_idb: ver_idb,
    log_lvl: 1,

    // Shared
    ds: {
        submit_status: null
    },
    ds_loaded: {},

    qry__enabled: 'enabled', // all, disabled, enabled
    qry__hidden: 'not_hidden', // all, hidden, not_hidden
    qry__limit: 20,
    qry__offset: 0,

    // This is intended to only be temporary.
    auth__person: {},
    auth__entered_key: null,
    auth__entered_passcode: null,
    auth__kv: {
        event: {},
        exhibit: {},
        location: {},
        session: {},
        presentation: {},
        presenter: {},
        person: {}
    },

    // Badge Printing
    badges: {
        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
    },

    // Event Files - uploads for sessions, presenters, etc
    files: {
        disable_submit__event_file_obj: null,
        status__submit: null,
        status__file_list: null, // processing, complete

        processed_file_list: []
    },

    // Event Presentation Launcher (and native Electron app)
    launcher: {
        ws_connect_status: null,
        av_recording_status: null,
        controller_cmd: null,
        controller_trigger_send: null,
        event_file_open: {}, // This is from the older Launcher.
        native: {},

        // Shared observability for Sync Monitor and Config Drawer
        sync_stats: {
            total: 0,
            cached: 0,
            missing: 0,
            currently_syncing: null
        },
        heartbeat_info: {
            last_timestamp: null,
            status: 'pending'
        },

        modal__title: '',
        modal__open_event_file_id: false,
        modal__event_file_obj: null,

        loading__session_li_status: null,
        loading__session_id_status: null,

        trigger_reload__event_session_obj_id: null,
        trigger_reload__event_session_obj_li: null,
        trigger_reload__event_location_obj_id: null,
        trigger_reload__event_location_obj_li: null,

        trigger__ws_connect: null,
        trigger__ws_disconnect: null
    },

    // Lead Retrievals (Exhibit)
    leads: {
        example: true,

        show_form__license: false,
        show_form__search: false,
        show_form__scan: false,
        show_form__view_lead: false, // Set to event_exhibit_tracking_id
        // show_form__view_lead: [],

        show_confirm__add_lead: [],

        submit_status__license: null, // 'saving', 'created', 'updated'
        // create_status__license: null, // 'creating', 'created', 'updated'
        // update_status__license: null, // 'updating', 'created', 'updated'

        submit_status__search: null, // 'searching', 'complete'

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

        // The entered_passcode is the exhibit booths shared passcode for staff. This is used to initially access the lead retrieval service.
        entered_passcode: null,

        tmp_license: {
            index: null,
            // 'agree' : false, // The user must agree to the license agreement.
            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
    },

    stripe: {
        license_qty: 1,
        rental_qty: 0,
        rental_option: true,
        api_use: false,
        client_reference_id: null,
        publishable_key: 'pk_live_zqaWNDfak2eDHeqnRiyaJcFi',
        btn_payment_id: null,
        btn_1_license: 'buy_btn_1OvqWJ2gJkNsDuiNqMCWz5nG',
        btn_1_license_rental: 'buy_btn_1OvqVA2gJkNsDuiNhk9r8Io2',
        btn_3_license: 'buy_btn_1OvrI22gJkNsDuiNXjBg3c4Y',
        btn_3_license_rental: 'buy_btn_1OvrKa2gJkNsDuiNhSBCkNau',
        btn_6_license: 'buy_btn_1OvrWc2gJkNsDuiN7mnwvZNL',
        btn_6_license_rental: 'buy_btn_1OvrXP2gJkNsDuiNZpWZs3Uy',
        btn_10_license: 'buy_btn_1OvrPM2gJkNsDuiNRCMHfSuz',
        btn_10_license_rental: 'buy_btn_1OvrPs2gJkNsDuiN1nPkjPOM'
    },

    // Presentation Management
    pres_mgmt: {
        // link: {
        //     ae_core: true,
        //     pres_mgmt__launcher_id: null, // event_location_id
        //     pres_mgmt__location_id: null, // event_location_id
        //     pres_mgmt__presenter_id: null, // event_presenter_id
        //     pres_mgmt__reports: null, // event_id
        //     pres_mgmt__session_id: null, // event_session_id
        //     pres_mgmt__session_search: null, // event_id
        // },
        // presenter__url_str: null,
        presenter__updated_on: null,
        session_updated_on: null,

        location_name_qry_str: null,
        fulltext_search_qry_str: null,
        status_qry__search: null,

        disable_submit__event_file_obj: true,

        show_form__search: true,
        show_form__search_results: true,

        show_content__agree_text: false,
        show_content__presenter_start: false,

        show_content__presentation_description: false, // Note that this is per presentation. The event_presentation_id_random should match.

        show_report: null,
        // show_report__presenters_agree: false,
        // show_report__recent_files: false,

        show_field_edit__filename: false, // For file rename

        new_upload_list: null,
        files_uploading_count: null,

        qry_limit__files: 75,
        qry_limit__presentations: 25,
        qry_limit__presenters: 500,
        qry_limit__sessions: 100,

        show_fields__presentation: true,
        show_fields__session: true,

        show_modal__presenter_agree: false,
        show__session_poc_profile: false,
        show_modal__session_poc_agree: false,

        // hide__edit_location: {},
        show__edit_location: {},

        show__edit_poc_person: {},

        show__view_alert: {}, // key values
        show__edit_alert_msg: {}, // key values

        tmp__alert_msg: {}, // key values

        session_qr_url: {}, // key value of session_id and URL string

        status_rpt: {
            recent_files: null,
            presenters_agree: null,
            presenters_biography: null
        },

        rpt__session_no_files: true,
        rpt__session_poc_agree: false, // Default to false for new events.
        rpt__session_no_bio: true,

        rpt__presenter_agree: true,

        tmp_val__filename_no_ext: null // For file rename
    }

    // Speakers Management (Collection)

    // other
};
// console.log(`AE Stores - App Events Session Storage Data:`, events_session_data_struct);
export const events_sess = writable(events_session_data_struct);

/* *** BEGIN *** Initialize events_slct and events_trigger */
/* The slct and slct_trigger variable should not be stored in local storage. Only use session storage because browser tabs can be open to different events, badges, exhibits, etc. */

// Intended for temporary session storage.
// Updated 2024-03-06
const events_slct_obj_template: key_val = {
    // Top level
    event_id: null,
    event_obj: {},
    event_obj_li: [],

    // Sub-level event_
    abstract_id: null,
    abstract_obj: {},
    abstract_obj_li: [],

    badge_id: null,
    badge_obj: {},
    badge_obj_li: [],

    badge_template_id: null,
    badge_template_obj: {},
    badge_template_obj_li: [],

    device_id: null,
    device_obj: {},
    device_obj_li: [],

    exhibit_id: null,
    exhibit_obj: {},
    exhibit_obj_li: [],

    // Rename these to badge_tracking_*?
    exhibit_tracking_id: null,
    exhibit_tracking_obj: {},
    exhibit_tracking_obj_li: [],

    file_id: null,
    file_obj: {},
    file_obj_li: [],
    event_file_obj: {},

    event_file_obj_li: [],

    location_id: null,
    location_obj: {},
    location_obj_li: [],

    person_id: null,
    person_obj: {},
    person_obj_li: [],

    presentation_id: null,
    presentation_obj: {},
    presentation_obj_li: [],
    event_presentation_obj: {},

    presenter_id: null,
    presenter_obj: {},
    presenter_obj_li: [],
    event_presenter_obj: {},

    session_id: null,
    event_session_id: null,
    session_obj: {},
    session_obj_li: [],
    event_session_obj: {},

    lq__presenter_obj: {}, // Testing passing a LiveQuery object around...

    auth__event_presenter_id: null,
    auth__event_presentation_id: null
};
// console.log(`AE Stores - Selected Events Objects:`, events_slct_obj_template);

// This works, and uses *session* (not local) storage:
export const events_slct = writable(events_slct_obj_template);

// This works and uses *local* storage:
// export let events_slct: Writable<key_val> = persisted('ae_events_slct', events_slct_obj_template);

/* *** BEGIN *** Initialize events_trigger */
// Intended for temporary session storage.
// Updated 2024-03-06
export const events_trigger: any = writable(null);
// console.log(`AE Events Stores - Events Trigger:`, events_trigger);

const tmp__events_trig: key_val = {
    event_id: null,
    event_id_li: [],
    event_location_id: null,
    event_location_id_li: [],
    event_session_id: null,
    event_session_id_li: [],
    event_presentation_id: null,
    event_presentation_id_li: [],
    event_presenter_id: null,
    event_presenter_id_li: []
};
// console.log(`AE Stores - Events Trigger:`, events_trig);
export const events_trig: Writable<key_val> = writable(tmp__events_trig);

/* *** BEGIN *** TESTING Initialize trig_resp */
// The idea behind this is for a shared (Svelte app (within Events for now)) trigger and response. In theory this could be used to monitor multiple downloads or have a universal status area. Intended for temporary session storage.
// Updated 2024-06-25
const tmp__events_trig_kv: key_val = {};
// {
// 'example-1':
//     {
//         'a-rand-id-1': true,
//         'a-rand-id-2': false,
//         'a-rand-id-3': Promise.resolve('This is a test promise.'),
//     },
// 'example-2':
//     {
//         'a-rand-id-4': true,
//         'a-rand-id-5': false,
//         'a-rand-id-6': Promise.resolve('This is a test promise.'),
//     },
// };
export const events_trig_kv = writable(tmp__events_trig_kv);