/**
 * 设备管理相关 API
 */

import { BaseService } from '../BaseService'
import type {
  Device,
  DeviceQueryParams,
  DeviceListResponse,
  DeviceCardsResponse,
  BindCardToDeviceRequest,
  BindCardToDeviceResponse,
  UnbindCardFromDeviceResponse,
  AllocateDevicesRequest,
  AllocateDevicesResponse,
  RecallDevicesRequest,
  RecallDevicesResponse,
  BatchSetDeviceSeriesBindingRequest,
  BatchSetDeviceSeriesBindingResponse,
  BatchUpdateAssetRealnamePolicyRequest,
  ImportDeviceRequest,
  ImportDeviceResponse,
  DeviceImportTaskQueryParams,
  DeviceImportTaskListResponse,
  DeviceImportTaskDetail,
  BaseResponse,
  SetSpeedLimitRequest,
  SwitchCardRequest,
  SetWiFiRequest,
  DeviceOperationResponse
} from '@/types/api'

export class DeviceService extends BaseService {
  // ========== 设备基础管理 ==========

  /**
   * 获取设备列表
   * @param params 查询参数
   */
  static getDevices(params?: DeviceQueryParams): Promise<BaseResponse<DeviceListResponse>> {
    return this.get<BaseResponse<DeviceListResponse>>('/api/admin/devices', params)
  }

  /**
   * 获取设备详情
   * @param id 设备ID
   */
  static getDeviceById(id: number): Promise<BaseResponse<Device>> {
    return this.getOne<Device>(`/api/admin/devices/${id}`)
  }

  /**
   * 通过ICCID查询设备详情
   * @param iccid ICCID
   */
  static getDeviceByIccid(iccid: string): Promise<BaseResponse<any>> {
    return this.getOne<any>(`/api/admin/devices/by-iccid/${iccid}`)
  }

  /**
   * 删除设备
   * @param virtualNo 设备虚拟号
   */
  static deleteDevice(virtualNo: string): Promise<BaseResponse> {
    return this.remove(`/api/admin/devices/${virtualNo}`)
  }

  // ========== 设备卡绑定管理 ==========

  /**
   * 获取设备绑定的卡列表
   * @param virtualNo 设备虚拟号
   */
  static getDeviceCards(virtualNo: string): Promise<BaseResponse<DeviceCardsResponse>> {
    return this.getOne<DeviceCardsResponse>(`/api/admin/devices/${virtualNo}/cards`)
  }

  /**
   * 绑定卡到设备
   * @param virtualNo 设备虚拟号
   * @param data 绑定参数
   */
  static bindCard(
    virtualNo: string,
    data: BindCardToDeviceRequest
  ): Promise<BaseResponse<BindCardToDeviceResponse>> {
    return this.post<BaseResponse<BindCardToDeviceResponse>>(
      `/api/admin/devices/${virtualNo}/cards`,
      data
    )
  }

  /**
   * 解绑设备上的卡
   * @param virtualNo 设备虚拟号
   * @param iccid 卡ICCID
   */
  static unbindCard(
    virtualNo: string,
    iccid: string
  ): Promise<BaseResponse<UnbindCardFromDeviceResponse>> {
    return this.delete<BaseResponse<UnbindCardFromDeviceResponse>>(
      `/api/admin/devices/${virtualNo}/cards/${iccid}`
    )
  }

  // ========== 批量分配和回收 ==========

  /**
   * 批量分配设备
   * @param data 分配参数
   */
  static allocateDevices(
    data: AllocateDevicesRequest
  ): Promise<BaseResponse<AllocateDevicesResponse>> {
    return this.post<BaseResponse<AllocateDevicesResponse>>('/api/admin/devices/allocate', data)
  }

  /**
   * 批量回收设备
   * @param data 回收参数
   */
  static recallDevices(data: RecallDevicesRequest): Promise<BaseResponse<RecallDevicesResponse>> {
    return this.post<BaseResponse<RecallDevicesResponse>>('/api/admin/devices/recall', data)
  }

  // ========== 设备导入 ==========

  /**
   * 批量导入设备
   * @param data 导入参数
   */
  static importDevices(data: ImportDeviceRequest): Promise<BaseResponse<ImportDeviceResponse>> {
    return this.post<BaseResponse<ImportDeviceResponse>>('/api/admin/devices/import', data)
  }

  /**
   * 获取导入任务列表
   * @param params 查询参数
   */
  static getImportTasks(
    params?: DeviceImportTaskQueryParams
  ): Promise<BaseResponse<DeviceImportTaskListResponse>> {
    return this.get<BaseResponse<DeviceImportTaskListResponse>>(
      '/api/admin/devices/import/tasks',
      params
    )
  }

  /**
   * 获取导入任务详情
   * @param id 任务ID
   */
  static getImportTaskDetail(id: number): Promise<BaseResponse<DeviceImportTaskDetail>> {
    return this.getOne<DeviceImportTaskDetail>(`/api/admin/devices/import/tasks/${id}`)
  }

  // ========== 批量设置设备的套餐系列绑定相关 ==========

  /**
   * 批量设置设备的套餐系列绑定
   * @param data 请求参数
   */
  static batchSetDeviceSeriesBinding(
    data: BatchSetDeviceSeriesBindingRequest
  ): Promise<BaseResponse<BatchSetDeviceSeriesBindingResponse>> {
    return this.patch<BaseResponse<BatchSetDeviceSeriesBindingResponse>>(
      '/api/admin/devices/series-binding',
      data
    )
  }

  /**
   * 批量更新设备实名认证策略
   */
  static batchUpdateRealnamePolicy(
    data: BatchUpdateAssetRealnamePolicyRequest,
    config?: Record<string, any>
  ): Promise<BaseResponse<void>> {
    return this.post<BaseResponse<void>>(
      '/api/admin/devices/batch-update-realname-policy',
      data,
      config
    )
  }

  // ========== 设备操作相关 ==========

  /**
   * 重启设备
   * @param imei 设备号(IMEI)
   */
  static rebootDevice(imei: string): Promise<BaseResponse<DeviceOperationResponse>> {
    return this.post<BaseResponse<DeviceOperationResponse>>(
      `/api/admin/devices/by-identifier/${imei}/reboot`,
      {}
    )
  }

  /**
   * 恢复出厂设置
   * @param imei 设备号(IMEI)
   */
  static resetDevice(imei: string): Promise<BaseResponse<DeviceOperationResponse>> {
    return this.post<BaseResponse<DeviceOperationResponse>>(
      `/api/admin/devices/by-identifier/${imei}/reset`,
      {}
    )
  }

  /**
   * 设置限速
   * @param imei 设备号(IMEI)
   * @param data 限速参数
   */
  static setSpeedLimit(
    imei: string,
    data: SetSpeedLimitRequest
  ): Promise<BaseResponse<DeviceOperationResponse>> {
    return this.put<BaseResponse<DeviceOperationResponse>>(
      `/api/admin/devices/by-identifier/${imei}/speed-limit`,
      data
    )
  }

  /**
   * 切换SIM卡
   * @param imei 设备号(IMEI)
   * @param data 切卡参数
   */
  static switchCard(
    imei: string,
    data: SwitchCardRequest
  ): Promise<BaseResponse<DeviceOperationResponse>> {
    return this.post<BaseResponse<DeviceOperationResponse>>(
      `/api/admin/devices/by-identifier/${imei}/switch-card`,
      data
    )
  }

  /**
   * 设置WiFi
   * @param imei 设备号(IMEI)
   * @param data WiFi参数
   */
  static setWiFi(
    imei: string,
    data: SetWiFiRequest
  ): Promise<BaseResponse<DeviceOperationResponse>> {
    return this.put<BaseResponse<DeviceOperationResponse>>(
      `/api/admin/devices/by-identifier/${imei}/wifi`,
      data
    )
  }

  /**
   * 设置切卡模式
   * @param identifier 设备标识（虚拟号 / IMEI / SN）
   * @param data
   */
  static setSwitchMode(
    identifier: string,
    data: { iot_card_id: number; switch_mode: 0 | 1 }
  ): Promise<BaseResponse<DeviceOperationResponse>> {
    return this.post<BaseResponse<DeviceOperationResponse>>(
      `/api/admin/devices/by-identifier/${identifier}/switch-mode`,
      data
    )
  }
}
