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

import { db_core } from '$lib/ae_core/db_core';

const ae_promises: key_val = {};

// Updated 2024-06-14
export async function load_ae_obj_id__hosted_file({
	api_cfg,
	hosted_file_id,
	try_cache = false,
	log_lvl = 0
}: {
	api_cfg: any;
	hosted_file_id: string;
	try_cache?: boolean;
	log_lvl?: number;
}) {
	if (log_lvl) {
		console.log(`*** load_ae_obj_id__hosted_file() *** hosted_file_id=${hosted_file_id}`);
	}

	const params = {};

	ae_promises.load__hosted_file_obj = await api
		.get_ae_obj_id_crud({
			api_cfg: api_cfg,
			obj_type: 'hosted_file',
			obj_id: hosted_file_id, // NOTE: This is the FQDN, not normally the ID.
			use_alt_table: false, // NOTE: This will use the table_name_alt value instead of the table_name value in the API config.
			use_alt_base: false, // NOTE: This will use the base_name_alt value instead of the base_name value
			params: params,
			log_lvl: log_lvl
		})
		.then(function (hosted_file_obj_get_result) {
			if (hosted_file_obj_get_result) {
				// This is expecting a list
				db_save_ae_obj_li__hosted_file({
					obj_type: 'hosted_file',
					obj_li: [hosted_file_obj_get_result]
				});
				return hosted_file_obj_get_result;
			} else {
				console.log('No results returned.');
				return null;
			}
		})
		.catch(function (error: any) {
			console.log('No results returned or failed.', error);
		});

	return ae_promises.load__hosted_file_obj;
}

// Updated 2024-07-03
export async function load_ae_obj_li__hosted_file({
	api_cfg,
	for_obj_type,
	for_obj_id,
	enabled = 'enabled',
	hidden = 'not_hidden',
	limit = 99,
	offset = 0,
	order_by_li = { priority: 'DESC', sort: 'DESC', updated_on: 'DESC', created_on: 'DESC' },
	params = {},
	try_cache = true,
	log_lvl = 0
}: {
	api_cfg: any;
	for_obj_type: string;
	for_obj_id: string;
	enabled?: 'enabled' | 'all' | 'not_enabled' | undefined;
	hidden?: 'hidden' | 'all' | 'not_hidden' | undefined;
	limit?: number;
	offset?: number;
	order_by_li?: key_val;
	params?: key_val;
	try_cache?: boolean;
	log_lvl?: number;
}) {
	if (log_lvl) {
		console.log(
			`*** load_ae_obj_li__hosted_file() *** for_obj_type=${for_obj_type} for_obj_id=${for_obj_id}`
		);
	}

	// Check if for_obj_type is in the list of valid Aether object types:
	const valid_for_obj_types = [
		'account',
		'archive',
		'archive_content',
		'event',
		'event_session',
		'event_presentation',
		'event_presenter',
		'event_location',
		'journal',
		'journal_entry',
		'post',
		'post_comment'
	];
	if (!valid_for_obj_types.includes(for_obj_type)) {
		console.log(`Invalid for_obj_type: ${for_obj_type}`);
		return [];
	}

	// let enabled: string = (params.qry__enabled ?? 'enabled'); // all, disabled, enabled
	// let hidden: string = (params.qry__hidden ?? 'all'); // all, hidden, not_hidden
	// let limit: number = (params.qry__limit ?? 99); // 99
	// let offset: number = (params.qry__offset ?? 0); // 0

	const params_json: key_val = {};

	// console.log('params_json:', params_json);

	ae_promises.load__hosted_file_obj_li = await api
		.get_ae_obj_li_for_obj_id_crud_v2({
			api_cfg: api_cfg,
			obj_type: 'hosted_file',
			for_obj_type: for_obj_type,
			for_obj_id: for_obj_id,
			use_alt_tbl: false,
			use_alt_mdl: false,
			use_alt_exp: false,
			enabled: enabled,
			hidden: hidden,
			order_by_li: order_by_li,
			limit: limit,
			offset: offset,
			params_json: params_json,
			params: params,
			log_lvl: log_lvl
		})
		.then(function (hosted_file_obj_li_get_result) {
			if (hosted_file_obj_li_get_result) {
				if (try_cache) {
					db_save_ae_obj_li__hosted_file({
						obj_type: 'hosted_file',
						obj_li: hosted_file_obj_li_get_result,
						log_lvl: log_lvl
					});
				}
				return hosted_file_obj_li_get_result;
			} else {
				console.log('No results returned.');
				return [];
			}
		})
		.catch(function (error: any) {
			console.log('No results returned or failed.', error);
		});

	if (log_lvl) {
		console.log('ae_promises.load__hosted_file_obj_li:', ae_promises.load__hosted_file_obj_li);
	}
	return ae_promises.load__hosted_file_obj_li;
}

// Updated 2024-11-07
export async function delete_ae_obj_id__hosted_file({
	api_cfg,
	hosted_file_id,
	link_to_type, // Ideally this should be required...
	link_to_id, // Ideally this should be required...
	rm_orphan = false,
	fake_delete = false, // Fake the delete result to "true"
	params = {},
	try_cache = true,
	log_lvl = 0
}: {
	api_cfg: any;
	hosted_file_id: string;
	link_to_type: string;
	link_to_id: string;
	rm_orphan?: boolean;
	fake_delete?: boolean;
	params?: key_val;
	try_cache?: boolean;
	log_lvl?: number;
}) {
	if (log_lvl) {
		console.log(`*** delete_ae_obj_id__hosted_file() *** hosted_file_id=${hosted_file_id}`);
	}

	const endpoint = `/hosted_file/${hosted_file_id}`;

	params['link_to_type'] = link_to_type;
	params['link_to_id'] = link_to_id;
	params['rm_orphan'] = rm_orphan; // This is what actually allows the hosted file to be deleted from the server.
	if (log_lvl) {
		console.log(`delete_ae_obj_id__hosted_file() params=`, params);
	}

	if (fake_delete) {
		console.log(`*** FAKE DELETE!!! ***`);
		ae_promises.delete__hosted_file_obj = true;
		return ae_promises.delete__hosted_file_obj;
	}

	ae_promises.delete__hosted_file_obj = await api
		.delete_object({
			api_cfg: api_cfg,
			endpoint: endpoint,
			params: params,
			// return_meta: return_meta,
			log_lvl: log_lvl
		})
		.then(function (hosted_file_obj_li_get_result) {
			if (hosted_file_obj_li_get_result) {
				if (try_cache) {
					if (log_lvl) {
						console.log(`Attempting to remove IDB entry for hosted_file_id=${hosted_file_id}`);
					}
					db_core.file.delete(hosted_file_id); // Delete from the DB no matter what.
				}
				return hosted_file_obj_li_get_result;
			} else {
				console.log('No results returned.');
				return [];
			}
		})
		.catch(function (error: any) {
			console.log('No results returned or failed.', error);
		});

	if (log_lvl) {
		console.log('ae_promises.delete__hosted_file_obj:', ae_promises.delete__hosted_file_obj);
	}

	return ae_promises.delete__hosted_file_obj;
}

// This function will loop through the hosted_file_obj_li and save each one to the DB.
// Updated 2025-01-07
export function db_save_ae_obj_li__hosted_file({
	obj_type,
	obj_li,
	log_lvl = 0
}: {
	obj_type: string;
	obj_li: any;
	log_lvl?: number;
}) {
	if (log_lvl) {
		console.log(`*** db_save_ae_obj_li__hosted_file() ***`);
	}

	if (obj_li && obj_li.length) {
		obj_li.forEach(async function (obj: any) {
			if (log_lvl) {
				console.log(`ae_obj ${obj_type}:`, obj);
			}

			try {
				const id_random = await db_core.file.put({
					id: obj.hosted_file_id_random,
					id_random: obj.hosted_file_id_random,
					hosted_file_id: obj.hosted_file_id_random,
					hosted_file_id_random: obj.hosted_file_id_random,

					hash_sha256: obj.hash_sha256, // Renamed with alias in FastAPI model

					for_type: obj.for_type,
					for_id: obj.for_id_id_random,
					for_id_random: obj.for_id_random,

					account_id: obj.account_id_random,

					filename: obj.filename,
					extension: obj.extension,
					content_type: obj.content_type,
					size: obj.size,

					enable: obj.enable,
					hide: obj.hide,
					// priority: obj.priority,
					// sort: obj.sort,
					group: obj.group,
					notes: obj.notes,
					created_on: obj.created_on,
					updated_on: obj.updated_on,

					filename_no_ext: obj.filename_no_ext,
					filename_w_ext: obj.filename_w_ext
				});
				// console.log(`Put obj with ID: ${obj.hosted_file_id_random} or ${id_random}`);
			} catch (error) {
				const status = `Failed to put ${obj.hosted_file_id_random}: ${error}`;
				console.log(status);
			}

			// const id_random = await db_core.file.put(obj);
			// console.log(`Put obj with ID: ${obj.hosted_file_id_random}`);
		});

		return true;
	}
	return false;
}

// Updated 2025-01-07
export function db_update_ae_obj_id__hosted_file({
	obj_type,
	obj_id,
	data_kv,
	log_lvl = 0
}: {
	obj_type: string;
	obj_id: string;
	data_kv: key_val;
	log_lvl?: number;
}) {
	if (log_lvl) {
		console.log(`*** db_update_ae_obj_id__hosted_file() ***`);
	}

	if (obj_id) {
		console.log(`ae_obj ${obj_type}:`, obj_id);

		try {
			// db_core.file.update(obj_id, data_kv);
			db_core.file.update(obj_id, {
				// for_type: data_kv.for_type,
				// for_id: data_kv.for_id_id_random,
				// for_id_random: data_kv.for_id_random,

				filename: data_kv.filename,
				extension: data_kv.extension,
				content_type: data_kv.content_type,
				size: data_kv.size,

				// enable: data_kv.enable,
				// hide: data_kv.hide,
				// priority: data_kv.priority,
				// sort: data_kv.sort,
				// group: data_kv.group,
				// notes: data_kv.notes,
				// created_on: data_kv.created_on,
				// updated_on: data_kv.updated_on,

				filename_no_ext: data_kv.filename_no_ext,
				filename_w_ext: data_kv.filename_w_ext
				// hosted_file_content_type: data_kv.hosted_file_content_type,
				// file_size: data_kv.file_size,
				// hosted_file_size: data_kv.hosted_file_size,
			});

			console.log(`Update obj with ID: ${obj_id}`);
		} catch (error) {
			const status = `Failed to update ${obj_id}: ${error}`;
			console.log(status);
		}

		// const id_random = await db_core.file.put(obj);
		// console.log(`Put obj with ID: ${data_kv.hosted_file_id_random}`);
		return true;
	}
	return false;
}
