export interface Site_Domain {
    id: string;
    // id_random: string;
    site_id: string;
    site_id_random?: string;

    fqdn: string;
    access_key?: null | string;
    required_referrer?: null | string;
    valid_for?: null | number; // In hours

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

import { api } from '$lib/api/api';

/**
 * Fetches a site_domain object by its Fully Qualified Domain Name (FQDN).
 *
 * @param api_cfg - The API configuration object.
 * @param fqdn - The FQDN of the site domain to fetch.
 * @param timeout - The request timeout in milliseconds.
 * @param log_lvl - The logging level.
 * @returns The site domain object or null if not found.
 */
export async function load_ae_obj_by_fqdn__site_domain({
    api_cfg,
    fqdn,
    timeout = 7000,
    log_lvl = 0
}: {
    api_cfg: any;
    fqdn: string;
    timeout?: number;
    log_lvl?: number;
}): Promise<any> {
    if (log_lvl) {
        console.log(
            `*** load_ae_obj_by_fqdn__site_domain() *** api.base_url=${api_cfg.base_url}, fqdn=${fqdn}, timeout=${timeout}`
        );
    }

    const params = {};

    try {
        const site_domain_obj = await api.get_ae_obj_id_crud({
            api_cfg: api_cfg,
            no_account_id: true, // This seems to be a special case for this endpoint
            obj_type: 'site_domain',
            obj_id: fqdn, // NOTE: This is the FQDN, not the ID.
            use_alt_table: true,
            use_alt_base: true,
            params: params,
            timeout: timeout,
            log_lvl: log_lvl
        });

        if (site_domain_obj) {
            return site_domain_obj;
        } else {
            console.log('No results returned.');
            return null;
        }
    } catch (error) {
        console.log('No results returned or failed.', error);
        return null;
    }
}
