/** @type {import('./$types').PageLoad} */
import { error } from '@sveltejs/kit';
console.log(`ae events launcher [event_location_id] +page.ts: start`);

import { browser } from '$app/environment';
import { events_func } from '$lib/ae_events/ae_events_functions';

export async function load({ params, parent, url }) {
    // route
    const log_lvl: number = 1;

    const data = await parent();
    // console.log(`ae events_pres_mgmt event [event_location_id] +page.ts data:`, data);
    data.log_lvl = log_lvl;

    const account_id = data.account_id;
    let ae_acct = data[account_id];

    if (!ae_acct) {
        console.warn(
            `ae Events - [event_id] launcher [event_location_id] +page.ts: Account ${account_id} not found in data. Initializing ghost acct.`
        );
        ae_acct = {
            api: data.ae_api || {},
            slct: {
                account_id: account_id
            }
        };
    }

    const event_location_id = params.event_location_id;
    if (!event_location_id) {
        console.warn(
            `ae Events - [event_id] launcher [event_location_id] +page.ts: The event_location_id was not found in the params.event_location_id!!!`
        );
        // error(404, {
        //     message: 'Events Pres Mgmt - Event Location ID not found'
        // });
    }

    if (browser && event_location_id) {
        if (log_lvl) {
            console.log(
                `ae_events launcher [event_location_id] +page.ts: event_location_id = `,
                event_location_id
            );
        }

        // OPTIMIZATION: Fire the session list refresh in the background.
        // The Launcher components already use liveQuery to watch Dexie,
        // so they will update automatically once this background task finishes.
        events_func.load_ae_obj_li__event_session({
            api_cfg: ae_acct.api,
            for_obj_type: 'event_location',
            for_obj_id: event_location_id,
            inc_file_li: false, // Optimized: Shell load for lists
            inc_all_file_li: false,
            inc_presentation_li: false, // Optimized: Shell load for lists
            inc_presenter_li: false,
            enabled: 'enabled',
            hidden: 'all',
            view: 'alt', // Standardized View for file counts and extended metadata
            limit: 150,
            log_lvl: 0
        });
    } else {
        console.log(
            `ae pres_mgmt launcher [slug] +page.ts: browser = false or location_id missing`
        );
    }

    const session_id = url.searchParams.get('session_id');
    if (browser && session_id) {
        if (log_lvl)
            console.log(`Triggering deep load for session_id: ${session_id}`);
        events_func.load_ae_obj_id__event_session({
            api_cfg: ae_acct.api,
            event_session_id: session_id,
            inc_file_li: true,
            inc_all_file_li: true,
            inc_presentation_li: true,
            inc_presenter_li: true,
            try_cache: true,
            log_lvl: 0
        });
    }

    // WARNING: Precaution against shared data between sites and sessions.
    data[account_id] = ae_acct;

    return data;
}
