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

export interface CompanyProfileResp {
  companyCode: string;
  dataFormat?: string;
  receiveMethod?: string;
  fileEncoding?: string;
  delimiter?: string;
  headerRow?: number;
  dataStartRow?: number;
  sheetName?: string;
  dateFormat?: string;
  amountFormat?: string;
  apiUrl?: string;
  ftpHost?: string;
  ftpPath?: string;
  receiveSchedule?: string;
  isActive: string;
}

export interface FieldMappingResp {
  mappingId: number;
  companyCode: string;
  sourceField: string;
  sourceColIndex?: number;
  targetTable: string;
  targetField: string;
  dataType?: string;
  transformRule?: string;
  defaultValue?: string;
  isRequired?: string;
  sortOrder: number;
  version?: number;
  effectiveFrom?: string;
}

export interface RawCommissionResp {
  rawId: number;
  companyCode: string;
  batchId?: string;
  settleMonth: string;
  dataType: string;
  parseStatus: string;
  mappedLedgerId?: number;
  mappedTable?: string;
  fileName?: string;
  rowNumber?: number;
  receivedAt?: string;
  parsedAt?: string;
}

export interface ParseErrorResp {
  errorId: number;
  rawId?: number;
  companyCode: string;
  errorType: string;
  errorField?: string;
  errorValue?: string;
  errorMessage?: string;
  resolveStatus: string;
  resolvedBy?: number;
  resolveNote?: string;
  resolvedAt?: string;
  createdAt?: string;
}

export const receiveApi = {
  profiles: () => unwrap<CompanyProfileResp[]>(api.get('/api/receive/profiles')),
  fields:   (companyCode: string) =>
    unwrap<FieldMappingResp[]>(api.get(`/api/receive/mapping/${companyCode}/fields`)),
  raw:      (params: { companyCode?: string; settleMonth?: string; parseStatus?: string }) =>
    unwrap<RawCommissionResp[]>(api.get('/api/receive/raw', { params })),
  errors:   (params: { companyCode?: string; resolveStatus?: string }) =>
    unwrap<ParseErrorResp[]>(api.get('/api/receive/errors', { params })),
};
