import { AE_IDAA_LOC_VERSION } from '$lib/stores/store_versions';
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';

/* *** BEGIN *** Initialize idaa_local_data_struct */
// Persisted to localStorage. Retains Novi identity, auth state, and IDAA
// query preferences across sessions. Wiped on schema change via store_versions.ts.
const idaa_local_data_struct: key_val = {
    __version: AE_IDAA_LOC_VERSION,

    name: 'Aether - IDAA',
    title: `OSIT's Æ IDAA`,

    novi_uuid: null,
    novi_email: null,
    novi_full_name: null,
    // True after a successful Novi API verification (UUID confirmed to be a real Novi member).
    // False on load, on verification failure, or for non-Novi sign-in paths.
    novi_verified: false,
    // Timestamp (ms since epoch) when the last successful verification occurred.
    // Used to cache verification results and avoid repeated Novi API calls.
    novi_verified_ts: null,
    // Populated from $ae_loc.site_cfg_json at IDAA layout mount — not managed here.
    // See routes/idaa/(idaa)/+layout.svelte for the override logic.
    novi_admin_li: [],
    novi_trusted_li: [],
    novi_jitsi_mod_li: [],

    archives: {
        enabled: 'enabled', // all, disabled, enabled
        hidden: 'not_hidden', // all, hidden, not_hidden
        limit: 150,
        offset: 0,

        edit_kv: {}, // Tracks which archive objects are being edited.
        edit__archive_obj: null,
        edit__archive_content_obj: null
    },

    bb: {
        enabled: 'enabled', // all, disabled, enabled
        hidden: 'not_hidden', // all, hidden, not_hidden
        limit: 50,
        offset: 0,

        edit_kv: {}, // Tracks which post objects are being edited.
        edit__post_obj: null,
        edit__post_comment_obj: null,

        show_list__post_obj_li: true,

        qry__enabled: 'enabled', // all, disabled, enabled
        qry__hidden: 'not_hidden', // all, hidden, not_hidden
        qry__limit: 25,
        qry__offset: 0,
        qry__order_by: 'updated_on', //  For the IDB index query
        qry__order_by_li: { updated_on: 'DESC', created_on: 'DESC' } // For the SQL query
    },

    recovery_meetings: {
        edit_kv: {}, // Tracks which meeting objects are being edited.
        edit__event_obj: null,

        qry__enabled: 'enabled', // all, disabled, enabled
        qry__hidden: 'not_hidden', // all, hidden, not_hidden
        qry__limit: 100,
        qry__order_by: 'updated_on', //  For the IDB index query; name, updated_on/created_on
        qry__order_by_li: {
            priority: 'DESC',
            sort: 'DESC',
            updated_on: 'DESC',
            created_on: 'DESC',
            name: 'ASC'
        }, // For the SQL query
        qry__offset: 0,

        qry__fulltext_str: null,
        qry__physical: null,
        qry__type: null,
        qry__virtual: null,

        // Favorites filter — when true, only show meetings the member has starred.
        // Favorites are stored server-side in event.mod_meetings_json.favorite (array of Novi UUIDs),
        // so they persist across browsers without requiring a Novi API write capability.
        qry__favorites_only: false,

        // Collapse the "Meeting Info" data store panel between the search bar and results.
        // Persisted so the user's preference survives page reloads.
        ds_info_collapsed: false
    }
};

// DEPRECATED: Migrate to $lib/stores/ae_idaa_stores__idaa_loc.svelte.ts.
// This store uses legacy 'svelte-persisted-store'.
// Do not add new features here; implement in the Svelte 5 PersistedState store.
export const idaa_loc: Writable<key_val> = persisted(
    'ae_idaa_loc',
    idaa_local_data_struct
);

/* *** BEGIN *** Initialize idaa_session_data_struct */
// In-memory only (not persisted). Resets on page load.
const idaa_session_data_struct: key_val = {
    log_lvl: 1,

    archives: {
        qry__status: null,
        show__modal_edit__archive_id: null,
        show__modal_view__archive_id: null,
        show__modal_edit__archive_content_id: null,
        show__modal_view__archive_content_id: null,
        obj_changed: false
    },

    bb: {
        qry__status: null,

        edit__post_obj: null,

        show__inline_edit__post_obj: null,
        show__modal_edit__post_id: null,
        show__modal_view__post_id: null,
        obj_changed: false
    },

    recovery_meetings: {
        qry__status: null,
        qry__fulltext_str: null,
        search_version: 0,

        edit__event_obj: null,

        status_qry__last_request_str: null,

        show__modal_edit: false,
        show__modal_view: false,
        show__modal_edit__event_id: null,
        show__modal_view__event_id: null,
        obj_changed: false,

        attend_platform: null // 'Zoom', 'Google Meet', 'Microsoft Teams', etc.
    }
};
export const idaa_sess = writable(idaa_session_data_struct);

// In-memory only — tabs can have different IDAA object selections.
const idaa_slct_obj_template: key_val = {
    // Top level
    event_id: null,
    event_obj: {},
    event_obj_li: [],

    archive_id: null,
    archive_obj: {},
    archive_obj_li: [],

    archive_content_id: null,
    archive_content_obj: {},
    archive_content_obj_li: [],

    post_id: null,
    post_obj: {},
    post_obj_li: [],

    post_comment_id: null,
    post_comment_obj: {},
    post_comment_obj_li: []
};
export const idaa_slct = writable(idaa_slct_obj_template);

// Fine-grained triggers — set an ID to signal that object type needs re-fetching.
const idaa_trig_template: key_val = {
    archive_id: false,
    archive_content_li: false,
    event_id: false,
    post_id: false,
    update_zoom_full_url: false
};
export const idaa_trig: Writable<key_val> = writable(idaa_trig_template);

// Promise map — keyed by object type; used to track in-flight async operations.
const idaa_prom_template: key_val = {
    archive_id: false,
    archive_content_li: false,
    event_id: false,
    post_id: false
};
export const idaa_prom: Writable<key_val> = writable(idaa_prom_template);
