import { http } from "@/utils/http";

type Result = {
  code: number;
  message: string;
  data?: any;
};

type ResultTable = {
  code: number;
  message: string;
  data?: {
    list: Array<any>;
    total: number;
    pageSize: number;
    currentPage: number;
  };
};

export const getDeviceList = (params: {
  page?: number;
  pageSize?: number;
  deviceId?: string;
  shopId?: number;
  status?: number;
  storeName?: string;
}) => {
  return http.request<ResultTable>("get", "/devices/list", { params });
};

export const getDeviceById = (id: number) => {
  return http.request<Result>("get", `/devices/${id}`);
};

export const getDeviceStatistics = () => {
  return http.request<Result>("get", "/devices/statistics");
};

export const openDoor = (id: number, data: { doorIndex?: string }) => {
  return http.request<Result>("post", `/devices/${id}/open`, { data });
};

export const setTemperature = (id: number, data: { temperature: number }) => {
  return http.request<Result>("put", `/devices/${id}/temperature`, { data });
};

export const setVolume = (id: number, data: { volume: number }) => {
  return http.request<Result>("put", `/devices/${id}/volume`, { data });
};

export interface DoorRecordItem {
  id: number;
  activityId: string;
  deviceId: string;
  userId: number;
  doorIndex?: string;
  openType: string;
  doorStatus: string;
  nobuy: number;
  orderId?: number;
  openTime: string;
  closeTime?: string;
  duration?: number;
  source?: string;
  remark?: string;
}

export const getDoorRecords = (deviceId: string, params: { page?: number; pageSize?: number }) => {
  return http.request<ResultTable>("get", `/devices/${deviceId}/door-records`, { params });
};

// ==================== 补货员管理 ====================

export const getDeviceReplenishers = (deviceId: string) => {
  return http.request<Result>("get", `/devices/${deviceId}/replenishers`);
};

export const bindDeviceReplenisher = (deviceId: string, data: { replenisherId: number }) => {
  return http.request<Result>("post", `/devices/${deviceId}/replenishers`, { data });
};

export const unbindDeviceReplenisher = (deviceId: string, replenisherId: number) => {
  return http.request<Result>("delete", `/devices/${deviceId}/replenishers/${replenisherId}`);
};

// 查询设备详细状态（含门/锁/摄像头/网络状态）
export const getDeviceDetailStatus = (deviceId: string) => {
  return http.request<Result>("get", `/devices/${deviceId}/detail-status`);
};
