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

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

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

/**
 * Archive - A collection of archival content
 * Related Files:
 * - src/lib/ae_archives/ae_archives__archive.ts (API)
 * - src/routes/idaa/(idaa)/archives/[archive_id]/+page.svelte (View)
 * - src/routes/idaa/(idaa)/archives/ae_idaa_comp__archive_obj_li.svelte (Search List View)
 */
export interface Archive {
    id: string;
    // id_random: string; // NO LONGER USE "_random"
    archive_id: string;
    // archive_id_random: string; // NO LONGER USE "_random"

    code?: null | string;

    account_id: string;
    // account_id_random: string; // NO LONGER USE "_random"

    // archive_type: string;

    // type: string;
    name: string;
    // summary?: null|string;
    description?: null | string;

    content_html?: null | string;
    content_json?: null | string;
    content_url?: null | string;
    content_url_text?: null | string;

    original_datetime?: Date;
    original_timezone?: null | string;
    original_location?: null | string;

    original_url?: null | string;
    original_url_text?: null | string;

    // meta_data?: null|string;
    // access_key?: null|string; /// Rename this to "passcode" if used later

    sort_by?: null | string;
    sort_by_desc?: 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;

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

    // Additional fields for convenience (database views)
    topic_id?: null | string;
    topic_name?: null | string;
    archive_content_count?: number;
    // archive_content_kv?: null|key_val;
    // archive_content_li?: null|[];
}

/**
 * Archive_Content - A single piece of content within an archive
 * Related Files:
 * - src/lib/ae_archives/ae_archives__archive_content.ts (API)
 * - src/routes/idaa/(idaa)/archives/[archive_id]/ae_idaa_comp__archive_content_obj_li.svelte (List View)
 * - src/routes/idaa/(idaa)/archives/[archive_id]/ae_idaa_comp__archive_content_obj_id_edit.svelte (Edit View)
 */
export interface Archive_Content {
    id: string;
    // id_random: string; // NO LONGER USE "_random"
    archive_content_id: string;
    // archive_content_id_random: string; // NO LONGER USE "_random"

    archive_id: string;
    // archive_id_random: string; // NO LONGER USE "_random"

    archive_content_type: string;

    name: string;
    code?: null | string;
    description?: null | string;

    content_html?: null | string;
    content_json?: null | string;

    // linked_li_json?: null|string; // For future use? linked content list instead of one content item

    url?: null | string;
    url_text?: null | string;

    hosted_file_id?: string;

    file_path?: null | string;

    filename?: null | string;
    file_extension?: null | string;

    original_datetime?: Date;
    original_timezone?: null | string;
    original_location?: null | string;
    original_url?: null | string;
    original_url_text?: null | string;

    // meta_data?: null|string;
    // access_key?: null|string; /// Rename this to "passcode" if used later

    enable_for_public?: boolean;

    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;

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

    // Additional fields for convenience (database views)
    archive_code?: null | string;
    archive_name?: null | string;

    hash_sha256?: null | string;
}

// Updated 2024-09-25
export class MySubClassedDexie extends Dexie {
    // We just tell the typing system this is the case
    archive!: Table<Archive>;
    content!: Table<Archive_Content>;

    constructor() {
        super('ae_archives_db');
        this.version(1).stores({
            archive: `
                id, archive_id,
                code,
                account_id,
                name,
                original_datetime, original_timezone, original_location,
                tmp_sort_1, tmp_sort_2,
                enable, hide, priority, sort, group, notes, created_on, updated_on`,
            content: `
                id, archive_content_id,
                archive_id,
                archive_content_type,
                name,
                hosted_file_id,
                original_datetime, original_timezone, original_location,
                [group+original_datetime],
                tmp_sort_1, tmp_sort_2,
                enable, hide, priority, sort, group, notes, created_on, updated_on, [group+priority+sort+updated_on]`
        });
        // v2: add code index to content table
        this.version(2).stores({
            archive: `
                id, archive_id,
                code,
                account_id,
                name,
                original_datetime, original_timezone, original_location,
                tmp_sort_1, tmp_sort_2,
                enable, hide, priority, sort, group, notes, created_on, updated_on`,
            content: `
                id, archive_content_id,
                archive_id,
                archive_content_type,
                name,
                code,
                hosted_file_id,
                original_datetime, original_timezone, original_location,
                [group+original_datetime],
                tmp_sort_1, tmp_sort_2,
                enable, hide, priority, sort, group, notes, created_on, updated_on, [group+priority+sort+updated_on]`
        });
    }
}

export const db_archives = new MySubClassedDexie();
