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

/** U 位状态 */
export type RackUnitStatus = 'available' | 'occupied' | 'reserved' | 'disabled'

/** U 位关联资产摘要 */
export interface RackUnitAssetSummary {
  unit_start: number
}

/** U 位公共字段 */
export interface RackUnitBase {
  unit_number: number
  occupied_units: number
  description?: string
}

/** 可用 U 位 */
export interface AvailableRackUnit extends RackUnitBase {
  status: 'available'
}

/** 已占用 U 位 */
export interface OccupiedRackUnit extends RackUnitBase {
  status: 'occupied'
  asset_id: number
  asset_name: string
  asset_code: string
  asset_type: string
  power_consumption: number
  asset: RackUnitAssetSummary
}

/** 已预留 U 位 */
export interface ReservedRackUnit extends RackUnitBase {
  status: 'reserved'
  reserved_for: string
  reserved_until: string
}

/** 已禁用 U 位 */
export interface DisabledRackUnit extends RackUnitBase {
  status: 'disabled'
}

/** 单个 U 位数据 */
export type RackUnitItem = AvailableRackUnit | OccupiedRackUnit | ReservedRackUnit | DisabledRackUnit

/** U 位页面使用的机柜摘要 */
export interface RackUnitRackInfo {
  id?: number
  room_id: number
  height: number
  status?: string
  used_power?: number
}

/** 获取机柜 U 位状态响应 */
export interface RackUnitListResponse {
  code: number
  message?: string
  details: {
    rack: RackUnitRackInfo
    units: RackUnitItem[]
  }
}

/** 获取机柜U位状态 */
export const fetchRackUnits = (rackId: number) => {
  return request.get<RackUnitListResponse>(`/Assets/v1/unit/rack/${rackId}`)
}

/** 获取U位列表（别名） */
export const fetchUnitList = fetchRackUnits

/** 机柜设备上架数据 */
export interface RackPlacementData {
  expected_version: number
  rack_id: number
  start_unit: number
  occupied_units: number
  power_consumption: number
}

/** 将资产上架到机柜U位 */
export const placeAssetInRack = (roomId: number, assetId: number, data: RackPlacementData) => {
  return request.put(`/Assets/v1/three-d/rooms/${roomId}/devices/${assetId}/placement`, {
    ...data,
    placement_type: 'rack',
  })
}

/** 取消资产在机房中的放置 */
export const unplaceAsset = (roomId: number, assetId: number, expectedVersion: number) => {
  return request.delete(`/Assets/v1/three-d/rooms/${roomId}/devices/${assetId}/placement`, {
    data: { expected_version: expectedVersion },
  })
}

/** 预留U位 */
export const reserveUnit = (data: {
  rack_id: number
  start_unit: number
  occupied_units: number
  reserved_for: string
  reserved_until: string
  description?: string
}) => {
  return request.post('/Assets/v1/unit/reserve', data)
}

/** 取消预留 */
export const cancelReservation = (data: { rack_id: number; start_unit: number; end_unit: number }) => {
  return request.post('/Assets/v1/unit/cancel_reservation', data)
}

/** 更新U位状态 */
export const updateUnitStatus = (data: { rack_id: number; start_unit: number; end_unit: number; status: 'available' | 'disabled' }) => {
  return request.put('/Assets/v1/unit/status', data)
}

/** 获取U位容量趋势 */
export const fetchUnitTrend = (params?: { datacenter_id?: number; months?: number }) => {
  return request.get('/Assets/v1/capacity/unit/trend', { params })
}
