/**
 * 设备类型管理 API
 */
import { request } from './request'
import type { DeviceType, DynamicField, PaginationResponse } from '@/types'

/** 设备类型列表参数 */
export interface DeviceTypeListParams {
  category?: string
  status?: string
}

/** 创建设备类型参数 */
export interface DeviceTypeCreateParams {
  typeCode: string
  typeName: string
  category: string
  description?: string
  icon?: string
  sortOrder?: number
}

/** 更新设备类型参数 */
export type DeviceTypeUpdateParams = Partial<DeviceTypeCreateParams>

/** 创建字段参数 */
export interface FieldCreateParams {
  fieldCode: string
  fieldName: string
  fieldType: DynamicField['fieldType']
  isRequired: boolean
  placeholder?: string
  options?: Array<{ label: string; value: any }>
  validationRules?: Record<string, any>
  defaultValue?: any
  sortOrder: number
}

/** 更新字段参数 */
export type FieldUpdateParams = Partial<FieldCreateParams>

/**
 * 获取设备类型列表
 */
export const getDeviceTypeList = (params?: DeviceTypeListParams) => {
  return request.get<DeviceType[]>('/device-types', { params })
}

/**
 * 获取设备类型详情
 */
export const getDeviceTypeById = (id: number) => {
  return request.get<DeviceType>(`/device-types/${id}`)
}

/**
 * 创建设备类型
 */
export const createDeviceType = (data: DeviceTypeCreateParams) => {
  return request.post<DeviceType>('/device-types', data)
}

/**
 * 更新设备类型
 */
export const updateDeviceType = (id: number, data: DeviceTypeUpdateParams) => {
  return request.put(`/device-types/${id}`, data)
}

/**
 * 删除设备类型
 */
export const deleteDeviceType = (id: number) => {
  return request.delete(`/device-types/${id}`)
}

/**
 * 获取设备类型的字段配置
 */
export const getDeviceTypeFields = (typeId: number) => {
  return request.get<DynamicField[]>(`/device-types/${typeId}/fields`)
}

/**
 * 添加字段
 */
export const addDeviceTypeField = (typeId: number, data: FieldCreateParams) => {
  return request.post<DynamicField>(`/device-types/${typeId}/fields`, data)
}

/**
 * 更新字段
 */
export const updateDeviceTypeField = (typeId: number, fieldId: number, data: FieldUpdateParams) => {
  return request.put(`/device-types/${typeId}/fields/${fieldId}`, data)
}

/**
 * 删除字段
 */
export const deleteDeviceTypeField = (typeId: number, fieldId: number) => {
  return request.delete(`/device-types/${typeId}/fields/${fieldId}`)
}
