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

// --- Types ---
export interface Alert {
  id: string;
  patient_id: string;
  patient_name?: string;
  rule_id: string;
  severity: string;
  title: string;
  detail?: Record<string, unknown>;
  status: string;
  acknowledged_by?: string;
  acknowledged_by_name?: string;
  acknowledged_at?: string;
  resolved_at?: string;
  created_at: string;
  version: number;
}

export interface AlertRule {
  id: string;
  name: string;
  description?: string;
  device_type: string;
  condition_type: string;
  condition_params: Record<string, unknown>;
  severity: string;
  is_active: boolean;
  apply_tags?: Record<string, unknown>;
  notify_roles: unknown[];
  cooldown_minutes: number;
  created_at: string;
  updated_at: string;
  version: number;
}

export interface CreateAlertRuleReq {
  name: string;
  description?: string;
  device_type: string;
  condition_type: string;
  condition_params: Record<string, unknown>;
  severity?: string;
  apply_tags?: Record<string, unknown>;
  notify_roles?: unknown[];
  cooldown_minutes?: number;
}

export interface UpdateAlertRuleReq {
  name?: string;
  description?: string;
  condition_params?: Record<string, unknown>;
  severity?: string;
  apply_tags?: Record<string, unknown>;
  notify_roles?: unknown[];
  cooldown_minutes?: number;
  version: number;
}

// --- API ---
export const alertApi = {
  list: (params?: { patient_id?: string; doctor_id?: string; status?: string; page?: number; page_size?: number }) =>
    client.get('/health/alerts', { params }).then((r) => r.data.data as PaginatedResponse<Alert>),

  acknowledge: (id: string, version: number) =>
    client.put(`/health/alerts/${id}/acknowledge`, { version }).then((r) => r.data.data as Alert),

  dismiss: (id: string, version: number) =>
    client.put(`/health/alerts/${id}/dismiss`, { version }).then((r) => r.data.data as Alert),

  resolve: (id: string, version: number) =>
    client.put(`/health/alerts/${id}/resolve`, { version }).then((r) => r.data.data as Alert),
};

export const alertRuleApi = {
  list: (params?: { device_type?: string; page?: number; page_size?: number }) =>
    client.get('/health/alert-rules', { params }).then((r) => r.data.data as PaginatedResponse<AlertRule>),

  create: (data: CreateAlertRuleReq) =>
    client.post('/health/alert-rules', data).then((r) => r.data.data as AlertRule),

  update: (id: string, data: UpdateAlertRuleReq) =>
    client.put(`/health/alert-rules/${id}`, data).then((r) => r.data.data as AlertRule),

  deactivate: (id: string, version: number) =>
    client.put(`/health/alert-rules/${id}/deactivate`, { version }).then((r) => r.data.data as AlertRule),
};

// --- Critical Alerts API ---

export interface CriticalAlert {
  id: string;
  patient_id: string;
  patient_name?: string;
  alert_type: string;
  severity: string;
  title: string;
  detail?: Record<string, unknown>;
  status: string;
  acknowledged_by?: string;
  acknowledged_at?: string;
  notes?: string;
  created_at: string;
  version: number;
}

export const criticalAlertApi = {
  list: (params?: { page?: number; page_size?: number }) =>
    client.get('/health/critical-alerts', { params }).then((r) => r.data.data as PaginatedResponse<CriticalAlert>),

  get: (id: string) =>
    client.get(`/health/critical-alerts/${id}`).then((r) => r.data.data as CriticalAlert),

  acknowledge: (id: string, req: { notes?: string }) =>
    client.post(`/health/critical-alerts/${id}/acknowledge`, req).then((r) => r.data),
};
