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

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

// Static display title for the Events module. Always this value — nothing writes to it.
// Use this constant instead of $events_loc.title (which is being retired).
export const EVENTS_MODULE_TITLE = `OSIT's Æ Events`;

import { badges_sess_defaults } from '$lib/stores/ae_events_stores__badges_defaults';
import { launcher_sess_defaults } from '$lib/stores/ae_events_stores__launcher_defaults';
import { leads_sess_defaults } from '$lib/stores/ae_events_stores__leads_defaults';
import { pres_mgmt_sess_defaults } from '$lib/stores/ae_events_stores__pres_mgmt_defaults';

// Deployment version stamp. Compared against events_sess.ver in events/+layout.svelte
// to detect stale persisted data after a deploy (triggers a reload). Bump this alongside
// events_session_data_struct.ver. See store_versions.ts for the schema-level wipe mechanism.
const ver = '2025-10-16_2139';

/* *** BEGIN *** Initialize events_session_data_struct */
// In-memory only (writable, not persisted). Resets on page load.
const events_session_data_struct: key_val = {
    // Deployment version stamp — bump alongside ver above when pushing breaking changes.
    ver: ver,
    log_lvl: 1,

    // Shared
    ds: {
        submit_status: null
    },

    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 — see ae_events_stores__badges_defaults.ts
    badges: badges_sess_defaults,

    // 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 — see ae_events_stores__launcher_defaults.ts
    launcher: launcher_sess_defaults,

    // Lead Retrievals (Exhibit) — see ae_events_stores__leads_defaults.ts
    leads: leads_sess_defaults,

    // Presentation Management — see ae_events_stores__pres_mgmt_defaults.ts
    pres_mgmt: pres_mgmt_sess_defaults

    // Speakers Management (Collection)

    // other
};
export const events_sess = writable(events_session_data_struct);

/* *** BEGIN *** Initialize events_slct and events_trigger */
// In-memory only — tabs can have different event/session/presenter selections.

// 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_
    badge_id: null,
    badge_obj: {},
    badge_obj_li: [],

    exhibit_id: null,
    exhibit_obj: {},
    exhibit_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: {},

    auth__event_presenter_id: null,
    auth__event_presentation_id: null
};
// In-memory only (not persisted) — separate tabs can select different events/sessions/presenters.
export const events_slct = writable(events_slct_obj_template);

// Broadcast trigger — increment or set truthy to signal subscribers to re-query.
export const events_trigger: any = writable(null);

// events_trig: fine-grained triggers per object type.
// Set an ID to signal that specific object needs to be re-fetched.
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);

// events_trig_kv: response/status map — keys are request IDs, values are
// completion status or Promise results. Used for tracking parallel downloads.
const tmp__events_trig_kv: key_val = {};
export const events_trig_kv = writable(tmp__events_trig_kv);
