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

export interface ReviewTaskParams {
  pageNum?: number;
  pageSize?: number;
  contractName: string;
}

export interface ReviewTaskItem {
  taskId: string | null;
  templateId: string | null;
  reviewRule: string;
  reviewElement: string;
  contractUrl: string;
  contractContentMd: string;
  contractPic: string | null;
  status: number;
  deptId: number;
  userId: number;
  createBy: string;
}

export interface ReviewTaskResponseData {
  code: number;
  msg: string;
  total: number;
  rows: ReviewTaskItem[];
}

export interface ReviewTaskResponse {
  response: {
    data: ReviewTaskResponseData;
  };
}

/** 审核结果接口 */
export interface ReviewResult {
  resultId?: number;
  taskId?: number;
  contractContent?: string;
  riskLevel?: number | string;
  riskTips?: string;
  reviewBasis?: string;
  modifyExample?: string;
  specType?: number; // 1:规则 2:要素
  pageNum?: number;
}

export interface ReviewResultResponse {
  code: number;
  msg: string;
  data: ReviewResult[];
}

/**
 * 创建合同审查任务
 *
 * @param data 审查任务参数
 */
export interface CreateReviewTaskParams {
  /** 合同名称 */
  contractName: string;
  /** 合同地址 */
  contractUrl: string;
  /** 规则模板ID */
  templateId: number;
  modelId: number | null;
}

/**
 * 创建合同审查任务
 *
 * @param data 审查任务参数
 */
export function createReviewTask(data: CreateReviewTaskParams) {
  return request({
    url: '/api/service/reviewTask/create',
    method: 'POST',
    data
  });
}

/**
 * 获取合同审核任务列表
 *
 * @param params 分页参数
 */
export function fetchReviewTaskList(params: ReviewTaskParams) {
  return request<ReviewTaskResponse>({
    url: '/api/service/reviewTask/list',
    params
  });
}

/**
 * 获取合同审核结果
 *
 * @param taskId 任务ID
 */
export function fetchReviewResults(taskId: string | null) {
  if (!taskId) {
    return Promise.resolve({ data: { code: 200, msg: '成功', data: [] }, error: null });
  }

  return request<ReviewResultResponse>({
    url: `/api/service/reviewResult/list/${taskId}`,
    method: 'GET'
  });
}

// 重试
export function retryReviewTask(taskId: string | number, query: { modelId?: string; type: string }) {
  return request({
    url: `/api/service/reviewTask/retry/${taskId}`,
    method: 'get',
    params: query
  });
}

// 轮询获取状态
export function pollReviewTaskStatus(taskId: string | number) {
  return request({
    url: `/api/service/reviewTask/status/${taskId}`,
    method: 'get'
  });
}

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


/**
 * 获取审核任务状态文本
 *
 * @param status 状态码
 */
export function getReviewStatusText(status: number): string {
  switch (status) {
    case 0:
      return '未开始';
    case 1:
      return '获取原文中';
    case 2:
      return '操作中';
    case 3:
      return '已完成';
    case 4:
      return '失败';
    default:
      return '未知状态';
  }
}

/**
 * 获取审核任务状态类型
 *
 * @param status 状态码
 */
export function getReviewStatusType(status: number): string {
  switch (status) {
    case 0:
      return 'waiting';
    case 1:
      return 'review';
    case 2:
      return 'waiting';
    case 3:
      return 'pass';
    case 4:
      return 'reject';
    default:
      return 'waiting';
  }
}
