import request from '@/utils/request'
import type { ApiResult } from '@/api'
import type { PageResult } from '@/api'
import { APP_API_URL } from '@/config/setting'

const BASE = APP_API_URL + '/contract'

// 合同类型
export type ContractType = 'service' | 'cooperation' | 'purchase' | 'other'
export type ContractStatus = 'draft' | 'pending' | 'active' | 'expired' | 'terminated'

// 合同模型
export interface Contract {
  contractId?: number
  contractNo?: string
  title: string
  contractType: ContractType
  partyA?: string
  partyB?: string
  amount?: number
  startDate?: string
  endDate?: string
  status: ContractStatus
  fileUrl?: string
  fileName?: string
  remark?: string
  userId?: number
  userName?: string
  createTime?: string
  updateTime?: string
}

// 合同类型选项
export const contractTypeOptions = [
  { label: '服务合同', value: 'service' },
  { label: '合作协议', value: 'cooperation' },
  { label: '采购合同', value: 'purchase' },
  { label: '其他', value: 'other' },
]

// 合同状态选项
export const contractStatusOptions = [
  { label: '草稿', value: 'draft', color: 'default' },
  { label: '待签署', value: 'pending', color: 'orange' },
  { label: '生效中', value: 'active', color: 'green' },
  { label: '已过期', value: 'expired', color: 'red' },
  { label: '已终止', value: 'terminated', color: 'purple' },
]

/** 获取合同类型文本 */
export function contractTypeText(type?: ContractType | null): string {
  if (!type) return '-'
  const map: Record<ContractType, string> = {
    service: '服务合同',
    cooperation: '合作协议',
    purchase: '采购合同',
    other: '其他',
  }
  return map[type] || '-'
}

/** 获取合同状态标签 */
export function contractStatusInfo(status?: ContractStatus | null) {
  if (!status) return { text: '-', color: 'default' }
  const map: Record<ContractStatus, { text: string; color: string }> = {
    draft: { text: '草稿', color: 'default' },
    pending: { text: '待签署', color: 'orange' },
    active: { text: '生效中', color: 'green' },
    expired: { text: '已过期', color: 'red' },
    terminated: { text: '已终止', color: 'purple' },
  }
  return map[status] || { text: '-', color: 'default' }
}

// ============ 接口方法 ============

/** 分页查询合同列表 */
export async function pageContract(params: {
  page?: number
  limit?: number
  keywords?: string
  contractType?: ContractType
  status?: ContractStatus
}) {
  const res = await request.get<ApiResult<PageResult<Contract>>>(BASE + '/page', { params })
  if (res.data.code === 0 && res.data.data) return res.data.data
  return Promise.reject(new Error(res.data.message))
}

/** 获取合同详情 */
export async function getContract(contractId: number) {
  const res = await request.get<ApiResult<Contract>>(BASE + '/' + contractId)
  if (res.data.code === 0 && res.data.data) return res.data.data
  return Promise.reject(new Error(res.data.message))
}

/** 新增合同 */
export async function addContract(data: Partial<Contract>) {
  const res = await request.post<ApiResult<Contract>>(BASE, data)
  if (res.data.code === 0) return res.data.data
  return Promise.reject(new Error(res.data.message))
}

/** 更新合同 */
export async function updateContract(contractId: number, data: Partial<Contract>) {
  const res = await request.put<ApiResult<Contract>>(BASE + '/' + contractId, data)
  if (res.data.code === 0) return res.data.data
  return Promise.reject(new Error(res.data.message))
}

/** 删除合同 */
export async function removeContract(contractId: number) {
  const res = await request.delete<ApiResult<unknown>>(BASE + '/' + contractId)
  if (res.data.code === 0) return res.data.message
  return Promise.reject(new Error(res.data.message))
}

/** 合同统计 */
export async function statsContract() {
  const res = await request.get<ApiResult<Record<string, number>>>(BASE + '/stats')
  if (res.data.code === 0 && res.data.data) return res.data.data
  return Promise.reject(new Error(res.data.message))
}
