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

export interface CommissionRateResp {
  rateId: number;
  productId: number;
  productName?: string;
  commissionYear: number;
  ratePct: number;
  payMethodCond?: string;
  minPremium?: number;
  effectiveFrom?: string;
  effectiveTo?: string;
  version: number;
}

export interface CommissionRateSaveReq {
  productId: number;
  commissionYear: number;
  ratePct: number;
  payMethodCond?: string;
  minPremium?: number;
  effectiveFrom?: string;
  effectiveTo?: string;
}

export interface PayoutRuleResp {
  ruleId: number;
  gradeId: number;
  gradeName?: string;
  insuranceType: string;
  commissionYear: number;
  payoutPct: number;
  payMethodCond?: string;
  performanceGrade?: string;
  effectiveFrom?: string;
  effectiveTo?: string;
  version: number;
}

export interface PayoutRuleSaveReq {
  gradeId: number;
  insuranceType: string;
  commissionYear: number;
  payoutPct: number;
  payMethodCond?: string;
  performanceGrade?: string;
  effectiveFrom?: string;
  effectiveTo?: string;
}

export interface OverrideRuleResp {
  overrideId: number;
  fromGrade: number;
  toGrade: number;
  fromGradeName?: string;
  toGradeName?: string;
  overridePct: number;
  calcType?: string;
  insuranceTypeCond?: string;
  capAmount?: number;
  effectiveFrom?: string;
  effectiveTo?: string;
}

export interface OverrideRuleSaveReq {
  fromGrade: number;
  toGrade: number;
  overridePct: number;
  calcType?: string;
  insuranceTypeCond?: string;
  capAmount?: number;
  effectiveFrom?: string;
  effectiveTo?: string;
}

export interface ChargebackRuleResp {
  cbRuleId: number;
  companyCode: string;
  insuranceType: string;
  lapseMonthFrom: number;
  lapseMonthTo: number;
  cbRate: number;
  includeOverride: string;
  installmentAllowed: string;
  maxInstallments?: number;
  effectiveFrom?: string;
  effectiveTo?: string;
}

export interface ChargebackRuleSaveReq {
  companyCode: string;
  insuranceType: string;
  lapseMonthFrom: number;
  lapseMonthTo: number;
  cbRate: number;
  includeOverride: string;
  installmentAllowed: string;
  maxInstallments?: number;
  effectiveFrom?: string;
  effectiveTo?: string;
}

export interface ExceptionCodeResp {
  exceptionCode: string;
  exceptionName: string;
  category?: string;
  direction: string;
  autoCalcYn: string;
  approvalRequired: string;
  taxIncluded: string;
  description?: string;
  isActive: string;
}

export interface ExceptionCodeSaveReq {
  exceptionCode: string;
  exceptionName: string;
  category?: string;
  direction: string;
  autoCalcYn: string;
  approvalRequired: string;
  taxIncluded: string;
  description?: string;
  isActive: string;
}

export const ruleApi = {
  commissionRates: (params?: { productId?: number; year?: number }) =>
    unwrap<CommissionRateResp[]>(api.get('/api/rules/commission-rates', { params })),
  createCommissionRate: (req: CommissionRateSaveReq) =>
    unwrap<number>(api.post('/api/rules/commission-rates', req)),
  updateCommissionRate: (id: number, req: CommissionRateSaveReq) =>
    unwrap<void>(api.put(`/api/rules/commission-rates/${id}`, req)),
  removeCommissionRate: (id: number) =>
    unwrap<void>(api.delete(`/api/rules/commission-rates/${id}`)),

  payoutRules: (params?: { gradeId?: number; insuranceType?: string }) =>
    unwrap<PayoutRuleResp[]>(api.get('/api/rules/payout', { params })),
  createPayoutRule: (req: PayoutRuleSaveReq) =>
    unwrap<number>(api.post('/api/rules/payout', req)),
  updatePayoutRule: (id: number, req: PayoutRuleSaveReq) =>
    unwrap<void>(api.put(`/api/rules/payout/${id}`, req)),
  removePayoutRule: (id: number) =>
    unwrap<void>(api.delete(`/api/rules/payout/${id}`)),

  overrideRules: () => unwrap<OverrideRuleResp[]>(api.get('/api/rules/override')),
  createOverrideRule: (req: OverrideRuleSaveReq) =>
    unwrap<number>(api.post('/api/rules/override', req)),
  updateOverrideRule: (id: number, req: OverrideRuleSaveReq) =>
    unwrap<void>(api.put(`/api/rules/override/${id}`, req)),
  removeOverrideRule: (id: number) =>
    unwrap<void>(api.delete(`/api/rules/override/${id}`)),

  chargebackRules: (params?: { companyCode?: string }) =>
    unwrap<ChargebackRuleResp[]>(api.get('/api/rules/chargeback', { params })),
  createChargebackRule: (req: ChargebackRuleSaveReq) =>
    unwrap<number>(api.post('/api/rules/chargeback', req)),
  updateChargebackRule: (id: number, req: ChargebackRuleSaveReq) =>
    unwrap<void>(api.put(`/api/rules/chargeback/${id}`, req)),
  removeChargebackRule: (id: number) =>
    unwrap<void>(api.delete(`/api/rules/chargeback/${id}`)),

  exceptionCodes: () => unwrap<ExceptionCodeResp[]>(api.get('/api/rules/exception-codes')),
  createExceptionCode: (req: ExceptionCodeSaveReq) =>
    unwrap<void>(api.post('/api/rules/exception-codes', req)),
  updateExceptionCode: (code: string, req: ExceptionCodeSaveReq) =>
    unwrap<void>(api.put(`/api/rules/exception-codes/${code}`, req)),
};
