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

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

// Updated 2025-01-07
export interface File {
	id: string;
	id_random: string;
	hosted_file_id: string;
	hosted_file_id_random: string;

	hash_sha256: string;

	for_type?: string;
	for_id?: string;
	for_id_random?: string;

	account_id: string;

	filename: string;
	extension: string;
	content_type: string;
	size: number; // In bytes

	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)
	filename_no_ext: string;
	filename_w_ext: string;
}

// Updated 2024-07-17
export interface Person {
	id: string;
	// id_random: string;
	person_id: string;
	person_id_random: string;

	external_id?: string; // This may be semi-random or unique only withing the account.
	external_sys_id?: string; // Generated by an external system. Ideally this should be something like a UUID. It may be the same as the external_id if nothing given.
	code?: string; // Not currently used.

	account_id?: string; // Technically this is not required for global users.
	account_id_random?: string; // Technically this is not required for global users.

	person_profile_id?: null | string;
	person_profile_id_random?: null | string; // The new table person_profile will be used soon...

	user_id?: string;
	user_id_random?: string;

	pronouns?: null | string;
	informal_name?: null | string;
	title_names?: null | string;
	given_name: string;
	middle_name?: null | string;
	family_name: null | string;
	designations?: null | string;

	professional_title?: null | string;

	full_name?: string;
	full_name_override?: null | string; // was called display_name

	affiliations?: null | string;

	primary_email?: string;

	biography?: null | string;

	agree?: null | boolean;
	comments?: null | string;

	allow_auth_key?: null | boolean; // For sign in without password
	auth_key?: null | string; // Should this be saved locally?
	passcode?: null | string;

	data_json?: null | string;

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

	// Additional fields for convenience (database views)
	username?: string; // Same as user_username
	// user_username?: null|string; // Same as username
	user_name?: null | string;
	user_email?: null | string;
	user_allow_auth_key?: null | boolean; // For sign in without password
	user_super?: boolean;
	user_manager?: boolean;
	user_administrator?: boolean;
	user_public?: boolean;

	organization_id?: null | string; // The organization this person belongs to, if any.
	organization_id_random?: null | string; // The random ID of the organization this person belongs to, if any.
	organization_name?: null | string;

	contact_id?: null | string; // The contact ID of the person, if any.
	contact_id_random?: null | string; // The random ID of the contact, if any.
	contact_name?: null | string;
	contact_email?: null | string;
	contact_cc_email?: null | string;
	contact_phone_mobile?: null | string;
	contact_phone_home?: null | string;
	contact_phone_office?: null | string;
	contact_phone_landline?: null | string;
	contact_phone_fax?: null | string;
	contact_phone_other?: null | string;

	address_id?: null | string; // The address ID of the person, if any.
	address_id_random?: null | string; // The random ID of the address, if any.
	address_city?: null | string;
	address_country_alpha_2_code?: null | string; // ISO 3166-1 alpha-2 country code
}

// Updated 2025-01-07
export class MySubClassedDexie extends Dexie {
	file!: Table<File>;
	person!: Table<Person>;
	// user!: Table<User>;

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

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

export const db_core = new MySubClassedDexie();
