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

/** Matches SITAR OpenAPI TLCSResponse */
export interface SitarTLCS {
	FRACCION: string;
	PAIS: string;
	TASATXT?: string | null;
	TASA1NUM?: string | null;
	TASA2NUM?: string | null;
	DOF?: string | null;
	NOTAS?: string | null;
	NICO?: string;
	SYSID: number;
}

/** Matches SITAR OpenAPI ProsecResponse */
export interface SitarPROSEC {
	FRACCION?: string;
	ARTICULO?: string;
	SECTOR?: string;
	TASATXT?: string;
	TASANUM?: string | null;
	DOF?: string | null;
	OBSERVACION?: string | null;
	NICO?: string;
	SYSID: number;
}

export interface SitarALADI {
	FRACCION: string;
	ACUERDO: string;
	PAIS: string;
	TASATXT: string;
	TASANUM?: string | null;
	DOF: string;
	NOTAS: string | null;
	NICO?: string;
	SYSID: number;
}

export async function getSitarTLCS(filters: { fraccion: string; nico?: string }): Promise<ApiResponse<SitarTLCS[]>> {
	const queryParams = new URLSearchParams(
		Object.entries(filters).reduce(
			(acc, [key, value]) => {
				if (value !== undefined && value !== null && String(value).trim() !== '') {
					acc[key] = String(value);
				}
				return acc;
			},
			{} as Record<string, string>
		)
	);
	return await api.get(`/v1/sitar/tlcs/?${queryParams.toString()}`);
}

export async function getSitarPROSEC(filters: { fraccion: string; nico?: string }): Promise<ApiResponse<SitarPROSEC[]>> {
	const queryParams = new URLSearchParams(
		Object.entries(filters).reduce(
			(acc, [key, value]) => {
				if (value !== undefined && value !== null && String(value).trim() !== '') {
					acc[key] = String(value);
				}
				return acc;
			},
			{} as Record<string, string>
		)
	);
	return await api.get(`/v1/sitar/prosec/?${queryParams.toString()}`);
}

export async function getSitarALADI(filters: { fraccion: string; nico?: string }): Promise<ApiResponse<SitarALADI[]>> {
	const queryParams = new URLSearchParams(
		Object.entries(filters).reduce(
			(acc, [key, value]) => {
				if (value !== undefined && value !== null && String(value).trim() !== '') {
					acc[key] = String(value);
				}
				return acc;
			},
			{} as Record<string, string>
		)
	);
	return await api.get(`/v1/sitar/aladi2/?${queryParams.toString()}`);
}

export type SitarGenericRecord = Record<string, string | number | boolean | null>;

export type SitarDatasetEndpoint =
	| 'reit'
	| 'requisito-previo'
	| 'informacion-general'
	| 'regulaciones'
	| 'fundamentos-tlc'
	| 'cuotas2'
	| 'cupos'
	| 'noms'
	| 'precios-estimados'
	| 'ieps'
	| 'rcg2'
	| 'vehiculos-marcas'
	| 'vehiculos-modelos';

export async function getSitarDataset(
	endpoint: SitarDatasetEndpoint,
	filters: { fraccion: string; nico?: string; [key: string]: string | undefined }
): Promise<ApiResponse<SitarGenericRecord[]>> {
	const queryParams = new URLSearchParams(
		Object.entries(filters).reduce(
			(acc, [key, value]) => {
				if (value !== undefined && value !== null && String(value).trim() !== '') {
					acc[key] = String(value);
				}
				return acc;
			},
			{} as Record<string, string>
		)
	);
	return await api.get(`/v1/sitar/${endpoint}/?${queryParams.toString()}`);
}
