import type { PaginatedResponse } from '$lib/types';
import { api } from '$lib/api';

export interface ExchangeRate {
	id: number;
	date: string;
	value: number | null;
	local_currency: string | null;
	foreign_currency: string | null;
	company_id: number;
	tenant_id: number;
}

export interface ExchangeRateCreate {
	date: string;
	value?: number | null;
	local_currency?: string | null;
	foreign_currency?: string | null;
}

export interface ExchangeRateUpdate {
	date?: string;
	value?: number | null;
	local_currency?: string | null;
	foreign_currency?: string | null;
}

export interface ExchangeRateListResponse extends PaginatedResponse {
	items: ExchangeRate[];
}

export interface ExchangeRateFilters {
	date?: string;
	local_currency?: string;
	foreign_currency?: string;
	page?: number;
	page_size?: number;
}

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

export async function getExchangeRates(
	companyId: number,
	filters?: ExchangeRateFilters
): Promise<ApiResponse<ExchangeRateListResponse>> {
	const params = new URLSearchParams({ company_id: companyId.toString() });

	if (filters) {
		if (filters.date) params.append('date', filters.date);
		if (filters.local_currency) params.append('local_currency', filters.local_currency);
		if (filters.foreign_currency) params.append('foreign_currency', filters.foreign_currency);
		if (filters.page) params.append('page', filters.page.toString());
		if (filters.page_size) params.append('page_size', filters.page_size.toString());
	}

	return api.get<ExchangeRateListResponse>(`/v1/a76/exchange-rate/?${params.toString()}`);
}

export async function getExchangeRate(
	exchangeRateId: number,
	companyId: number
): Promise<ApiResponse<ExchangeRate>> {
	const params = new URLSearchParams({ company_id: companyId.toString() });
	return api.get<ExchangeRate>(`/v1/a76/exchange-rate/${exchangeRateId}?${params.toString()}`);
}

export async function createExchangeRate(
	data: ExchangeRateCreate,
	companyId: number
): Promise<ApiResponse<ExchangeRate>> {
	const params = new URLSearchParams({ company_id: companyId.toString() });
	return api.post<ExchangeRate>(`/v1/a76/exchange-rate/?${params.toString()}`, data);
}

export async function updateExchangeRate(
	exchangeRateId: number,
	data: ExchangeRateUpdate,
	companyId: number
): Promise<ApiResponse<ExchangeRate>> {
	const params = new URLSearchParams({ company_id: companyId.toString() });
	return api.put<ExchangeRate>(
		`/v1/a76/exchange-rate/${exchangeRateId}/?${params.toString()}`,
		data
	);
}

export async function deleteExchangeRate(
	exchangeRateId: number,
	companyId: number
): Promise<ApiResponse<void>> {
	const params = new URLSearchParams({ company_id: companyId.toString() });
	return api.delete(`/v1/a76/exchange-rate/${exchangeRateId}?${params.toString()}`);
}

export const exchangeRateApi = {
    list: (companyId: number, options: Record<string, any> = {}) => {
        const { page = 1, page_size = 50, ...filters } = options;
        return getExchangeRates(companyId, { ...filters, page: Number(page), page_size: Number(page_size) });
    },
    get: (id: number, companyId: number) => getExchangeRate(id, companyId),
    create: (data: ExchangeRateCreate, companyId: number) => createExchangeRate(data, companyId),
    update: (id: number, data: ExchangeRateUpdate, companyId: number) => updateExchangeRate(id, data, companyId),
    delete: (id: number, companyId: number) => deleteExchangeRate(id, companyId),
    getDof: (date: string) => getDofExchangeRate(date)
};

export interface DofResponse {
	success: boolean;
	message?: string;
	value?: number | null;
}

export async function getDofExchangeRate(date: string): Promise<ApiResponse<DofResponse>> {
	return api.get<DofResponse>(`/v1/a76/exchange-rate/dof-search?date=${date}`);
}
