import api, { PageResponse, unwrap } from './request';

export interface AgentTrainingRow extends Record<string, unknown> {
  trainingId: number;
  agentId: number;
  agentName: string;
  trainingName: string;
  trainingType: string;
  completedHours: number;
  trainingDate: string;
  provider: string;
  status: string;
  year: number;
}

export interface TrainingRequirement extends Record<string, unknown> {
  requirementId: number;
  trainingType: string;
  requiredHours: number;
  year: number;
}

export interface AnnualHoursSummary extends Record<string, unknown> {
  agentId: number;
  agentName: string;
  year: number;
  completedHours: number;
  requiredHours: number;
  satisfactionRate: number;
  satisfied: boolean;
}

export interface AgentTrainingSaveReq {
  agentId: number;
  trainingName: string;
  trainingType: string;
  completedHours: number;
  trainingDate: string;
  provider: string;
}

export interface AgentTrainingSearchParam {
  agentId?: number;
  agentName?: string;
  year?: number;
  trainingType?: string;
  pageNum?: number;
  pageSize?: number;
}

export const trainingApi = {
  list: (p: AgentTrainingSearchParam) =>
    unwrap<PageResponse<AgentTrainingRow>>(
      api.get('/api/agent-trainings', { params: p }),
    ),
  annualHours: (agentId: number, year: number) =>
    unwrap<AnnualHoursSummary>(
      api.get('/api/agent-trainings/annual-hours', { params: { agentId, year } }),
    ),
  requirements: () =>
    unwrap<TrainingRequirement[]>(
      api.get('/api/training-requirements'),
    ),
  create: (body: AgentTrainingSaveReq) =>
    unwrap<AgentTrainingRow>(api.post('/api/agent-trainings', body)),
  update: (id: number, body: Partial<AgentTrainingSaveReq>) =>
    unwrap<AgentTrainingRow>(api.put(`/api/agent-trainings/${id}`, body)),
  delete: (id: number) =>
    unwrap<void>(api.delete(`/api/agent-trainings/${id}`)),
};
