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

export interface ChargebackGradeResp {
  gradeId: number;
  productCategory: string | null;
  productCategoryName: string | null;
  monthsFrom: number;
  monthsTo: number | null;
  monthsRange: string;
  chargebackPercent: number;
  effectiveFrom: string;
  effectiveTo: string | null;
  isActive: boolean;
}

export interface ChargebackGradeSaveReq {
  productCategory?: string | null;
  monthsFrom: number;
  monthsTo?: number | null;
  chargebackPercent: number;
  effectiveFrom: string;
  effectiveTo?: string | null;
}

export interface ChargebackGradeSearchParam {
  productCategory?: string;
  effectiveDate?: string;
  pageNum?: number;
  pageSize?: number;
}

export const chargebackGradeApi = {
  list: (params: ChargebackGradeSearchParam) =>
    unwrap<PageResponse<ChargebackGradeResp>>(
      api.get('/api/chargeback-grades', { params }),
    ),

  get: (gradeId: number) =>
    unwrap<ChargebackGradeResp>(api.get(`/api/chargeback-grades/${gradeId}`)),

  create: (req: ChargebackGradeSaveReq) =>
    unwrap<number>(api.post('/api/chargeback-grades', req)),

  update: (gradeId: number, req: ChargebackGradeSaveReq) =>
    unwrap<void>(api.put(`/api/chargeback-grades/${gradeId}`, req)),

  deactivate: (gradeId: number) =>
    unwrap<void>(api.put(`/api/chargeback-grades/${gradeId}/deactivate`)),
};
