import { request } from '../request';

/** 合同对比任务类型 */
export interface ContractTaskItem {
  taskId: string | number;
  compareRule: string | null;
  firstContractUrl: string;
  secondContractUrl: string;
  firstContractContent?: string;
  secondContractContent?: string;
  status: number;
  deptId: string | number | null;
  userId: string | number | null;
  creator?: string | null;
}

/** 合同差异对比结果项API返回格式 */
export interface CompareResultItemApi {
  resultId: number;
  taskId: number;
  baseBoxArea: string;
  baseDiffContent: string;
  baseDiffType: 'insert' | 'delete' | 'change';
  basePageNum: number;
  compareBoxArea: string;
  compareDiffContent: string | null;
  compareDiffType: 'insert' | 'delete' | 'change';
  comparePageNum: number;
  firstPageNum: number;
  secondPageNum: number;
  createTime: string;
}

/** 合同差异对比结果项 */
export interface CompareResultItem {
  baseBoxArea: string;
  baseDiffContent: string;
  baseDiffType: 'modify' | 'insert' | 'delete';
  basePageNum: number;
  compareBoxArea: string;
  compareDiffContent: string;
  compareDiffType: 'modify' | 'insert' | 'delete';
  comparePageNum: number;
  firstPageNum: number;
  secondPageNum: number;
  id: number;
}

/** 合同对比任务API内层响应格式 */
export interface ContractTaskResponseInner {
  code: number;
  msg: string;
  total: number;
  rows: ContractTaskItem[];
}

/** 合同对比任务API响应格式 */
export interface ContractTaskResponse {
  data: {
    total: number;
    data: ContractTaskItem[];
  };
  error: any;
  response: {
    data: ContractTaskResponseInner;
    status: number;
    statusText: string;
    headers: Record<string, string>;
    config: any;
    request: any;
  };
}

/** 合同对比分析结果API响应格式 */
export interface CompareResultResponse {
  data: CompareResultItemApi[];
  error: any;
  response: {
    data: {
      code: number;
      msg: string;
      data: CompareResultItemApi[];
    };
    status: number;
    statusText: string;
    headers: Record<string, string>;
    config: any;
    request: any;
  };
}

/**
 * 获取合同对比任务列表
 *
 * @param params 分页参数 {pageNum, pageSize}
 * @returns 合同对比任务列表
 */
export function fetchContractTaskList(params?: { pageNum?: number; pageSize?: number }) {
  return request<ContractTaskResponse>({
    url: '/api/service/compareTask/list',
    params
  });
}

/**
 * 获取合同对比分析结果
 *
 * @param taskId 任务ID
 * @returns 合同对比分析结果
 */
export function fetchCompareResult(taskId: string | number) {
  return request<CompareResultResponse>({
    url: `/api/compareResult/list/${taskId}`
  });
}

// 创建合同对比任务
export function createCompareTask(data: {
  firstContractUrl: string;
  firstContractName: string;
  secondContractUrl: string;
  secondContractName: string;
}) {
  return request({
    url: '/api/service/compareTask/create',
    method: 'post',
    data
  });
}

// 重试
export function retryCompareTask(taskId: string | number) {
  return request({
    url: `/api/service/compareTask/retry/${taskId}`,
    method: 'get'
  });
}

// 轮询对比任务结果
export function pollCompareTaskResult(taskId: string | number) {
  return request({
    url: `/api/service/compareTask/status/${taskId}`,
    method: 'get'
  });
}

// 删除对比任务 "service:compareTask:remove"
export function deleteCompareTask(taskIds: Array<string | number>) {
  return request({
    url: `/api/service/compareTask/${taskIds}`,
    method: 'delete'
  });
}
