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

// --- Types ---
export interface PatientListItem {
  id: string;
  name: string;
  gender?: string;
  birth_date?: string;
  blood_type?: string;
  status: string;
  verification_status: string;
  source?: string;
  created_at: string;
  updated_at: string;
  version: number;
}

export interface PatientDetail {
  id: string;
  user_id?: string;
  name: string;
  gender?: string;
  birth_date?: string;
  blood_type?: string;
  id_number?: string;
  allergy_history?: string;
  medical_history_summary?: string;
  emergency_contact_name?: string;
  emergency_contact_phone?: string;
  status: string;
  verification_status: string;
  source?: string;
  notes?: string;
  created_at: string;
  updated_at: string;
  version: number;
}

export interface CreatePatientReq {
  name: string;
  gender?: string;
  birth_date?: string;
  blood_type?: string;
  id_number?: string;
  allergy_history?: string;
  medical_history_summary?: string;
  emergency_contact_name?: string;
  emergency_contact_phone?: string;
  source?: string;
  notes?: string;
}

export interface UpdatePatientReq extends Partial<CreatePatientReq> {
  status?: string;
  verification_status?: string;
}

export interface FamilyMember {
  id: string;
  name: string;
  relationship: string;
  phone?: string;
  id_number?: string;
  notes?: string;
  created_at: string;
  updated_at: string;
  version: number;
}

export interface CreateFamilyMemberReq {
  name: string;
  relationship: string;
  phone?: string;
  id_number?: string;
  notes?: string;
}

export interface TagItem {
  id: string;
  name: string;
  color: string | null;
  description: string | null;
}

// --- API ---
export const patientApi = {
  list: async (params: {
    page?: number;
    page_size?: number;
    search?: string;
    status?: string;
    tag_id?: string;
  }) => {
    const { data } = await client.get<{
      success: boolean;
      data: PaginatedResponse<PatientListItem>;
    }>('/health/patients', { params });
    return data.data;
  },

  get: async (id: string) => {
    const { data } = await client.get<{
      success: boolean;
      data: PatientDetail;
    }>(`/health/patients/${id}`);
    return data.data;
  },

  create: async (req: CreatePatientReq) => {
    const { data } = await client.post<{
      success: boolean;
      data: PatientDetail;
    }>('/health/patients', req);
    return data.data;
  },

  update: async (id: string, req: UpdatePatientReq & { version: number }) => {
    const { data } = await client.put<{
      success: boolean;
      data: PatientDetail;
    }>(`/health/patients/${id}`, req);
    return data.data;
  },

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

  manageTags: async (id: string, tagIds: string[]) => {
    await client.post(`/health/patients/${id}/tags`, { tag_ids: tagIds });
  },

  listFamilyMembers: async (id: string) => {
    const { data } = await client.get<{
      success: boolean;
      data: FamilyMember[];
    }>(`/health/patients/${id}/family-members`);
    return data.data;
  },

  createFamilyMember: async (id: string, req: CreateFamilyMemberReq) => {
    const { data } = await client.post<{
      success: boolean;
      data: FamilyMember;
    }>(`/health/patients/${id}/family-members`, req);
    return data.data;
  },

  updateFamilyMember: async (
    patientId: string,
    memberId: string,
    req: Partial<CreateFamilyMemberReq> & { version: number },
  ) => {
    const { data } = await client.put<{
      success: boolean;
      data: FamilyMember;
    }>(`/health/patients/${patientId}/family-members/${memberId}`, req);
    return data.data;
  },

  deleteFamilyMember: async (patientId: string, memberId: string) => {
    await client.delete(
      `/health/patients/${patientId}/family-members/${memberId}`,
    );
  },

  listTags: async () => {
    const { data } = await client.get<{
      success: boolean;
      data: TagItem[];
    }>('/health/patient-tags');
    return data.data;
  },
};
