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

// ── Response VO ────────────────────────────────────────────────────────────────
export interface IncentiveProgramResp {
  programId: number;
  programName: string;
  programType: string;
  programTypeName?: string;
  targetType: string;
  targetGradeId?: number;
  targetGradeName?: string;
  targetOrgId?: number;
  targetOrgName?: string;
  productCategory?: string;
  startMonth: string;
  endMonth: string;
  conditionDesc?: string;
  payRate?: number;
  payAmountFixed?: number;
  payType: 'RATE' | 'FIXED_AMOUNT';
  budgetAmount?: number;
  isActive: boolean;
  createdAt: string;
}

// ── Save Request ───────────────────────────────────────────────────────────────
export interface IncentiveProgramSaveReq {
  programName: string;
  programType: string;
  targetType: 'AGENT' | 'ORG' | 'GRADE';
  targetGradeId?: number;
  targetOrgId?: number;
  productCategory?: string;
  startMonth: string;
  endMonth: string;
  conditionDesc?: string;
  payRate?: number;
  payAmountFixed?: number;
  payType: 'RATE' | 'FIXED_AMOUNT';
  budgetAmount?: number;
}

// ── Search Param ───────────────────────────────────────────────────────────────
export interface IncentiveProgramSearchParam {
  programType?: string;
  targetType?: string;
  startMonth?: string;
  endMonth?: string;
  pageNum?: number;
  pageSize?: number;
}

// ── API ────────────────────────────────────────────────────────────────────────
export const incentiveApi = {
  list: (p: IncentiveProgramSearchParam) =>
    unwrap<PageResponse<IncentiveProgramResp>>(api.get('/api/incentive-programs', { params: p })),

  get: (programId: number) =>
    unwrap<IncentiveProgramResp>(api.get(`/api/incentive-programs/${programId}`)),

  create: (body: IncentiveProgramSaveReq) =>
    unwrap<IncentiveProgramResp>(api.post('/api/incentive-programs', body)),

  update: (programId: number, body: IncentiveProgramSaveReq) =>
    unwrap<IncentiveProgramResp>(api.put(`/api/incentive-programs/${programId}`, body)),

  deactivate: (programId: number) =>
    unwrap<void>(api.put(`/api/incentive-programs/${programId}/deactivate`)),
};
