import Dexie, { type Table } from 'dexie';
import type {
    ae_HostedFile,
    ae_Person,
    ae_User,
    ae_Account,
    ae_Site,
    ae_SiteDomain,
    ae_Address,
    ae_Contact,
    ae_DataStore,
    ae_ActivityLog
} from '$lib/types/ae_types';

// li = list
// kv = key value list

// Updated 2026-01-09 - Unified Types
export interface ae_LocalFile extends ae_HostedFile {
    // Link properties (present in Dexie schema)
    for_type?: string;
    for_id?: string;
    for_id_random?: string;

    // Legacy mapping support
    content_type?: string;
    filename_no_ext?: string;
    filename_w_ext?: string;
}

/**
 * Person - A human entity in the Aether system
 * Related Files:
 * - src/lib/ae_core/ae_core_functions.ts (API)
 * - src/routes/core/people/[person_id]/+page.svelte (View)
 * - src/routes/core/people/ae_comp__person_search.svelte (Search)
 */
export interface Person extends ae_Person {
    // Legacy mapping support and view fields
    external_id?: string;
    external_sys_id?: string;
    person_profile_id?: string;
    person_profile_id_random?: string;

    username?: string;
    user_name?: string;
    user_email?: string;
    user_super?: boolean;
    user_manager?: boolean;
    user_administrator?: boolean;
    user_public?: boolean;

    organization_id?: string;
    organization_id_random?: string;
    organization_name?: string;

    contact_id?: string;
    contact_id_random?: string;
    contact_name?: string;

    address_id?: string;
    address_id_random?: string;
}

// Updated 2026-02-16 - Added Activity Log
export class MySubClassedDexie extends Dexie {
    file!: Table<ae_LocalFile>;
    person!: Table<Person>;
    user!: Table<ae_User>;
    account!: Table<ae_Account>;
    site!: Table<ae_Site>;
    site_domain!: Table<ae_SiteDomain>;
    address!: Table<ae_Address>;
    contact!: Table<ae_Contact>;
    data_store!: Table<ae_DataStore>;
    activity_log!: Table<ae_ActivityLog>;

    constructor() {
        super('ae_core_db');
        this.version(4).stores({
            file: `
                id, hosted_file_id, hosted_file_id_random,
                hash_sha256,
                account_id, account_id_random,
                for_type, for_id, for_id_random,
                filename, extension,
                enable, hide, priority, sort, group, created_on, updated_on`,

            person: `
                id, person_id, person_id_random,
                external_id, code,
                account_id, account_id_random,
                user_id, user_id_random,
                given_name, family_name,
                full_name, affiliations, email,
                enable, hide, priority, sort, group, created_on, updated_on`,

            user: `
                id, user_id, user_id_random,
                username, email,
                super, manager, administrator,
                enable, hide, priority, sort, group, created_on, updated_on`,

            account: `
                id, account_id, account_id_random,
                code, name,
                enable, hide, priority, sort, group, created_on, updated_on`,

            site: `
                id, site_id, site_id_random,
                account_id, account_id_random,
                code, name,
                enable, hide, priority, sort, group, created_on, updated_on`,

            site_domain: `
                id, site_domain_id, site_domain_id_random,
                site_id, site_id_random,
                fqdn,
                enable, hide, priority, sort, group, created_on, updated_on`,

            address: `
                id, address_id, address_id_random,
                account_id, account_id_random,
                for_type, for_id_random,
                city, state_province, country,
                enable, hide, priority, sort, group, created_on, updated_on`,

            contact: `
                id, contact_id, contact_id_random,
                account_id, account_id_random,
                for_type, for_id_random,
                title, email, phone_mobile,
                enable, hide, priority, sort, group, created_on, updated_on`,

            data_store: `
                id, data_store_id, data_store_id_random,
                account_id, account_id_random,
                for_type, for_id_random,
                code, name, type,
                [code+for_type+for_id_random],
                [code+account_id_random+for_type],
                [code+account_id_random],
                enable, hide, priority, sort, group, created_on, updated_on`,

            activity_log: `
                id, activity_log_id, activity_log_id_random,
                account_id, account_id_random,
                person_id_random, user_id_random,
                external_client_id, object_type, object_id_random,
                name, action,
                enable, hide, priority, sort, group, created_on, updated_on`
        });
    }
}

export const db_core = new MySubClassedDexie();
