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

import { db_save_ae_obj_li__ae_obj } from '$lib/ae_core/core__idb_dexie';
import { db_events } from '$lib/ae_events/db_events';
import type { ae_EventFile } from '$lib/types/ae_types';

const ae_promises: key_val = {};

// Updated 2026-01-30: Fixed - Removed aggressive ID overwriting
export async function load_ae_obj_id__event_file({
    api_cfg,
    event_file_id,
    inc_hosted_file = false,
    view = 'default',
    try_cache = true,
    log_lvl = 0
}: {
    api_cfg: any;
    event_file_id: string;
    inc_hosted_file?: boolean;
    view?: string;
    try_cache?: boolean;
    log_lvl?: number;
}): Promise<ae_EventFile | null> {
    if (log_lvl) {
        console.log(
            `*** load_ae_obj_id__event_file() *** [V3] id=${event_file_id} (SWR)`
        );
    }

    if (try_cache) {
        try {
            const cached = await db_events.file.get(event_file_id);
            if (cached) {
                _refresh_file_id_background({
                    api_cfg,
                    event_file_id,
                    inc_hosted_file,
                    view,
                    try_cache,
                    log_lvl: 0
                });
                return cached;
            }
        } catch (e) {}
    }

    return await _refresh_file_id_background({
        api_cfg,
        event_file_id,
        inc_hosted_file,
        view,
        try_cache,
        log_lvl
    });
}

async function _refresh_file_id_background({
    api_cfg,
    event_file_id,
    inc_hosted_file = false,
    view,
    try_cache,
    log_lvl
}: any) {
    if (typeof navigator !== 'undefined' && !navigator.onLine) return null;
    try {
        const result = await api.get_ae_obj({
            api_cfg,
            obj_type: 'event_file',
            obj_id: event_file_id,
            view,
            params: { inc_hosted_file },
            log_lvl
        });
        if (result) {
            const processed = await process_ae_obj__event_file_props({
                obj_li: [result],
                log_lvl
            });
            const processed_obj = processed[0];
            if (try_cache) {
                await db_save_ae_obj_li__ae_obj({
                    db_instance: db_events,
                    table_name: 'file',
                    obj_li: [processed_obj],
                    properties_to_save,
                    log_lvl
                });
            }
            return processed_obj;
        }
    } catch (e) {}
    return null;
}

export async function load_ae_obj_li__event_file({
    api_cfg,
    for_obj_type,
    for_obj_id,
    inc_hosted_file = false,
    enabled = 'enabled',
    hidden = 'not_hidden',
    view = 'default',
    limit = 100,
    offset = 0,
    order_by_li = [
        { priority: 'DESC' },
        { sort: 'DESC' },
        { updated_on: 'DESC' }
    ],
    try_cache = true,
    log_lvl = 0
}: {
    api_cfg: any;
    for_obj_type: string;
    for_obj_id: string;
    inc_hosted_file?: boolean;
    enabled?: 'enabled' | 'all' | 'not_enabled';
    hidden?: 'hidden' | 'all' | 'not_hidden';
    view?: string;
    limit?: number;
    offset?: number;
    order_by_li?: any;
    try_cache?: boolean;
    log_lvl?: number;
}): Promise<ae_EventFile[]> {
    if (log_lvl) {
        console.log(
            `*** load_ae_obj_li__event_file() *** [V3] for=${for_obj_type}:${for_obj_id} (SWR)`
        );
    }

    if (try_cache) {
        try {
            const cached_li = await db_events.file
                .where('for_id')
                .equals(for_obj_id)
                .toArray();
            if (cached_li && cached_li.length > 0) {
                _refresh_file_li_background({
                    api_cfg,
                    for_obj_type,
                    for_obj_id,
                    inc_hosted_file,
                    enabled,
                    hidden,
                    view,
                    limit,
                    offset,
                    order_by_li,
                    try_cache,
                    log_lvl: 0
                });
                return cached_li;
            }
        } catch (e) {}
    }

    return await _refresh_file_li_background({
        api_cfg,
        for_obj_type,
        for_obj_id,
        inc_hosted_file,
        enabled,
        hidden,
        view,
        limit,
        offset,
        order_by_li,
        try_cache,
        log_lvl
    });
}

async function _refresh_file_li_background({
    api_cfg,
    for_obj_type,
    for_obj_id,
    inc_hosted_file = false,
    enabled,
    hidden,
    view,
    limit,
    offset,
    order_by_li,
    try_cache,
    log_lvl
}: any) {
    if (typeof navigator !== 'undefined' && !navigator.onLine) return [];
    try {
        if (log_lvl) {
            console.log(
                `📡 [DEBUG] _refresh_file_li_background: Fetching files for ${for_obj_type}:${for_obj_id}`
            );
        }

        const result_li = await api.get_ae_obj_li({
            api_cfg,
            obj_type: 'event_file',
            for_obj_type,
            for_obj_id,
            enabled,
            hidden,
            view,
            limit,
            offset,
            order_by_li,
            params: { inc_hosted_file },
            log_lvl: log_lvl
        });

        if (result_li) {
            if (log_lvl) {
                console.log(
                    `📦 [DEBUG] Raw API results count:`,
                    result_li.length
                );
                if (result_li.length > 0) {
                    console.log(`🔍 [DEBUG] Sample Raw IDs:`, {
                        id: result_li[0].id,
                        event_file_id: result_li[0].event_file_id,
                        event_id: result_li[0].event_id,
                        for_id: result_li[0].for_id,
                        for_type: result_li[0].for_type
                    });
                }
            }

            // Minimal Fix: Only set what we are absolutely sure of if missing
            const fixed_li = result_li.map((obj: any) => {
                const fixed_obj = { ...obj };
                if (!fixed_obj.for_type) fixed_obj.for_type = for_obj_type;
                if (!fixed_obj.for_id) fixed_obj.for_id = for_obj_id;

                // Map specific ID field (e.g. event_presenter_id) ONLY if it matches the current for_obj_type
                const specific_id_key = `${for_obj_type}_id`;
                if (!fixed_obj[specific_id_key]) {
                    fixed_obj[specific_id_key] = for_obj_id;
                }

                return fixed_obj;
            });

            const processed = await process_ae_obj__event_file_props({
                obj_li: fixed_li,
                log_lvl
            });

            if (log_lvl && processed.length > 0) {
                console.log(`🛠️ [DEBUG] Sample Processed Record:`, {
                    id: processed[0].id,
                    event_id: processed[0].event_id,
                    for_id: processed[0].for_id,
                    event_presenter_id: processed[0].event_presenter_id
                });
            }

            if (try_cache) {
                if (log_lvl)
                    console.log(
                        `💾 [DEBUG] Saving ${processed.length} records to ae_events_db.file`
                    );
                await db_save_ae_obj_li__ae_obj({
                    db_instance: db_events,
                    table_name: 'file',
                    obj_li: processed,
                    properties_to_save,
                    log_lvl
                });

                // Prune stale Dexie records that no longer exist on the server.
                // WHY: bulkPut only upserts — it never removes records deleted on
                // the server. Without this, deleted files remain visible in the UI
                // indefinitely (the liveQuery sees them in Dexie forever).
                //
                // Scope guard: only delete records that WOULD have been included in
                // this query. Records outside the query scope (e.g. a hidden file
                // when hidden='not_hidden') are intentionally left in Dexie so
                // that a later 'all' query can still find them.
                // We prune on any valid API response, including an empty list —
                // an empty array from the API is authoritative ("no files here").
                const returned_ids = new Set(
                    processed.map((f: any) => f.id).filter(Boolean)
                );
                const dexie_all = await db_events.file
                    .where('for_id')
                    .equals(for_obj_id)
                    .toArray();
                const stale_ids = dexie_all
                    .filter((f) => {
                        if (!f.id || returned_ids.has(f.id)) return false;
                        // Would this record have appeared in our query?
                        const is_enabled = !!f.enable;
                        const is_hidden = !!f.hide;
                        const within_enabled_scope =
                            enabled === 'all' ||
                            (enabled === 'enabled' && is_enabled) ||
                            (enabled === 'not_enabled' && !is_enabled);
                        const within_hidden_scope =
                            hidden === 'all' ||
                            (hidden === 'not_hidden' && !is_hidden) ||
                            (hidden === 'hidden' && is_hidden);
                        return within_enabled_scope && within_hidden_scope;
                    })
                    .map((f) => f.id as string);
                if (stale_ids.length > 0) {
                    if (log_lvl)
                        console.log(
                            `🗑️ [DEBUG] Pruning ${stale_ids.length} stale file records from Dexie`,
                            stale_ids
                        );
                    await db_events.file.bulkDelete(stale_ids);
                }
            }
            return processed;
        }
    } catch (e) {
        if (log_lvl)
            console.error(
                `❌ [DEBUG] Error in _refresh_file_li_background:`,
                e
            );
    }
    return [];
}
export async function create_event_file_obj_from_hosted_file_async({
    api_cfg,
    hosted_file_id,
    params = {},
    data = {},
    return_obj = false,
    inc_hosted_file = false,
    log_lvl = 0
}: {
    api_cfg: any;
    hosted_file_id: string;
    params?: key_val;
    data?: key_val;
    return_obj?: boolean;
    inc_hosted_file?: boolean;
    log_lvl?: number;
}) {
    if (!hosted_file_id) return false;

    // Use V3 endpoint for creation from hosted file
    const endpoint = `/v3/action/event_file/from_hosted_file/${hosted_file_id}`;
    const query_params = { ...params };
    if (return_obj) query_params['return_obj'] = true;
    if (inc_hosted_file) query_params['inc_hosted_file'] = true;

    const result = await api.post_object({
        api_cfg,
        endpoint,
        params: query_params,
        data,
        log_lvl
    });

    if (return_obj) return result;
    return result?.event_file_id || result?.id;
}

export async function delete_ae_obj_id__event_file({
    api_cfg,
    event_file_id,
    params = {},
    try_cache = true,
    log_lvl = 0
}: {
    api_cfg: any;
    event_file_id: string;
    params?: key_val;
    try_cache?: boolean;
    log_lvl?: number;
}) {
    // Single atomic call: removes hosted_file_link, cleans up physical file +
    // hosted_file record if no other links remain (rm_orphan=true is the default),
    // then deletes the event_file row. Replaces the old multi-step workaround.
    const result = await api.delete_object({
        api_cfg,
        endpoint: `/v3/action/event_file/${event_file_id}`,
        params,
        log_lvl
    });
    if (try_cache) await db_events.file.delete(event_file_id);
    return result;
}

export async function update_ae_obj__event_file({
    api_cfg,
    event_file_id,
    data_kv,
    params = {},
    try_cache = true,
    log_lvl = 0
}: {
    api_cfg: any;
    event_file_id: string;
    data_kv: key_val;
    params?: key_val;
    try_cache?: boolean;
    log_lvl?: number;
}): Promise<ae_EventFile | null> {
    const result = await api.update_ae_obj({
        api_cfg,
        obj_type: 'event_file',
        obj_id: event_file_id,
        fields: data_kv,
        params,
        log_lvl
    });
    if (result && try_cache) {
        const processed = await process_ae_obj__event_file_props({
            obj_li: [result],
            log_lvl
        });
        await db_save_ae_obj_li__ae_obj({
            db_instance: db_events,
            table_name: 'file',
            obj_li: processed,
            properties_to_save,
            log_lvl
        });
    }
    return result;
}

export async function search__event_file({
    api_cfg,
    event_id,
    qry_str = '',
    qry_created_on = null,
    qry_min_file_size = null,
    qry_file_purpose = null,
    inc_hosted_file = false,
    enabled = 'enabled',
    hidden = 'not_hidden',
    view = 'default',
    limit = 25,
    offset = 0,
    order_by_li = [
        { priority: 'DESC' },
        { sort: 'DESC' },
        { updated_on: 'DESC' }
    ],
    try_cache = true,
    log_lvl = 0
}: {
    api_cfg: any;
    event_id: string;
    qry_str?: string;
    qry_created_on?: string | null;
    qry_min_file_size?: null | number;
    qry_file_purpose?: string | null;
    inc_hosted_file?: boolean;
    enabled?: 'enabled' | 'all' | 'not_enabled';
    hidden?: 'hidden' | 'all' | 'not_hidden';
    view?: string;
    limit?: number;
    offset?: number;
    order_by_li?: any;
    try_cache?: boolean;
    log_lvl?: number;
}): Promise<ae_EventFile[]> {
    const search_query: any = {
        q: qry_str,
        and: [{ field: 'event_id', op: 'eq', value: event_id }]
    };
    if (qry_min_file_size)
        search_query.and.push({
            field: 'hosted_file_size',
            op: 'gt',
            value: qry_min_file_size
        });
    if (qry_created_on)
        search_query.and.push({
            field: 'created_on',
            op: 'gte',
            value: qry_created_on
        });
    if (qry_file_purpose)
        search_query.and.push({
            field: 'file_purpose',
            op: 'eq',
            value: qry_file_purpose
        });
    const result_li = await api.search_ae_obj({
        api_cfg,
        obj_type: 'event_file',
        search_query,
        enabled,
        hidden,
        view,
        order_by_li,
        limit,
        offset,
        params: { inc_hosted_file },
        log_lvl
    });

    // CRITICAL: Always process the results to map hosted_file_size → file_size
    // and other field mappings for UI consistency
    if (result_li) {
        const processed = await process_ae_obj__event_file_props({
            obj_li: result_li,
            log_lvl
        });

        if (try_cache) {
            await db_save_ae_obj_li__ae_obj({
                db_instance: db_events,
                table_name: 'file',
                obj_li: processed,
                properties_to_save,
                log_lvl
            });
        }

        return processed; // Return processed data with mapped fields
    }

    return [];
}

export const qry__event_file = search__event_file;

export const properties_to_save = [
    'id',
    'event_file_id',
    'hosted_file_id',
    'hash_sha256',
    'for_type',
    'for_id',
    'event_id',
    'event_session_id',
    'event_presentation_id',
    'event_presenter_id',
    'event_location_id',
    'filename',
    'extension',
    'open_in_os',
    'lu_file_purpose_id',
    'lu_event_file_purpose_name',
    'file_purpose',
    'enable',
    'hide',
    'priority',
    'sort',
    'group',
    'notes',
    'created_on',
    'updated_on',
    'tmp_sort_1',
    'tmp_sort_2',
    'filename_no_ext',
    'filename_w_ext',
    // 'hosted_file_hash_sha256', // DO NOT UNCOMMENT
    // 'hosted_file_content_type', // DO NOT UNCOMMENT
    'content_type',
    'file_size',
    // 'hosted_file_size', // DO NOT UNCOMMENT
    // 'hosted_file_subdirectory_path', // DO NOT UNCOMMENT
    // 'subdirectory_path', // DO NOT UNCOMMENT
    'event_location_code',
    'event_location_name',
    'event_session_code',
    'event_session_type_code',
    'event_session_name',
    'event_session_start_datetime',
    'event_session_end_datetime',
    'event_presentation_code',
    'event_presentation_type_code',
    'event_presentation_name',
    'event_presentation_start_datetime',
    'event_presentation_end_datetime',
    'event_presenter_given_name',
    'event_presenter_family_name',
    'event_presenter_full_name',
    'event_presenter_email'
];

async function _process_generic_props<T extends Record<string, any>>({
    obj_li,
    obj_type,
    log_lvl = 0,
    specific_processor
}: {
    obj_li: T[];
    obj_type: string;
    log_lvl?: number;
    specific_processor?: (obj: T) => Promise<T> | T;
}): Promise<T[]> {
    if (!obj_li || obj_li.length === 0) return [];
    const processed_obj_li: T[] = [];
    for (const original_obj of obj_li) {
        let processed_obj = { ...original_obj };
        const base_id_key = `${obj_type}_id`;
        if (processed_obj[base_id_key])
            (processed_obj as any).id = processed_obj[base_id_key];
        const group = processed_obj.group ?? '0';
        const priority = processed_obj.priority ? 1 : 0;
        const sort = processed_obj.sort ?? '0';
        const updated = processed_obj.updated_on ?? processed_obj.created_on;
        const name = processed_obj.name ?? '';
        (processed_obj as any).tmp_sort_1 =
            `${group}_${priority}_${sort}_${updated}`;
        (processed_obj as any).tmp_sort_2 =
            `${group}_${priority}_${sort}_${name}_${updated}`;
        if (specific_processor)
            processed_obj = await Promise.resolve(
                specific_processor(processed_obj)
            );
        processed_obj_li.push(processed_obj as T);
    }
    return processed_obj_li;
}

export async function process_ae_obj__event_file_props({
    obj_li,
    log_lvl = 0
}: {
    obj_li: any[];
    log_lvl?: number;
}) {
    return _process_generic_props({
        obj_li,
        obj_type: 'event_file',
        log_lvl,
        specific_processor: (obj) => {
            // SYNC: Map prefixed Hosted File fields to flat names for UI consistency
            if (obj.hosted_file_hash_sha256 && !obj.hash_sha256) {
                obj.hash_sha256 = obj.hosted_file_hash_sha256;
            }
            if (obj.hosted_file_size && !obj.file_size) {
                obj.file_size = obj.hosted_file_size;
            }
            if (obj.hosted_file_content_type && !obj.content_type) {
                obj.content_type = obj.hosted_file_content_type;
            }
            // The subdirectory is not needed by the frontend. Do not uncomment!
            // if (obj.hosted_file_subdirectory_path && !obj.subdirectory_path) {
            //     obj.subdirectory_path = obj.hosted_file_subdirectory_path;
            // }

            // SYNC: Ensure for_id matches the specific object ID it was linked to
            if (obj.for_type && !obj.for_id) {
                const specific_id_key = `${obj.for_type}_id`;
                if (obj[specific_id_key]) {
                    obj.for_id = obj[specific_id_key];
                }
            }
            // SYNC: Ensure specific object ID is populated if for_id is present
            if (obj.for_type && obj.for_id) {
                const specific_id_key = `${obj.for_type}_id`;
                if (!obj[specific_id_key]) {
                    obj[specific_id_key] = obj.for_id;
                }
            }
            return obj;
        }
    });
}
