import client from '../client';
import type { PaginatedResponse } from '../types';

// --- Types ---

export interface BleGateway {
  id: string;
  tenant_id: string;
  gateway_id: string;
  name: string;
  status: string;
  firmware_version?: string;
  ip_address?: string;
  last_heartbeat_at?: string;
  metadata?: Record<string, unknown>;
  created_at: string;
  updated_at: string;
  version: number;
  api_key?: string;
  patient_count?: number;
}

export interface GatewayBinding {
  id: string;
  tenant_id: string;
  gateway_id: string;
  patient_id: string;
  peripheral_mac?: string;
  device_type?: string;
  status: string;
  created_at: string;
  updated_at: string;
  version: number;
}

export interface CreateBleGatewayReq {
  gateway_id: string;
  name: string;
  firmware_version?: string;
  metadata?: Record<string, unknown>;
}

export interface UpdateBleGatewayReq {
  name?: string;
  status?: string;
  firmware_version?: string;
  metadata?: Record<string, unknown>;
}

export interface ListBleGatewaysParams {
  page?: number;
  page_size?: number;
  status?: string;
}

export interface CreateBindingReq {
  patient_id: string;
  peripheral_mac?: string;
  device_type?: string;
}

export interface BatchBindReq {
  bindings: CreateBindingReq[];
}

// --- Constants ---

export const GATEWAY_STATUS_OPTIONS = [
  { label: '在线', value: 'online' },
  { label: '离线', value: 'offline' },
  { label: '未激活', value: 'inactive' },
  { label: '已禁用', value: 'disabled' },
];

export const GATEWAY_STATUS_COLOR: Record<string, string> = {
  online: 'green',
  offline: 'red',
  inactive: 'default',
  disabled: 'error',
};

export const GATEWAY_STATUS_LABEL: Record<string, string> = Object.fromEntries(
  GATEWAY_STATUS_OPTIONS.map((o) => [o.value, o.label]),
);

export const BINDING_STATUS_COLOR: Record<string, string> = {
  active: 'green',
  inactive: 'default',
  unbound: 'error',
};

// --- API ---

export const bleGatewayApi = {
  // --- Gateways ---

  list: async (params?: ListBleGatewaysParams) => {
    const { data } = await client.get<{
      success: boolean;
      data: PaginatedResponse<BleGateway>;
    }>('/health/ble-gateways', { params });
    return data.data;
  },

  get: async (gatewayId: string) => {
    const { data } = await client.get<{
      success: boolean;
      data: BleGateway;
    }>(`/health/ble-gateways/${gatewayId}`);
    return data.data;
  },

  create: async (req: CreateBleGatewayReq) => {
    const { data } = await client.post<{
      success: boolean;
      data: BleGateway;
    }>('/health/ble-gateways', req);
    return data.data;
  },

  update: async (gatewayId: string, req: UpdateBleGatewayReq & { version: number }) => {
    const { data } = await client.put<{
      success: boolean;
      data: BleGateway;
    }>(`/health/ble-gateways/${gatewayId}`, req);
    return data.data;
  },

  delete: async (gatewayId: string, version: number) => {
    await client.delete(`/health/ble-gateways/${gatewayId}`, { data: { version } });
  },

  regenerateKey: async (gatewayId: string) => {
    const { data } = await client.post<{
      success: boolean;
      data: BleGateway;
    }>(`/health/ble-gateways/${gatewayId}/regenerate-key`);
    return data.data;
  },

  // --- Bindings ---

  listBindings: async (gatewayId: string, params?: { page?: number; page_size?: number }) => {
    const { data } = await client.get<{
      success: boolean;
      data: PaginatedResponse<GatewayBinding>;
    }>(`/health/ble-gateways/${gatewayId}/bindings`, { params });
    return data.data;
  },

  bindPatient: async (gatewayId: string, req: CreateBindingReq) => {
    const { data } = await client.post<{
      success: boolean;
      data: GatewayBinding;
    }>(`/health/ble-gateways/${gatewayId}/bindings`, req);
    return data.data;
  },

  batchBind: async (gatewayId: string, req: BatchBindReq) => {
    const { data } = await client.post<{
      success: boolean;
      data: GatewayBinding[];
    }>(`/health/ble-gateways/${gatewayId}/bindings/batch`, req);
    return data.data;
  },

  unbindPatient: async (gatewayId: string, bindingId: string, version: number) => {
    await client.delete(`/health/ble-gateways/${gatewayId}/bindings/${bindingId}`, { data: { version } });
  },
};
