/** @type {import('./$types').PageLoad} */

console.log(`ae_events_pres_mgmt_event [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 }) {
    // route
    const log_lvl: number = 0;

    const data = await parent();

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

    const event_location_id = params.event_location_id;
    if (!event_location_id) {
        console.warn(
            `ae events_pres_mgmt location [event_location_id] +page.ts: The event_location_id was not found in the params!!!`
        );
    }
    ae_acct.slct.event_location_id = event_location_id;

    if (browser && event_location_id) {
        if (log_lvl)
            console.log(
                `ae_events [event_location_id] +page.ts (Non-Blocking Refresh)`
            );

        // OPTIMIZATION: Fire these in the background without 'await'.
        // The Location View components use LiveQuery/Dexie and will update
        // automatically once these background tasks complete.

        // 1. Refresh the location object
        events_func.load_ae_obj_id__event_location({
            api_cfg: ae_acct.api,
            event_location_id: event_location_id,
            inc_file_li: true,
            inc_session_li: true,
            inc_device_li: true,
            inc_presentation_li: true,
            inc_presenter_li: true,
            log_lvl: log_lvl
        });

        // 2. Refresh sessions and their nested presentations (REDUNDANT: Now handled by inc_session_li above)
        /*
        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_presentation_li: true,
            inc_presenter_li: true,
            enabled: 'all',
            limit: 50,
            log_lvl: log_lvl
        });
        */

        // 4. Refresh devices for the location (REDUNDANT: Now handled by inc_device_li above)
        /*
        events_func.load_ae_obj_li__event_device({
            api_cfg: ae_acct.api,
            for_obj_type: 'event_location',
            for_obj_id: event_location_id,
            log_lvl: log_lvl
        });
        */
    }

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

    return data;
}
