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

// --- Types ---

export interface DialysisRecord {
  id: string;
  patient_id: string;
  dialysis_date: string;
  start_time?: string;
  end_time?: string;
  dry_weight?: number;
  pre_weight?: number;
  post_weight?: number;
  pre_bp_systolic?: number;
  pre_bp_diastolic?: number;
  post_bp_systolic?: number;
  post_bp_diastolic?: number;
  pre_heart_rate?: number;
  post_heart_rate?: number;
  ultrafiltration_volume?: number;
  dialysis_duration?: number;
  blood_flow_rate?: number;
  dialysis_type: string;
  symptoms?: Record<string, unknown>;
  complication_notes?: string;
  status: string;
  reviewed_by?: string;
  reviewed_at?: string;
  created_at: string;
  updated_at: string;
  version: number;
}

export interface CreateDialysisRecordReq {
  patient_id: string;
  dialysis_date: string;
  start_time?: string;
  end_time?: string;
  dry_weight?: number;
  pre_weight?: number;
  post_weight?: number;
  pre_bp_systolic?: number;
  pre_bp_diastolic?: number;
  post_bp_systolic?: number;
  post_bp_diastolic?: number;
  pre_heart_rate?: number;
  post_heart_rate?: number;
  ultrafiltration_volume?: number;
  dialysis_duration?: number;
  blood_flow_rate?: number;
  dialysis_type?: string;
  complication_notes?: string;
}

// --- API ---

export const dialysisApi = {
  listRecords: async (
    patientId: string,
    params: { page?: number; page_size?: number },
  ) => {
    const { data } = await client.get<{
      success: boolean;
      data: PaginatedResponse<DialysisRecord>;
    }>(`/health/patients/${patientId}/dialysis-records`, { params });
    return data.data;
  },

  getRecord: async (id: string) => {
    const { data } = await client.get<{
      success: boolean;
      data: DialysisRecord;
    }>(`/health/dialysis-records/${id}`);
    return data.data;
  },

  createRecord: async (req: CreateDialysisRecordReq) => {
    const { data } = await client.post<{
      success: boolean;
      data: DialysisRecord;
    }>('/health/dialysis-records', req);
    return data.data;
  },

  updateRecord: async (
    id: string,
    req: Partial<CreateDialysisRecordReq> & { version: number },
  ) => {
    const { data } = await client.put<{
      success: boolean;
      data: DialysisRecord;
    }>(`/health/dialysis-records/${id}`, req);
    return data.data;
  },

  deleteRecord: async (id: string, version: number) => {
    await client.delete(`/health/dialysis-records/${id}`, { data: { version } });
  },

  reviewRecord: async (id: string, req: { version: number; doctor_notes?: string }) => {
    const { data } = await client.put<{
      success: boolean;
      data: Record<string, unknown>;
    }>(`/health/dialysis-records/${id}/review`, req);
    return data.data;
  },

  completeRecord: async (id: string, version: number) => {
    const { data } = await client.put<{
      success: boolean;
      data: DialysisRecord;
    }>(`/health/dialysis-records/${id}/complete`, { version });
    return data.data;
  },
};
