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

export interface RetentionAnomalyResp extends Record<string, unknown> {
  alertId: number;
  targetType: string;
  targetTypeName?: string;
  targetId?: number;
  targetName?: string;
  alertMonth: string;
  retentionBand: string;
  expectedRate?: number;
  actualRate?: number;
  deviation?: number;
  severity: string;
  severityName?: string;
  status: string;
  statusName?: string;
  note?: string;
  detectedAt?: string;
  reviewedAt?: string;
  reviewedByName?: string;
}

export interface RetentionAnomalySearchParam {
  alertMonth?: string;
  targetType?: string;
  retentionBand?: string;
  severity?: string;
  status?: string;
  pageNum?: number;
  pageSize?: number;
}

export interface RetentionAnomalyReviewReq {
  ids: number[];
  status: string;
  note?: string;
}

export const retentionAnomalyApi = {
  list: (params?: RetentionAnomalySearchParam) =>
    unwrap<{ list: RetentionAnomalyResp[]; total: number }>(
      api.get('/api/retention-anomalies', { params })
    ),
  detail: (id: number) =>
    unwrap<RetentionAnomalyResp>(api.get(`/api/retention-anomalies/${id}`)),
  detect: (alertMonth: string) =>
    unwrap<number>(api.post('/api/retention-anomalies/detect', { alertMonth })),
  review: (req: RetentionAnomalyReviewReq) =>
    unwrap<void>(api.put('/api/retention-anomalies/review', req)),
};
