import { request } from '@/api/request'

/** 资产状态枚举 */
export enum AssetStatus {
  IN_USE = 'in_use', // 在用
  IDLE = 'idle', // 闲置
  MAINTAIN = 'maintain', // 维修中
  SCRAP = 'scrap', // 待报废
  DISPOSED = 'disposed', // 已报废
}

/** 资产状态选项 */
export const assetStatusOptions = [
  { label: '在用', value: AssetStatus.IN_USE },
  { label: '闲置', value: AssetStatus.IDLE },
  { label: '维修中', value: AssetStatus.MAINTAIN },
  { label: '待报废', value: AssetStatus.SCRAP },
  { label: '已报废', value: AssetStatus.DISPOSED },
]

/** 获取资产状态文本 */
export const getAssetStatusText = (status: string) => {
  const item = assetStatusOptions.find((opt) => opt.value === status)
  return item?.label || status
}

/** 获取资产状态颜色 */
export const getAssetStatusColor = (status: string) => {
  const colorMap: Record<string, string> = {
    [AssetStatus.IN_USE]: 'green',
    [AssetStatus.IDLE]: 'blue',
    [AssetStatus.MAINTAIN]: 'orange',
    [AssetStatus.SCRAP]: 'red',
    [AssetStatus.DISPOSED]: 'gray',
  }
  return colorMap[status] || 'gray'
}

/** 资产列表查询参数 */
export interface AssetListParams {
  page?: number
  page_size?: number
  keyword?: string
  status?: string
  sort?: string
  order?: string
}

/** 资产表单数据 */
export interface AssetForm {
  id?: number
  asset_name: string
  asset_code: string
  category_id?: number
  model?: string
  manufacturer?: string
  serial_number?: string
  source_address?: string
  purchase_date?: string
  original_value?: number
  supplier_id?: number
  warranty_period?: number
  warranty_expiry?: string
  department?: string
  user?: string
  status?: string
  location?: string
  datacenter_id?: number
  floor_id?: number
  room_id?: number
  qr_code?: string
  rfid_tag?: string
  asset_tag?: string
  specifications?: string
  description?: string
  remarks?: string
}

/** 获取资产列表（分页） */
export const fetchAssetList = (data?: AssetListParams) => {
  return request.post('/Assets/v1/asset/list', data || {})
}

/** 获取资产列表（不分页，下拉） */
export const fetchAssetAll = (params?: { keyword?: string }) => {
  return request.get('/Assets/v1/asset/all', { params })
}

/** 获取资产详情 */
export const fetchAssetDetail = (id: number) => {
  return request.get(`/Assets/v1/asset/detail/${id}`)
}

/** 创建资产 */
export const createAsset = (data: AssetForm) => {
  return request.post('/Assets/v1/asset/create', data)
}

/** 批量导入资产 */
export const bulkImportAssets = (data: { dry_run?: boolean; items: any[] }) => {
  return request.post('/Assets/v1/asset/bulk_import', data)
}

/** 更新资产 */
export const updateAsset = (data: AssetForm) => {
  return request.put('/Assets/v1/asset/update', data)
}

/** 删除资产 */
export const deleteAsset = (id: number) => {
  return request.delete(`/Assets/v1/asset/delete/${id}`)
}

/** 导出资产 */
export const exportAssets = (keyword?: string) => {
  const params: any = {}
  if (keyword) params.keyword = keyword
  return request.get('/Assets/v1/asset/export', { params })
}

/** 获取资产分类列表（下拉） */
export const fetchCategoryOptions = () => {
  return request.get('/Assets/v1/category/all')
}

/** 获取供应商列表（下拉） */
export const fetchSupplierOptions = () => {
  return request.get('/Assets/v1/supplier/all')
}

/** 绑定资产和监控资源 */
export const linkAssetResource = (data: {
  asset_id: number
  resource_uid: string
  display_name?: string
  business_system_id?: number
  health_status?: string
  alert_status?: string
}) => {
  return request.post('/Assets/v1/asset/resource/link', data)
}

/** 查询资产绑定的监控资源 */
export const fetchAssetResourceBindings = (assetId: number) => {
  return request.get(`/Assets/v1/asset/resource/${assetId}`)
}

/** 解除资产和监控资源绑定 */
export const unlinkAssetResource = (assetId: number, resourceUid: string) => {
  return request.delete(`/Assets/v1/asset/resource/${assetId}`, { params: { resource_uid: resourceUid } })
}
