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

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

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

/**
 * Post - A bulletin board post
 * Related Files:
 * - src/lib/ae_posts/ae_posts__post.ts (API)
 * - src/routes/idaa/(idaa)/bb/[post_id]/+page.svelte (View)
 * - src/routes/idaa/(idaa)/bb/ae_idaa_comp__post_obj_li.svelte (Search List)
 */
export interface Post {
    id: string;
    post_id: string;

    account_id: string;

    person_id?: null | string;
    external_person_id?: null | string; // For IDAA this is the Novi UUID
    user_id?: null | string;

    topic_id?: number;
    topic?: string; // or topic_name?
    topic_name?: string;

    name: null | string;
    title: null | string;
    // summary?: null|string;
    content?: null | string;

    anonymous?: null | boolean;
    full_name?: null | string;
    email?: null | string;
    notify?: null | boolean;

    enable_comments?: null | boolean;

    archive?: null | boolean;
    archive_on?: null | Date;

    linked_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;

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

    // Additional fields for convenience (database views)
    post_comment_count?: number;

    // Placeholder for generated temp data
    hosted_file_id_li?: null | Array<string>;
    hosted_file_obj_li?: null | Array<any>;
    upload_complete?: boolean;
}

/**
 * Post_Comment - A comment on a bulletin board post
 * Related Files:
 * - src/lib/ae_posts/ae_posts__post_comment.ts (API)
 * - src/routes/idaa/(idaa)/bb/[post_id]/ae_idaa_comp__post_comment_obj_li.svelte (Comment List View)
 * - src/routes/idaa/(idaa)/bb/ae_idaa_comp__post_comment_obj_id_edit.svelte (Edit View)
 */
export interface Post_Comment {
    id: string;
    post_comment_id: string;

    post_id: string;

    external_person_id?: null | string; // For IDAA this is the Novi UUID

    name: null | string;
    title: null | string;
    // summary?: null|string;
    content?: null | string;

    anonymous?: null | boolean;
    full_name?: null | string;
    email?: null | string;
    notify?: null | boolean;

    linked_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;

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

    // Additional fields for convenience (database views)
    account_id: string;
}

// Updated 2026-02-05
export class MySubClassedDexie extends Dexie {
    // We just tell the typing system this is the case
    post!: Table<Post>;
    comment!: Table<Post_Comment>;

    constructor() {
        super('ae_posts_db');
        this.version(2).stores({
            post: `
                id, post_id,
                account_id,
                topic_id, topic,
                name,
                title,
                full_name, email,
                archive, archive_on,
                tmp_sort_1, tmp_sort_2,
                enable, hide, priority, sort, group, notes, created_on, updated_on, [updated_on+created_on], [created_on+updated_on]`,

            comment: `
                id, post_comment_id,
                post_id,
                name,
                title,
                full_name, email,
                tmp_sort_1, tmp_sort_2,
                enable, hide, priority, sort, group, notes, created_on, updated_on, [updated_on+created_on]`
        });
    }
}

export const db_posts = new MySubClassedDexie();
