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

// ── 공통 ──────────────────────────────────────────
export interface CommissionSearchParam {
  agentId?: number;
  agentName?: string;
  settleMonth?: string;   // YYYYMM
  commissionType?: string;
  pageNum?: number;
  pageSize?: number;
}

// ── CommissionMaster ──────────────────────────────
export interface CommissionMasterRow extends Record<string, unknown> {
  masterId: number;
  agentId: number;
  agentName: string;
  settleMonth: string;
  commissionType: string;
  commissionTypeName: string;
  grossAmount: number;
  netAmount: number;
  status: string;
}

// ── CollectionCommission ──────────────────────────
export interface CollectionRuleRow extends Record<string, unknown> {
  ruleId: number;
  carrierId: number;
  carrierName: string;
  productType: string;
  paymentCycle: string;
  ratePercent: number;
  effectiveFrom: string;
  effectiveTo?: string;
  status: string;
}

export interface CollectionLedgerRow extends Record<string, unknown> {
  ledgerId: number;
  agentId: number;
  agentName: string;
  contractNo: string;
  settleMonth: string;
  collectionAmount: number;
  commissionAmount: number;
  ratePercent: number;
  status: string;
}

// ── RenewalCommission ─────────────────────────────
export interface RenewalRuleRow extends Record<string, unknown> {
  ruleId: number;
  carrierId: number;
  carrierName: string;
  productType: string;
  ratePercent: number;
  effectiveFrom: string;
  effectiveTo?: string;
  status: string;
}

export interface RenewalLedgerRow extends Record<string, unknown> {
  ledgerId: number;
  agentId: number;
  agentName: string;
  contractNo: string;
  settleMonth: string;
  renewalPremium: number;
  commissionAmount: number;
  status: string;
}

// ── ReinstatementCommission ───────────────────────
export interface ReinstatementRuleRow extends Record<string, unknown> {
  ruleId: number;
  carrierId: number;
  carrierName: string;
  productType: string;
  ratePercent: number;
  effectiveFrom: string;
  effectiveTo?: string;
  status: string;
}

export interface ReinstatementLedgerRow extends Record<string, unknown> {
  ledgerId: number;
  agentId: number;
  agentName: string;
  contractNo: string;
  settleMonth: string;
  reinstatementPremium: number;
  commissionAmount: number;
  status: string;
}

// ── PersistencyBonus ──────────────────────────────
export interface PersistencyRuleRow extends Record<string, unknown> {
  ruleId: number;
  bonusType: string;   // 13M/25M/37M/49M
  minRate: number;
  bonusAmount?: number;
  bonusRate?: number;
  effectiveFrom: string;
  effectiveTo?: string;
  status: string;
}

export interface PersistencyBonusRow extends Record<string, unknown> {
  bonusId: number;
  agentId: number;
  agentName: string;
  bonusType: string;
  evaluationMonth: string;
  retentionRate: number;
  bonusAmount: number;
  paidYn: string;
  status: string;
}

// ── OverrideLedger ────────────────────────────────
export interface OverrideLedgerRow extends Record<string, unknown> {
  ledgerId: number;
  fromAgentId: number;
  fromAgentName: string;
  toAgentId: number;
  toAgentName: string;
  settleMonth: string;
  baseCommission: number;
  overrideRate: number;
  overrideAmount: number;
  orgId: number;
  orgName: string;
  status: string;
}

export interface OverrideLedgerSearchParam {
  fromAgentId?: number;
  toAgentId?: number;
  agentName?: string;
  settleMonth?: string;
  orgId?: number;
  pageNum?: number;
  pageSize?: number;
}

// ── API ───────────────────────────────────────────
export const commissionApi = {
  // CommissionMaster
  masterList: (p: CommissionSearchParam) =>
    unwrap<PageResponse<CommissionMasterRow>>(
      api.get('/api/commissions', { params: p }),
    ),
  masterByAgent: (agentId: number, yyyymm: string) =>
    unwrap<CommissionMasterRow[]>(
      api.get(`/api/commissions/agent/${agentId}`, { params: { yyyymm } }),
    ),

  // CollectionCommission
  collectionRules: (p?: Partial<CommissionSearchParam>) =>
    unwrap<PageResponse<CollectionRuleRow>>(
      api.get('/api/collection-commissions/rules', { params: p }),
    ),
  collectionRuleCreate: (body: Partial<CollectionRuleRow>) =>
    unwrap<CollectionRuleRow>(api.post('/api/collection-commissions/rules', body)),
  collectionRuleUpdate: (id: number, body: Partial<CollectionRuleRow>) =>
    unwrap<CollectionRuleRow>(api.put(`/api/collection-commissions/rules/${id}`, body)),
  collectionRuleDelete: (id: number) =>
    unwrap<void>(api.delete(`/api/collection-commissions/rules/${id}`)),
  collectionLedgers: (p: CommissionSearchParam) =>
    unwrap<PageResponse<CollectionLedgerRow>>(
      api.get('/api/collection-commissions/ledgers', { params: p }),
    ),

  // RenewalCommission
  renewalRules: (p?: Partial<CommissionSearchParam>) =>
    unwrap<PageResponse<RenewalRuleRow>>(
      api.get('/api/renewal-commissions/rules', { params: p }),
    ),
  renewalRuleCreate: (body: Partial<RenewalRuleRow>) =>
    unwrap<RenewalRuleRow>(api.post('/api/renewal-commissions/rules', body)),
  renewalRuleUpdate: (id: number, body: Partial<RenewalRuleRow>) =>
    unwrap<RenewalRuleRow>(api.put(`/api/renewal-commissions/rules/${id}`, body)),
  renewalRuleDelete: (id: number) =>
    unwrap<void>(api.delete(`/api/renewal-commissions/rules/${id}`)),
  renewalLedgers: (p: CommissionSearchParam) =>
    unwrap<PageResponse<RenewalLedgerRow>>(
      api.get('/api/renewal-commissions/ledgers', { params: p }),
    ),

  // ReinstatementCommission
  reinstatementRules: (p?: Partial<CommissionSearchParam>) =>
    unwrap<PageResponse<ReinstatementRuleRow>>(
      api.get('/api/reinstatement-commissions/rules', { params: p }),
    ),
  reinstatementRuleCreate: (body: Partial<ReinstatementRuleRow>) =>
    unwrap<ReinstatementRuleRow>(api.post('/api/reinstatement-commissions/rules', body)),
  reinstatementRuleUpdate: (id: number, body: Partial<ReinstatementRuleRow>) =>
    unwrap<ReinstatementRuleRow>(api.put(`/api/reinstatement-commissions/rules/${id}`, body)),
  reinstatementRuleDelete: (id: number) =>
    unwrap<void>(api.delete(`/api/reinstatement-commissions/rules/${id}`)),
  reinstatementLedgers: (p: CommissionSearchParam) =>
    unwrap<PageResponse<ReinstatementLedgerRow>>(
      api.get('/api/reinstatement-commissions/ledgers', { params: p }),
    ),

  // PersistencyBonus
  persistencyRules: (p?: Partial<CommissionSearchParam>) =>
    unwrap<PageResponse<PersistencyRuleRow>>(
      api.get('/api/persistency-bonus/rules', { params: p }),
    ),
  persistencyRuleCreate: (body: Partial<PersistencyRuleRow>) =>
    unwrap<PersistencyRuleRow>(api.post('/api/persistency-bonus/rules', body)),
  persistencyRuleUpdate: (id: number, body: Partial<PersistencyRuleRow>) =>
    unwrap<PersistencyRuleRow>(api.put(`/api/persistency-bonus/rules/${id}`, body)),
  persistencyRuleDelete: (id: number) =>
    unwrap<void>(api.delete(`/api/persistency-bonus/rules/${id}`)),
  persistencyBonuses: (p: CommissionSearchParam) =>
    unwrap<PageResponse<PersistencyBonusRow>>(
      api.get('/api/persistency-bonus/bonuses', { params: p }),
    ),
  persistencyBonusUpsert: (body: Partial<PersistencyBonusRow>) =>
    unwrap<PersistencyBonusRow>(api.post('/api/persistency-bonus/bonuses', body)),

  // OverrideLedger
  overrideLedgers: (p: OverrideLedgerSearchParam) =>
    unwrap<PageResponse<OverrideLedgerRow>>(
      api.get('/api/override-ledgers', { params: p }),
    ),
};
