/**
 * 企业客户管理 API
 */

import { BaseService } from '../BaseService'
import type { BaseResponse } from '@/types/api'
import type {
  EnterpriseItem,
  EnterprisePageResult,
  EnterpriseQueryParams,
  CreateEnterpriseParams,
  UpdateEnterpriseParams,
  UpdateEnterprisePasswordParams,
  UpdateEnterpriseStatusParams,
  CreateEnterpriseResponse
} from '@/types/api/enterprise'
import type {
  AllocateCardsRequest,
  AllocateCardsResponse,
  AllocateCardsPreviewRequest,
  AllocateCardsPreviewResponse,
  EnterpriseCardListParams,
  EnterpriseCardPageResult,
  RecallCardsRequest,
  RecallCardsResponse
} from '@/types/api/enterpriseCard'
import type {
  EnterpriseDeviceListParams,
  EnterpriseDevicePageResult,
  EnterpriseAllocateDevicesRequest,
  EnterpriseAllocateDevicesResponse,
  EnterpriseRecallDevicesRequest,
  EnterpriseRecallDevicesResponse
} from '@/types/api/enterpriseDevice'

export class EnterpriseService extends BaseService {
  /**
   * 查询企业客户列表
   */
  static getEnterprises(
    params?: EnterpriseQueryParams
  ): Promise<BaseResponse<EnterprisePageResult>> {
    return this.get<BaseResponse<EnterprisePageResult>>('/api/admin/enterprises', params)
  }

  /**
   * 新增企业客户
   */
  static createEnterprise(
    data: CreateEnterpriseParams
  ): Promise<BaseResponse<CreateEnterpriseResponse>> {
    return this.post<BaseResponse<CreateEnterpriseResponse>>('/api/admin/enterprises', data)
  }

  /**
   * 编辑企业信息
   */
  static updateEnterprise(
    id: number,
    data: UpdateEnterpriseParams
  ): Promise<BaseResponse<EnterpriseItem>> {
    return this.put<BaseResponse<EnterpriseItem>>(`/api/admin/enterprises/${id}`, data)
  }

  /**
   * 修改企业账号密码
   */
  static updateEnterprisePassword(
    id: number,
    data: UpdateEnterprisePasswordParams
  ): Promise<BaseResponse> {
    return this.put<BaseResponse>(`/api/admin/enterprises/${id}/password`, data)
  }

  /**
   * 启用/禁用企业
   */
  static updateEnterpriseStatus(
    id: number,
    data: UpdateEnterpriseStatusParams
  ): Promise<BaseResponse> {
    return this.put<BaseResponse>(`/api/admin/enterprises/${id}/status`, data)
  }

  // ========== 企业卡授权相关 ==========

  /**
   * 授权卡给企业
   * @param enterpriseId 企业ID
   * @param data 授权请求数据
   */
  static allocateCards(
    enterpriseId: number,
    data: AllocateCardsRequest
  ): Promise<BaseResponse<AllocateCardsResponse>> {
    return this.post<BaseResponse<AllocateCardsResponse>>(
      `/api/admin/enterprises/${enterpriseId}/allocate-cards`,
      data
    )
  }

  /**
   * 卡授权预检
   * @param enterpriseId 企业ID
   * @param data 预检请求数据
   */
  static previewAllocateCards(
    enterpriseId: number,
    data: AllocateCardsPreviewRequest
  ): Promise<BaseResponse<AllocateCardsPreviewResponse>> {
    return this.post<BaseResponse<AllocateCardsPreviewResponse>>(
      `/api/admin/enterprises/${enterpriseId}/allocate-cards/preview`,
      data
    )
  }

  /**
   * 获取企业卡列表
   * @param enterpriseId 企业ID
   * @param params 查询参数
   */
  static getEnterpriseCards(
    enterpriseId: number,
    params?: EnterpriseCardListParams
  ): Promise<BaseResponse<EnterpriseCardPageResult>> {
    return this.get<BaseResponse<EnterpriseCardPageResult>>(
      `/api/admin/enterprises/${enterpriseId}/cards`,
      params
    )
  }

  /**
   * 复机卡
   * @param enterpriseId 企业ID
   * @param cardId 卡ID
   */
  static resumeCard(enterpriseId: number, cardId: number): Promise<BaseResponse> {
    return this.post<BaseResponse>(`/api/admin/enterprises/${enterpriseId}/cards/${cardId}/resume`)
  }

  /**
   * 停机卡
   * @param enterpriseId 企业ID
   * @param cardId 卡ID
   */
  static suspendCard(enterpriseId: number, cardId: number): Promise<BaseResponse> {
    return this.post<BaseResponse>(`/api/admin/enterprises/${enterpriseId}/cards/${cardId}/suspend`)
  }

  /**
   * 回收卡授权
   * @param enterpriseId 企业ID
   * @param data 回收请求数据
   */
  static recallCards(
    enterpriseId: number,
    data: RecallCardsRequest
  ): Promise<BaseResponse<RecallCardsResponse>> {
    return this.post<BaseResponse<RecallCardsResponse>>(
      `/api/admin/enterprises/${enterpriseId}/recall-cards`,
      data
    )
  }

  // ========== 企业设备授权相关 ==========

  /**
   * 授权设备给企业
   * @param enterpriseId 企业ID
   * @param data 授权请求数据
   */
  static allocateDevices(
    enterpriseId: number,
    data: EnterpriseAllocateDevicesRequest
  ): Promise<BaseResponse<EnterpriseAllocateDevicesResponse>> {
    return this.post<BaseResponse<EnterpriseAllocateDevicesResponse>>(
      `/api/admin/enterprises/${enterpriseId}/allocate-devices`,
      data
    )
  }

  /**
   * 获取企业设备列表
   * @param enterpriseId 企业ID
   * @param params 查询参数
   */
  static getEnterpriseDevices(
    enterpriseId: number,
    params?: EnterpriseDeviceListParams
  ): Promise<BaseResponse<EnterpriseDevicePageResult>> {
    return this.get<BaseResponse<EnterpriseDevicePageResult>>(
      `/api/admin/enterprises/${enterpriseId}/devices`,
      params
    )
  }

  /**
   * 撤销设备授权
   * @param enterpriseId 企业ID
   * @param data 撤销请求数据
   */
  static recallDevices(
    enterpriseId: number,
    data: EnterpriseRecallDevicesRequest
  ): Promise<BaseResponse<EnterpriseRecallDevicesResponse>> {
    return this.post<BaseResponse<EnterpriseRecallDevicesResponse>>(
      `/api/admin/enterprises/${enterpriseId}/recall-devices`,
      data
    )
  }
}
