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

interface GetAeObjV3Params {
    api_cfg: any;
    obj_type: string;
    obj_id: string;
    view?: string;
    params?: key_val;
    headers?: key_val;
    log_lvl?: number;
}

/**
 * Get a single object by ID (V3)
 */
export async function get_ae_obj({
    api_cfg,
    obj_type,
    obj_id,
    view = 'default',
    params = {},
    headers = {},
    log_lvl = 0
}: GetAeObjV3Params) {
    const endpoint = `/v3/crud/${obj_type}/${obj_id}`;
    const query_params: key_val = { view, ...params };

    if (log_lvl) {
        console.log('*** get_ae_obj ***');
        console.log('Endpoint:', endpoint);
        console.log('Params:', query_params);
    }

    return await get_object({
        api_cfg,
        endpoint,
        params: query_params,
        headers,
        log_lvl
    });
}

interface GetNestedAeObjV3Params {
    api_cfg: any;
    parent_type: string;
    parent_id: string;
    child_type: string;
    child_id: string;
    view?: string;
    params?: key_val;
    headers?: key_val;
    log_lvl?: number;
}

/**
 * Get a single nested object by ID (V3)
 */
export async function get_nested_ae_obj({
    api_cfg,
    parent_type,
    parent_id,
    child_type,
    child_id,
    view = 'default',
    params = {},
    headers = {},
    log_lvl = 0
}: GetNestedAeObjV3Params) {
    const endpoint = `/v3/crud/${parent_type}/${parent_id}/${child_type}/${child_id}`;
    const query_params: key_val = { view, ...params };

    if (log_lvl) {
        console.log('*** get_nested_ae_obj ***');
        console.log('Endpoint:', endpoint);
        console.log('Params:', query_params);
    }

    return await get_object({
        api_cfg,
        endpoint,
        params: query_params,
        headers,
        log_lvl
    });
}

interface GetAeObjLiV3Params {
    api_cfg: any;
    obj_type: string;
    for_obj_type?: string;
    for_obj_id?: string;
    enabled?: 'all' | 'enabled' | 'not_enabled';
    hidden?: 'all' | 'hidden' | 'not_hidden';
    view?: string;
    limit?: number;
    offset?: number;
    order_by_li?:
        | Record<string, 'ASC' | 'DESC'>
        | Record<string, 'ASC' | 'DESC'>[]
        | null;
    delay_ms?: number;
    params?: key_val;
    headers?: key_val;
    log_lvl?: number;
}

export async function get_ae_obj_li({
    api_cfg,
    obj_type,
    for_obj_type,
    for_obj_id,
    enabled = 'enabled',
    hidden = 'not_hidden',
    view = 'default',
    limit = 100,
    offset = 0,
    order_by_li = null,
    delay_ms = 0,
    params = {},
    headers = {},
    log_lvl = 0
}: GetAeObjLiV3Params) {
    // 1. Build V3 Endpoint
    const endpoint = `/v3/crud/${obj_type}/`;

    // 2. Build Query Params
    const query_params: key_val = {
        enabled,
        hidden,
        view,
        limit,
        offset,
        ...params
    };

    if (for_obj_type) query_params['for_obj_type'] = for_obj_type;
    if (for_obj_id) query_params['for_obj_id'] = for_obj_id;
    if (order_by_li) query_params['order_by_li'] = JSON.stringify(order_by_li);
    if (delay_ms > 0) query_params['delay_ms'] = delay_ms;

    if (log_lvl) {
        console.log('*** get_ae_obj_li ***');
        console.log('Endpoint:', endpoint);
        console.log('Params:', query_params);
        console.log('Headers:', headers);
    }

    return await get_object({
        api_cfg,
        endpoint,
        params: query_params,
        headers,
        log_lvl
    });
}

interface GetNestedObjLiV3Params {
    api_cfg: any;
    parent_type: string;
    parent_id: string;
    child_type: string;
    enabled?: 'all' | 'enabled' | 'not_enabled';
    hidden?: 'all' | 'hidden' | 'not_hidden';
    view?: string;
    limit?: number;
    offset?: number;
    order_by_li?:
        | Record<string, 'ASC' | 'DESC'>
        | Record<string, 'ASC' | 'DESC'>[]
        | null;
    delay_ms?: number;
    log_lvl?: number;
}

export async function get_nested_obj_li({
    api_cfg,
    parent_type,
    parent_id,
    child_type,
    enabled = 'enabled',
    hidden = 'not_hidden',
    view = 'default',
    limit = 100,
    offset = 0,
    order_by_li = null,
    delay_ms = 0,
    log_lvl = 0
}: GetNestedObjLiV3Params) {
    const endpoint = `/v3/crud/${parent_type}/${parent_id}/${child_type}/`;

    const params: key_val = {
        enabled,
        hidden,
        view,
        limit,
        offset
    };

    if (order_by_li) params['order_by_li'] = JSON.stringify(order_by_li);
    if (delay_ms > 0) params['delay_ms'] = delay_ms;

    if (log_lvl) {
        console.log('*** get_nested_obj_li ***');
        console.log('Endpoint:', endpoint);
        console.log('Params:', params);
    }

    return await get_object({
        api_cfg,
        endpoint,
        params,
        log_lvl
    });
}
