/**
 * H5 用户端 API 服务
 * 封装所有 H5 端接口调用
 */
import { api } from '@/api/request';

// ─── 数据类型 ───

export interface DashboardData {
  online_cabinets: number;
  charging_count: number;
  idle_channels: number;
  fault_channels: number;
  today_charge_count: number;
  today_charge_hours: number;
  today_energy_kwh: number;
  alerts: AlertItem[];
  projects: ProjectSummary[];
}

export interface AlertItem {
  project: string;
  cabinet: string;
  channel: number;
  alert_type: string;
}

export interface ProjectSummary {
  id: number;
  name: string;
  cabinet_count: number;
  charging: number;
  idle: number;
  fault: number;
}

export interface H5Project {
  id: number;
  name: string;
}

export interface H5Cabinet {
  id: number;
  abstract_id: string;
  name?: string;
  status: number;       // 0=离线 1=在线
  project_id: number;
}

export interface CompartmentStatus {
  id: number;
  channel_id: number;
  status: number;       // 0=空闲 1=充电中 2=故障 3=离线
  voltage?: number;
  current?: number;
  power?: number;
  energy?: number;
  soc?: number;         // 荷电状态 %
  soh?: number;         // 健康度 %
  cell_voltages?: number[];
  cell_temps?: number[];
  bms_alerts?: string[];
}

export interface CabinBoardStatus {
  id: number;
  board_id: number;
  status: number;
  compartments: CompartmentStatus[];
}

export interface CabinetDetail {
  id: number;
  abstract_id: string;
  imei: string;
  name?: string;
  status: number;
  cabin_boards: CabinBoardStatus[];
}

export interface CompartmentDetail {
  id: number;
  channel_id: number;
  status: number;
  voltage: number;
  current: number;
  power: number;
  energy: number;
  soc: number;
  soh: number;
  cell_voltages: number[];
  cell_temps: number[];
  bms_alerts: string[];
  charge_records: ChargeRecord[];
}

export interface ChargeRecord {
  id: number;
  start_time: string;
  end_time?: string;
  energy_kwh: number;
  cost: number;
}

export interface H5User {
  id: number;
  phone: string;
  name: string;
}

export interface H5LoginResponse {
  token: string;
  user: H5User;
}

// ─── API 函数 ───

export const h5Api = {
  // 认证
  login: (data: { phone: string; password: string }) =>
    api.post<H5LoginResponse>('/h5/auth/login', data),

  // 首页仪表盘
  getDashboard: () => api.get<DashboardData>('/h5/dashboard'),

  // 项目列表
  getProjects: () => api.get<H5Project[]>('/h5/projects'),

  // 设备列表（按项目）
  getCabinets: (projectId?: number) =>
    api.get<H5Cabinet[]>(`/h5/cabinets${projectId ? `?project_id=${projectId}` : ''}`),

  // 设备详情（含仓板+仓体状态）
  getCabinetDetail: (id: number) =>
    api.get<CabinetDetail>(`/h5/cabinets/${id}`),

  // 通过 abstract_id 获取设备详情（扫码）
  getCabinetByAbstractId: (abstractId: string) =>
    api.get<CabinetDetail>(`/h5/cabinets/abstract/${abstractId}`),

  // 仓体/通道详情
  getCompartmentDetail: (id: number) =>
    api.get<CompartmentDetail>(`/h5/compartments/${id}`),

  // 充电控制
  startCharge: (compartmentId: number) =>
    api.post<{ success: boolean }>('/h5/charge/start', { compartment_id: compartmentId }),
  stopCharge: (compartmentId: number) =>
    api.post<{ success: boolean }>('/h5/charge/stop', { compartment_id: compartmentId }),

  // 开门
  openDoor: (compartmentId: number) =>
    api.post<{ success: boolean }>('/h5/door/open', { compartment_id: compartmentId }),
};
