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

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

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

// Updated 2025-01-15
export interface Sponsorship {
    id: string;
    // id_random: string;
    sponsorship_id: string;
    // sponsorship_id_random: string;

    account_id: string;
    // account_id_random: string;

    organization_id?: null | string;
    person_id?: null | string;

    poc_person_id?: null | string;
    poc_json?: null | string;

    name: null | string;
    name_override: null | string;

    description?: null | string;

    email?: null | string;
    website_url?: null | string;

    // html_text?: null|string;
    // thumbnail_url?: null|string;
    // picture_url?: null|string;
    // video_url?: null|string;
    // audio_url?: null|string;
    // image_url?: null|string;
    // document_url?: null|string;
    // logo_url?: null|string;

    logo_li_json?: null | string;
    media_li_json?: null | string;
    social_li_json?: null | string;
    address_li_json?: null | string;
    contact_li_json?: null | string;
    guest_li_json?: null | string;

    level_num?: null | number;
    level_str?: null | string;

    amount?: null | number; // In dollars

    questions_li_json?: null | string;

    agree?: null | boolean; // Catchall agree or consent

    comments?: null | string; // From the sponsor
    staff_notes?: null | string; // Internal use; from staff

    enable: null | boolean;
    hide?: null | boolean;
    priority?: null | boolean;
    sort?: null | number;
    group?: null | string;
    notes?: null | string;
    created_on: Date;
    updated_on?: null | Date;

    // Generated fields for sorting locally only
    tmp_sort_1?: null | string;
    tmp_sort_2?: null | string;

    // Additional fields for convenience (database views)
}

// Updated 2025-01-15
export interface Sponsorship_Cfg {
    id: string;
    // id_random: string;
    sponsorship_cfg_id: string;
    // sponsorship_cfg_id_random: string;

    account_id: string;
    // account_id_random: string;

    for_type?: null | string;
    for_id?: null | number;

    level_li_json?: null | string;
    option_li_json?: null | string;
    schedule_li_json?: null | string;

    cfg_json?: null | key_val;

    enable: null | boolean;
    hide?: null | boolean;
    priority?: null | boolean;
    sort?: null | number;
    group?: null | string;
    notes?: null | string;
    created_on: Date;
    updated_on?: null | Date;

    // Additional fields for convenience (database views)
}

// Updated 2024-09-25
export class MySubClassedDexie extends Dexie {
    // We just tell the typing system this is the case
    sponsorship!: Table<Sponsorship>;
    cfg!: Table<Sponsorship_Cfg>;

    constructor() {
        super('ae_sponsorships_db');
        this.version(1).stores({
            sponsorship: `
                id, sponsorship_id,
                account_id,
                poc_person_id,
                name, name_override,
                level_num, level_str, amount,
                agree,
                enable, hide, priority, sort, group, notes, created_on, updated_on, [updated_on+created_on], [created_on+updated_on]`,

            cfg: `
                id, sponsorship_cfg_id,
                account_id,
                for_type, for_id`
        });
    }
}

export const db_sponsorships = new MySubClassedDexie();
