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

export interface AgentLicenseRow extends Record<string, unknown> {
  licenseId: number;
  agentId: number;
  agentName: string;
  licenseType: string;      // LIFE / NONLIFE / VARIABLE
  licenseTypeName: string;
  licenseNo: string;
  issuedAt: string;
  expiresAt: string;
  renewedAt?: string;
  status: string;
  daysUntilExpiry?: number;
}

export interface AgentLicenseSaveReq {
  agentId: number;
  licenseType: string;
  licenseNo: string;
  issuedAt: string;
  expiresAt: string;
}

export interface AgentLicenseSearchParam {
  agentId?: number;
  agentName?: string;
  licenseType?: string;
  status?: string;
  pageNum?: number;
  pageSize?: number;
}

export const licenseApi = {
  list: (p: AgentLicenseSearchParam) =>
    unwrap<PageResponse<AgentLicenseRow>>(
      api.get('/api/agent-licenses', { params: p }),
    ),
  expiring: (days = 30) =>
    unwrap<AgentLicenseRow[]>(
      api.get('/api/agent-licenses/expiring', { params: { days } }),
    ),
  create: (body: AgentLicenseSaveReq) =>
    unwrap<AgentLicenseRow>(api.post('/api/agent-licenses', body)),
  update: (id: number, body: Partial<AgentLicenseSaveReq>) =>
    unwrap<AgentLicenseRow>(api.put(`/api/agent-licenses/${id}`, body)),
  delete: (id: number) =>
    unwrap<void>(api.delete(`/api/agent-licenses/${id}`)),
};
