import { apiClient } from '@/lib/api-client';
import type {
  TrainingMatrixByIdResponse,
  TrainingMatrixComplianceResponse,
  TrainingMatrixFilters,
  TrainingMatrixMutationPayload,
  TrainingMatricesResponse,
  TrainingMatrixOptionsResponse
} from './types';

export async function getTrainingMatrices(
  filters: TrainingMatrixFilters
): Promise<TrainingMatricesResponse> {
  const searchParams = new URLSearchParams();

  if (filters.page) searchParams.set('page', String(filters.page));
  if (filters.limit) searchParams.set('limit', String(filters.limit));
  if (filters.search) searchParams.set('search', filters.search);
  if (filters.sort) searchParams.set('sort', filters.sort);

  return apiClient<TrainingMatricesResponse>(`/training-matrix?${searchParams.toString()}`);
}

export async function getTrainingMatrixById(id: number): Promise<TrainingMatrixByIdResponse> {
  return apiClient<TrainingMatrixByIdResponse>(`/training-matrix/${id}`);
}

export async function createTrainingMatrix(data: TrainingMatrixMutationPayload) {
  return apiClient<TrainingMatrixByIdResponse>('/training-matrix', {
    method: 'POST',
    body: JSON.stringify(data)
  });
}

export async function updateTrainingMatrix(
  id: number,
  data: TrainingMatrixMutationPayload
) {
  return apiClient<TrainingMatrixByIdResponse>(`/training-matrix/${id}`, {
    method: 'PATCH',
    body: JSON.stringify(data)
  });
}

export async function deleteTrainingMatrix(id: number) {
  return apiClient<{ success: boolean; message: string }>(`/training-matrix/${id}`, {
    method: 'DELETE'
  });
}

export async function getTrainingMatrixOptions(): Promise<TrainingMatrixOptionsResponse> {
  return apiClient<TrainingMatrixOptionsResponse>('/training-matrix/options');
}

export async function getTrainingMatrixCompliance(): Promise<TrainingMatrixComplianceResponse> {
  return apiClient<TrainingMatrixComplianceResponse>('/training-matrix/compliance');
}
