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

/** Contexto de uso: Fixed Asset (activo fijo) o inventario */
export type LocationSystem = 'fixed_asset' | 'inventory';

export interface Location {
	id: number;
	clave_localizacion: string;
	localizacion: string | null;
	system: LocationSystem;
}

export interface LocationCreate {
	clave_localizacion: string;
	localizacion?: string | null;
	system: LocationSystem;
	/** Used when system === 'fixed_asset' */
	department?: string | null;
	responsible?: string | null;
	observations?: string | null;
}

export interface LocationUpdate {
	clave_localizacion?: string;
	localizacion?: string | null;
	system?: LocationSystem;
}

export interface LocationListResponse {
	items: Location[];
	total: number;
	page: number;
	page_size: number;
}

export interface LocationFilters {
	clave_localizacion?: string;
	localizacion?: string;
	system?: LocationSystem;
	page?: number;
	page_size?: number;
}

const BASE_URL = '/v1/a76/locations';

function buildQuery(companyId: number, params?: LocationFilters): string {
	const search = new URLSearchParams();
	search.set('company_id', String(companyId));
	if (params?.page != null) search.set('page', String(params.page));
	if (params?.page_size != null) search.set('page_size', String(params.page_size));
	if (params?.clave_localizacion) search.set('clave_localizacion', params.clave_localizacion);
	if (params?.localizacion) search.set('localizacion', params.localizacion);
	if (params?.system) search.set('system', params.system);
	return search.toString();
}

export async function getLocations(
	companyId: number,
	filters?: LocationFilters
): Promise<LocationListResponse> {
	const q = buildQuery(companyId, filters);
	const res = await api.get<LocationListResponse>(`${BASE_URL}/?${q}`);
	return (res.data ?? res) as LocationListResponse;
}

export async function getLocation(
	locationId: number,
	companyId: number
): Promise<Location> {
	const q = new URLSearchParams({ company_id: String(companyId) });
	const res = await api.get<Location>(`${BASE_URL}/${locationId}?${q}`);
	return (res.data ?? res) as Location;
}

export async function createLocation(
	data: LocationCreate,
	companyId: number
): Promise<Location> {
	const q = new URLSearchParams({ company_id: String(companyId) });
	const res = await api.post<Location>(`${BASE_URL}/?${q}`, data);
	return (res.data ?? res) as Location;
}

export async function updateLocation(
	locationId: number,
	data: LocationUpdate,
	companyId: number
): Promise<Location> {
	const q = new URLSearchParams({ company_id: String(companyId) });
	const res = await api.put<Location>(`${BASE_URL}/${locationId}/?${q}`, data);
	return (res.data ?? res) as Location;
}

export async function deleteLocation(
	locationId: number,
	companyId: number
): Promise<void> {
	const q = new URLSearchParams({ company_id: String(companyId) });
	await api.delete(`${BASE_URL}/${locationId}?${q}`);
}

export const locationsApi = {
    list: (companyId: number, options: Record<string, any> = {}) => {
        const { page = 1, page_size = 50, ...filters } = options;
        return getLocations(companyId, { ...filters, page: Number(page), page_size: Number(page_size) });
    },
    get: (id: number, companyId: number) => getLocation(id, companyId),
    create: (data: LocationCreate, companyId: number) => createLocation(data, companyId),
    update: (id: number, data: LocationUpdate, companyId: number) => updateLocation(id, data, companyId),
    delete: (id: number, companyId: number) => deleteLocation(id, companyId)
};
