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

export interface DodaAltaLog {
	id: number;
	doda_id?: number | null;
	variant?: string | null;
	responsible?: string | null;
	patent?: string | null;
	dispatch_customs?: string | null;
	operation_type?: string | null;
	integration_number?: string | null;
	task_id?: string | null;
	status?: string | null;
	message?: string | null;
	result_json?: string | null;
	company_id: number;
	tenant_id: number;
	created_at?: string | null;
	updated_at?: string | null;
}

export interface DodaAltaLogListResponse {
	items: DodaAltaLog[];
	total: number;
	page: number;
	page_size: number;
}

export interface DodaAltaLogCreateDTO {
	doda_id?: number | null;
	variant?: string | null;
	responsible?: string | null;
	patent?: string | null;
	dispatch_customs?: string | null;
	operation_type?: string | null;
	integration_number?: string | null;
	task_id?: string | null;
	status?: string | null;
	message?: string | null;
	result_json?: string | null;
}

export interface DodaAltaLogUpdateDTO {
	status?: string | null;
	message?: string | null;
	result_json?: string | null;
}

export const dodaAltaLogApi = {
	list(
		companyId: number,
		params?: {
			page?: number;
			page_size?: number;
			doda_id?: number;
			search?: string;
		}
	): Promise<ApiResponse<DodaAltaLogListResponse>> {
		const qs = new URLSearchParams({ company_id: companyId.toString() });
		if (params?.page) qs.set('page', params.page.toString());
		if (params?.page_size) qs.set('page_size', params.page_size.toString());
		if (params?.doda_id) qs.set('doda_id', params.doda_id.toString());
		if (params?.search) qs.set('search', params.search);
		return api.get<DodaAltaLogListResponse>(`/v1/a76/doda/alta-logs?${qs}`);
	},

	get(id: number, companyId: number): Promise<ApiResponse<DodaAltaLog>> {
		return api.get<DodaAltaLog>(`/v1/a76/doda/alta-logs/${id}?company_id=${companyId}`);
	},

	create(dto: DodaAltaLogCreateDTO, companyId: number): Promise<ApiResponse<DodaAltaLog>> {
		return api.post<DodaAltaLog>(`/v1/a76/doda/alta-logs?company_id=${companyId}`, dto);
	},

	update(
		id: number,
		dto: DodaAltaLogUpdateDTO,
		companyId: number
	): Promise<ApiResponse<DodaAltaLog>> {
		return api.put<DodaAltaLog>(`/v1/a76/doda/alta-logs/${id}?company_id=${companyId}`, dto);
	},

	delete(id: number, companyId: number): Promise<ApiResponse<void>> {
		return api.delete(`/v1/a76/doda/alta-logs/${id}?company_id=${companyId}`);
	}
};
