/**
 * ae_events_stores__launcher_defaults.ts
 *
 * Default state for the launcher (Event Presentation Launcher + Electron native app)
 * section of ae_events_stores.ts.
 *   launcher_loc_defaults  → events_loc.launcher  (persisted to localStorage)
 *   launcher_sess_defaults → events_sess.launcher (in-memory, resets on page load)
 */

import type { LaunchProfile } from '$lib/ae_events/ae_launcher__default_launch_profiles';

/** 3-way section collapse state used throughout the Launcher UI. */
export type SectionState = 'collapsed' | 'auto' | 'pinned';

export interface LauncherLocState {
    app_mode: 'default' | 'onsite' | 'native';
    ws_connect: boolean;
    qry_limit__files: number;
    qry_limit__presentations: number;
    qry_limit__presenters: number;
    qry_limit__sessions: number;
    hide__launcher_header: boolean;
    hide__launcher_menu: boolean;
    hide__launcher_footer: boolean;
    hide__modal_header_title: boolean;
    hide_drawer__debug: boolean;
    hide__ws_element: boolean;
    hide__ws_form: boolean;
    hide__ws_messages: boolean;
    hide__ws_commands: boolean;
    show_content__disabled_files: boolean;
    show_content__hidden_files: boolean;
    show_content__hidden_presentations: boolean;
    show_content__hidden_presenters: boolean;
    show_content__hidden_sessions: boolean;
    show_content__internal_files: boolean;
    show_content__session_code: boolean;
    show_content__presentation_code: boolean;
    show_content__presenter_code: boolean;
    show_section__controller: boolean;
    section_state__health: SectionState;
    section_state__native_os: SectionState;
    section_state__launch_timing: SectionState;
    section_state__wallpaper: SectionState;
    section_state__sync_timers: SectionState;
    section_state__updates: SectionState;
    section_state__controller: SectionState;
    section_state__screen_saver: SectionState;
    section_state__app_modes: SectionState;
    section_state__local_actions: SectionState;
    /** URL last successfully applied as wallpaper on this device. Used to detect remote config changes. */
    wallpaper_applied_url: string | null;
    /** URL last applied to the external display (when url_external is configured). */
    wallpaper_applied_url_external: string | null;
    datetime_format: string;
    time_format: string;
    time_hours: 12 | 24;
    sync_intervals: {
        event: number;
        device: number;
        location: number;
        session: number;
        presentation: number;
        presenter: number;
        file_sync: number;
    };
    slct: {
        event_id: string | null;
        event_location_id: string | null;
        event_session_id: string | null;
        event_presentation_id: string | null;
        event_presenter_id: string | null;
        event_file_id: string | null;
    };
    native: {
        host_file_config_path: string;
    };
    idle_timer: number;
    idle_cycle: number;
    idle_loop_period: number;
    screen_saver_img_kv: Record<string, any>;
    modal__title: string;
    modal__open: string | null;
    modal__open_filename: string | null;
    modal_img_src: string | null;
    controller: string;
    controller_group_code: string;
    controller_client_id: string | null;
    /**
     * Native test mode: simulates the full native-branch open flow without Electron.
     * Shows a debug popup with the resolved profile, commands, and AppleScript instead
     * of actually launching files. Useful for testing LaunchProfile config from any
     * device/OS without deploying to the Mac laptop.
     */
    native_test_mode: boolean;
    /**
     * Per-file display mode overrides, keyed by event_file_id.
     * Stored locally (per-device) because event_file has no cfg_json column yet.
     * TODO: migrate to event_file.cfg_json once the backend column is added.
     */
    file_display_overrides: Record<string, 'extend' | 'mirror' | 'none'>;
    /**
     * Local per-device launch profile overrides, keyed by profile/extension name.
     * Overrides DEFAULT_LAUNCH_PROFILES for this device only.
     * Priority: device API config > this local override > built-in defaults.
     */
    launch_profiles: Record<string, Partial<LaunchProfile>> | null;
}

export interface LauncherSessState {
    ws_connect_status: string | null;
    av_recording_status: string | null;
    controller_cmd: string | null;
    controller_trigger_send: any;
    event_file_open: Record<string, any>;
    native: Record<string, any>;
    sync_stats: {
        total: number;
        cached: number;
        missing: number;
        currently_syncing: string | null;
    };
    heartbeat_info: {
        last_timestamp: string | null;
        status: string;
    };
    modal__title: string;
    modal__open_event_file_id: boolean | string;
    modal__event_file_obj: Record<string, any> | null;
    loading__session_li_status: string | null;
    loading__session_id_status: string | null;
    trigger_reload__event_session_obj_id: string | null;
    trigger_reload__event_session_obj_li: string | null;
    trigger_reload__event_location_obj_id: string | null;
    trigger_reload__event_location_obj_li: string | null;

    trigger__force_location_sync: any;

    trigger__ws_connect: any;
    trigger__ws_disconnect: any;
}

// Persisted launcher config — survives browser sessions.
export const launcher_loc_defaults: LauncherLocState = {
    // App mode: 'default' = browser, 'onsite' = browser onsite, 'native' = Electron app onsite.
    app_mode: 'default',
    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,

    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__internal_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__launch_timing: 'collapsed',
    section_state__wallpaper: '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
    },

    native: {
        host_file_config_path:
            'device_configs/ae_native_app_config.default.json'
    },

    idle_timer: 7 * 60 * 1000, // Milliseconds until idle state is entered.
    idle_cycle: 5 * 1000, // How frequently idle status is checked (ms).
    idle_loop_period: 3 * 60 * 1000, // How frequently screen-saver loop and similar run (ms).

    screen_saver_img_kv: {}, // Built when launcher_file_cont loads — image files only.
    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,
    native_test_mode: false,
    wallpaper_applied_url: null,
    wallpaper_applied_url_external: null,
    file_display_overrides: {},
    launch_profiles: null
    // controller_cmd: null,
    // controller_trigger_send: null,
};

// In-memory launcher state — resets on page load (mostly real-time status/triggers).
export const launcher_sess_defaults: LauncherSessState = {
    ws_connect_status: null,
    av_recording_status: null,
    controller_cmd: null,
    controller_trigger_send: null,
    event_file_open: {}, // From older launcher version; retained for compatibility.
    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__force_location_sync: null,

    trigger__ws_connect: null,
    trigger__ws_disconnect: null
};
