import Dexie, { type Table } from 'dexie';

/**
 * Lookup DB — IDB-backed cache for V3 Uniform Lookup System reference data.
 *
 * These tables store the deduplicated, priority-ranked list returned by
 * GET /v3/lookup/{lu_type}/list. Data is refreshed automatically on a 24-hour
 * TTL via the core__*.ts load helpers; components subscribe via liveQuery.
 *
 * Updated 2026-03-23
 */

export interface LuCountry {
    id: number;
    group: string; // dedup key = alpha_2_code (e.g. "US")
    alpha_2_code: string;
    name: string;
    english_short_name?: string;
    name_override?: string;
    enable?: number;
    hide?: number;
    priority?: number;
    sort?: number;
    account_id?: number | null;
    [key: string]: unknown; // allow extra fields from API without TS errors
}

export interface LuCountrySubdivision {
    id: number;
    group: string; // dedup key = code (e.g. "US-NY")
    code: string;
    name: string;
    country_alpha_2_code?: string;
    name_override?: string;
    enable?: number;
    hide?: number;
    priority?: number;
    sort?: number;
    account_id?: number | null;
    [key: string]: unknown;
}

export interface LuTimeZone {
    id: number;
    group: string; // dedup key = name (IANA identifier, e.g. "US/Eastern")
    name: string;
    name_override?: string; // display label override; prefer this over name when set
    enable?: number;
    hide?: number;
    priority?: number;
    sort?: number;
    account_id?: number | null;
    [key: string]: unknown;
}

export interface LuCacheMeta {
    lu_type: 'country' | 'country_subdivision' | 'time_zone';
    refreshed_at: number; // Unix timestamp ms — used for 24h TTL check
}

class LookupsDexie extends Dexie {
    lu_country!: Table<LuCountry>;
    lu_country_subdivision!: Table<LuCountrySubdivision>;
    lu_time_zone!: Table<LuTimeZone>;
    lu_cache_meta!: Table<LuCacheMeta>;

    constructor() {
        super('ae_lookups_db');
        this.version(1).stores({
            lu_country: 'id, alpha_2_code, group',
            lu_country_subdivision: 'id, code, country_alpha_2_code, group',
            lu_time_zone: 'id, name, group',
            lu_cache_meta: 'lu_type'
        });
    }
}

export const db_lookups = new LookupsDexie();

/** 24-hour TTL in milliseconds */
export const LOOKUP_TTL_MS = 24 * 60 * 60 * 1000;
