/** @type {import('./$types').LayoutLoad} */
// console.log(`ae_root +layout.ts: start`);

import { error } from '@sveltejs/kit';

// import { api } from '$lib/api';
// import { ae_loc, ae_sess, ae_api, slct, slct_trigger } from '$lib/ae_stores';
import { add_url_params, clean_headers } from '$lib/ae_core/core__api_helpers';
import type { key_val } from '$lib/stores/ae_stores';

import {
	PUBLIC_AE_API_PROTOCOL,
	PUBLIC_AE_API_SERVER,
	PUBLIC_AE_API_BAK_SERVER,
	PUBLIC_AE_API_PORT,
	PUBLIC_AE_API_PATH,
	PUBLIC_AE_API_SECRET_KEY,
	PUBLIC_AE_API_CRUD_SUPER_KEY,
	PUBLIC_AE_NO_ACCOUNT_ID,
	PUBLIC_AE_NO_ACCOUNT_ID_TOKEN
} from '$env/static/public';

const api_base_url = `${PUBLIC_AE_API_PROTOCOL}://${PUBLIC_AE_API_SERVER}:${PUBLIC_AE_API_PORT}${PUBLIC_AE_API_PATH}`;
const api_base_url_bak = `${PUBLIC_AE_API_PROTOCOL}://${PUBLIC_AE_API_BAK_SERVER}:${PUBLIC_AE_API_PORT}${PUBLIC_AE_API_PATH}`;

const api_secret_key = PUBLIC_AE_API_SECRET_KEY;
const api_crud_super_key = PUBLIC_AE_API_CRUD_SUPER_KEY;

const ae_account_id: null | string = null;
const ae_no_account_id = PUBLIC_AE_NO_ACCOUNT_ID;
const ae_no_account_id_token = PUBLIC_AE_NO_ACCOUNT_ID_TOKEN;

const ae_api_init: key_val = {
	ver: '2024-08-11_11',
	base_url: api_base_url,
	base_url_bak: api_base_url_bak,
	api_secret_key: api_secret_key, // 'YOUR_API_SECRET_KEY',
	api_secret_key_bak: api_secret_key, // 'YOUR_API_SECRET_KEY',
	api_crud_super_key: api_crud_super_key, // 'YOUR_SUPER_KEY' 'zp5PtX4zUsI'
	headers: {},
	account_id: ae_account_id
};

const ae_api_headers: key_val = {};
ae_api_headers['Access-Control-Allow-Origin'] = '*';
ae_api_headers['Content-Type'] = 'application/json';
ae_api_headers['x-aether-api-key'] = ae_api_init.api_secret_key;
ae_api_headers['x-aether-api-token'] = 'fake-temp-token';
ae_api_headers['x-aether-api-expire-on'] = '';
if (ae_account_id) {
	ae_api_headers['x-account-id'] = ae_account_id;
} else {
	// ae_api_headers['x-account-id'] = ;
}
if (ae_no_account_id) {
	ae_api_headers['x-no-account-id'] = ae_no_account_id;
}

ae_api_init['headers'] = ae_api_headers;

const ae_loc_init: key_val = {};

// export const prerender = false;

// There is not an initial data store from SvelteKit for this, so we will just use the API to get the data.
// properties: params, route, url
// functions: fetch, setHeaders, parent, depends, untrack
export async function load({ fetch, params, parent, route, url }) {
	// params, route, url
	const log_lvl: number = 0;
	// console.log(`Svelte root +layout.ts params:`, params);
	// console.log(`Svelte root +layout.ts route:`, route);
	// console.log(`Svelte root +layout.ts url:`, url);

	let account_id: any; // Promise<any>;

	const ae_acct: key_val = {
		api: ae_api_init,
		ds: {},
		loc: {
			account_id: '',
			site_id: '',
			site_domain_id: '',
			iframe: false
		},
		sess: {},
		slct: {}
	};

	// let ae_loc_tmp = get(ae_loc);
	// console.log(`ae_loc = `, ae_loc_tmp);

	// let ae_api_tmp = get(ae_api);
	// console.log(`ae_api = `, ae_api_tmp);

	const ds_code_li: null | key_val = {};
	// if (ae_loc_tmp && ae_loc_tmp.ds) {
	//     ds_code_li = ae_loc_tmp.ds;
	// }
	// console.log(`ae_ ds_code_li = `, ds_code_li);
	const ds_code: null | string = null;

	const data_struct: key_val = {
		// ae_acct should only be updated and referenced by the corresponding account_id.
		account_id: null,
		ae_acct: {
			// '_XY7DXtc9MY': ae_acct,
		},

		ae_loc: {},
		ae_api: ae_api_init,
		ae_ds: {},
		ae_hub: {}, // was ae_core
		ae_m_sponsorships: {},
		ae_m_events: {},
		ae_m_events_speakers: {},
		ae_slct: {},

		iframe: false,

		ae_root_layout_ts: true,

		params: params,
		route: route,
		url: url,

		// Not really used yet
		sections: [
			{ slug: 'new', title: 'New Test' },
			{ slug: 'manage', title: 'Manage Test' },
			{ slug: 'test', title: 'Test Test' }
		],

		// Not really used yet
		submenu: {}
	};

	// let parent_data = await parent();
	// console.log(`root +layout.ts parent_data:`, parent_data);

	// First do a site_domain look up to check if it is valid and get the account_id.
	// ae_loc.url_host = data.url.host; // Use this to look up? sub.example.com:123
	// ae_loc.fqdn = url.host; // Use this to look up? sub.example.com:123
	// ae_loc.url_hostname = parent_data.url.hostname; // sub.example.com
	// ae_loc.url_origin = parent_data.url.origin; // Use this to look up? https://sub.example.com:123
	// ae_loc.site_domain = data.url.origin;
	// console.log(`ae_loc = `, ae_loc);

	const fqdn = url.host;
	const api_endpoint = `/crud/site/domain/${fqdn}`;

	const api_params: any = {
		use_alt_table: true,
		use_alt_base: true
	};

	const api_url_str = add_url_params({
		base_url: ae_api_init['base_url'],
		endpoint: api_endpoint,
		params: api_params,
		log_lvl: log_lvl
	});

	let headers: any = {
		'x-no-account-id': 'Nothing to See Here'
	};

	headers = clean_headers({
		headers: headers,
		log_lvl: log_lvl
	});

	const fetchOptions: RequestInit = {
		method: 'GET',
		headers: {
			...ae_api_init['headers'],
			...headers
		}
		// signal: controller.signal
	};

	const result = await fetch(api_url_str, fetchOptions)
		.then(async function (response) {
			if (response.ok) {
				const json_raw = await response.json();
				// console.log(`ALERT json_raw = `, json_raw);

				let json_data: any = null;

				if (json_raw.data) {
					json_data = json_raw.data;
					account_id = json_data.account_id_random;
					data_struct.account_id = json_data.account_id_random;
					ae_acct.account_id = json_data.account_id_random;

					ae_api_init['account_id'] = json_data.account_id_random;
					ae_api_init['fetch'] = fetch;
					// ae_api_init['fetch'] = null;
					ae_api_init['headers']['x-account-id'] = json_data.account_id_random;
					ae_api_init['headers']['x-no-account-id'] = null;

					ae_loc_init['account_id'] = json_data.account_id_random;
					ae_loc_init['account_code'] = json_data.account_code; // Useful for export file naming
					ae_loc_init['account_name'] = json_data.account_name; // Generally useful for display

					ae_loc_init['site_id'] = json_data.site_id_random;
					ae_loc_init['site_domain_id'] = json_data.site_domain_id_random;
					ae_loc_init['site_enable'] = json_data.enable;
					ae_loc_init['site_header_image_path'] = json_data.header_image_path;
					ae_loc_init['site_style_href'] = json_data.style_href;
					ae_loc_init['site_google_tracking_id'] = json_data.google_tracking_id;
					ae_loc_init['site_access_code_kv'] = json_data.access_code_kv_json;
					ae_loc_init['site_cfg_json'] = json_data.cfg_json;
					ae_loc_init['site_access_key'] = json_data.access_key; // This is the general site access key
					ae_loc_init['site_domain_access_key'] = json_data.site_domain_access_key; // This is specific to a (sub)domain.

					ae_loc_init['base_url'] = url.origin;
					ae_loc_init['hostname'] = url.hostname;

					if (!ae_loc_init['site_access_key'] && !ae_loc_init['site_domain_access_key']) {
						ae_loc_init['key_checked'] = true; // Se to true to allow access without a key.
						ae_loc_init['allow_access'] = true; // No access key is required here.
					} else {
						const access_key = url.searchParams.get('key'); // Technically this is the site's auth key.

						if (access_key) {
							if (log_lvl) {
								console.log(`root +layout.ts: access_key = ${access_key}`);
							}
							if (access_key == ae_loc_init['site_access_key']) {
								ae_loc_init['key_checked'] = ae_loc_init['site_access_key'];
								ae_loc_init['allow_access'] = ae_loc_init['site_access_key'];
							} else if (access_key == ae_loc_init['site_domain_access_key']) {
								ae_loc_init['key_checked'] = ae_loc_init['site_domain_access_key'];
								ae_loc_init['allow_access'] = ae_loc_init['site_domain_access_key'];
							} else {
								// The key changed and no longer matches the site or domain key.
								ae_loc_init['key_checked'] = true;
								ae_loc_init['allow_access'] = false;
							}
						} else {
							if (log_lvl > 1) {
								console.log(`root +layout.ts: No access key found in URL.`);
							}
							ae_loc_init['key_checked'] = true;
							// Do nothing to change the allow_access here
							// ae_loc_init['allow_access'] = false; // An access key is required at this point.
						}
					}
				} else {
					console.log(
						'API GET Object *fetch* response was missing the JSON "data" part. This should not happen.'
					);
					return false;
				}
				return json_data; // || json_raw;
			} else if (response.status === 404) {
				console.log('API GET Object *fetch* response was a 404 not found.');
				return null;
			} else {
				console.log('API GET Object *fetch* request was not ok for some other reason.');
				return false;
			}
		})
		.catch((err) => {
			console.log(
				'API GET Object *fetch* request was aborted or failed in an unexpected way.',
				err
			);
			error(500, {
				message:
					'Site lookup aborted or failed! Check the network connection, domain name, and API.'
			});
		});
	if (log_lvl > 1) {
		console.log(`ALERT response = `, result);
	}

	if (result === null) {
		error(403, {
			message: 'The site lookup failed! Check that the domain name is configured and enabled.'
		});
	} else if (result === false) {
		error(500, {
			message: 'Site lookup failed or returned an unexpected response! Check the API.'
		});
	}

	// log_lvl = 0;
	if (log_lvl > 1) {
		console.log(`root +layout.ts: Using account_id = `, account_id);
		console.log(`root +layout.ts: ae_loc_init = `, ae_loc_init);
	}

	if (!account_id) {
		error(500, {
			message: 'The account ID was not found! Check the API.'
		});
	}

	ae_acct['api'] = ae_api_init;
	ae_acct['loc'] = ae_loc_init;
	ae_acct['ds'] = ds_code_li;
	ae_acct['slct'] = {
		account_id: account_id,
		site_domain_id: ae_loc_init.site_domain_id,
		site_id: ae_loc_init.site_id,

		// For events_leads, events_badges, events_speakers
		event_id: ae_loc_init.site_cfg_json?.slct__event_id,
		event_badge_template_id: ae_loc_init.site_cfg_json?.slct__event_badge_template_id,

		// For sponsorships
		sponsorship_cfg_id: ae_loc_init.site_cfg_json?.slct__sponsorship_cfg_id
	};
	// console.log(`ae_acct = `, ae_acct);

	// WARNING: Using the Account ID is a precaution against shared data between other accounts (sites and sessions).
	data_struct[ae_loc_init.account_id] = ae_acct;

	return data_struct;
}
