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

export interface AgentResp {
  agentId: number;
  agentName: string;
  licenseNo?: string;
  phone?: string;
  email?: string;
  status: string;
  statusName?: string;
  orgId?: number;
  orgName?: string;
  gradeId?: number;
  gradeName?: string;
  joinDate?: string;
  contractCount?: number;
  totalCommission?: number;
}

export interface AgentSearchParam {
  pageNum?: number;
  pageSize?: number;
  searchKeyword?: string;
  status?: string;
  orgId?: number;
  gradeId?: number;
}

export interface AgentSaveReq {
  agentName: string;
  orgId: number;
  gradeId: number;
  residentNo?: string;
  licenseNo?: string;
  phone?: string;
  email?: string;
  bankCode?: string;
  accountNo?: string;
  joinDate?: string;
}

export const agentApi = {
  list: (p: AgentSearchParam) =>
    unwrap<PageResponse<AgentResp>>(api.get('/api/agents', { params: p })),
  detail: (id: number) => unwrap<AgentResp>(api.get(`/api/agents/${id}`)),
  create: (req: AgentSaveReq) => unwrap<number>(api.post('/api/agents', req)),
  update: (id: number, req: AgentSaveReq) => unwrap<void>(api.put(`/api/agents/${id}`, req)),
  changeStatus: (id: number, status: string) =>
    unwrap<void>(api.put(`/api/agents/${id}/status`, { status })),
};
