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

export interface BatchRunResp {
  jobLogId: number;
}

export interface BatchJobLogResp {
  jobLogId: number;
  jobName: string;
  status: 'REQUESTED' | 'RUNNING' | 'COMPLETED' | 'FAILED';
  stepName?: string;
  totalCount?: number;
  successCount?: number;
  errorCount?: number;
  startedAt?: string;
  endedAt?: string;
  durationMs?: number;
  errorMessage?: string;
}

export interface BatchHistoryResp {
  jobLogId: number;
  jobName: string;
  status: string;
  stepName?: string;
  startedAt?: string;
  endedAt?: string;
  durationMs?: number;
  errorMessage?: string;
}

export const batchRunApi = {
  run: (settleMonth: string, companyCode?: string) =>
    unwrap<number>(
      api.post('/api/batch/settlement/run', null, {
        params: { settleMonth, companyCode },
      }),
    ),
  status: (jobLogId: number) =>
    unwrap<BatchJobLogResp>(
      api.get('/api/batch/settlement/status', { params: { jobLogId } }),
    ),
  history: (size = 10) =>
    unwrap<BatchHistoryResp[]>(
      api.get('/api/batch/settlement/history', { params: { size } }),
    ),
};
