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

/** 机房项 */
export interface RoomItem {
  id: number
  created_at: string
  updated_at: string
  deleted_at: string | null
  name: string
  code: string
  description?: string
  floor_id: number
  datacenter_id: number
  layout_version: number
  status: string
  has3D: boolean
  floor?: { id: number; name: string }
  datacenter?: { id: number; name: string }
}

/** 机房列表响应 */
export interface RoomListResponse {
  code: number
  details: {
    data: RoomItem[]
    total: number
  }
}

/** 机房详情响应 */
export interface RoomDetailResponse {
  code: number
  message?: string
  details: RoomItem
}

/** 机房列表查询参数 */
export interface RoomListParams {
  page?: number
  page_size?: number
  keyword?: string
  datacenter_id?: number
  floor_id?: number
  status?: string
}

/** 机房下拉选项参数 */
export interface RoomOptionsParams {
  datacenter_id?: number
  floor_id?: number
  keyword?: string
}

/** 机房下拉选项项 */
export interface RoomOptionItem {
  id: number
  name: string
  code: string
  datacenter_id: number
  floor_id: number
  enabled: boolean
  sort: number
}

export interface ApiResponse<T> {
  code: number
  message?: string
  details: T
}

/** 机房创建数据 */
export interface RoomCreateData {
  name: string
  code: string
  description?: string
  floor_id: number
  datacenter_id: number
  status: string
}

/** 机房更新数据 */
export interface RoomUpdateData {
  id: number
  name?: string
  code?: string
  description?: string
  floor_id?: number
  datacenter_id?: number
  status?: string
}

/** 获取机房列表（分页） */
export const fetchRoomList = (data?: RoomListParams) => {
  return request.post<RoomListResponse>('/Assets/v1/room/list', data || {})
}

/** 获取机房详情 */
export const fetchRoomDetail = (id: number) => {
  return request.get<RoomDetailResponse>(`/Assets/v1/room/detail/${id}`)
}

/** 获取机房列表（下拉，不分页） */
export const fetchRoomOptions = (params?: RoomOptionsParams) => {
  return request.get<ApiResponse<RoomOptionItem[]>>('/Assets/v1/room/all', { params })
}

/** 根据楼层获取机房列表（下拉，不分页） */
export const fetchRoomListByFloor = (floorId: number, params?: { name?: string }) => {
  return request.get<ApiResponse<RoomItem[]>>(`/Assets/v1/room/floor/${floorId}`, { params })
}

/** 创建机房 */
export const createRoom = (data: RoomCreateData) => {
  return request.post('/Assets/v1/room/create', data)
}

/** 更新机房 */
export const updateRoom = (data: RoomUpdateData) => {
  return request.put('/Assets/v1/room/update', data)
}

/** 删除机房 */
export const deleteRoom = (id: number) => {
  return request.delete(`/Assets/v1/room/delete/${id}`)
}
