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

// --- Types ---
export interface FollowUpTask {
  id: string;
  patient_id: string;
  assigned_to?: string;
  patient_name?: string;
  assigned_to_name?: string;
  follow_up_type: string;
  planned_date: string;
  status: string;
  content_template?: string;
  related_appointment_id?: string;
  created_at: string;
  updated_at: string;
  version: number;
}

export interface CreateFollowUpTaskReq {
  patient_id: string;
  assigned_to?: string;
  follow_up_type: string;
  planned_date: string;
  content_template?: string;
  related_appointment_id?: string;
}

export interface UpdateFollowUpTaskReq {
  assigned_to?: string;
  follow_up_type?: string;
  planned_date?: string;
  content_template?: string;
  status?: string;
}

export interface FollowUpRecord {
  id: string;
  task_id: string;
  executed_by?: string;
  executed_date: string;
  result?: string;
  patient_condition?: string;
  medical_advice?: string;
  next_follow_up_date?: string;
  created_at: string;
  updated_at: string;
  version: number;
}

export interface CreateFollowUpRecordReq {
  task_id: string;
  executed_by?: string;
  executed_date: string;
  result?: string;
  patient_condition?: string;
  medical_advice?: string;
  next_follow_up_date?: string;
}

// --- API ---
export const followUpApi = {
  // Tasks
  listTasks: async (params: {
    page?: number;
    page_size?: number;
    patient_id?: string;
    assigned_to?: string;
    status?: string;
  }) => {
    const { data } = await client.get<{
      success: boolean;
      data: PaginatedResponse<FollowUpTask>;
    }>('/health/follow-up-tasks', { params });
    return data.data;
  },

  getTask: async (id: string) => {
    const { data } = await client.get<{
      success: boolean;
      data: FollowUpTask;
    }>(`/health/follow-up-tasks/${id}`);
    return data.data;
  },

  createTask: async (req: CreateFollowUpTaskReq) => {
    const { data } = await client.post<{
      success: boolean;
      data: FollowUpTask;
    }>('/health/follow-up-tasks', req);
    return data.data;
  },

  updateTask: async (
    id: string,
    req: UpdateFollowUpTaskReq & { version: number },
  ) => {
    const { data } = await client.put<{
      success: boolean;
      data: FollowUpTask;
    }>(`/health/follow-up-tasks/${id}`, req);
    return data.data;
  },

  deleteTask: async (id: string, version: number) => {
    await client.delete(`/health/follow-up-tasks/${id}`, {
      data: { version },
    });
  },

  // Records
  listRecords: async (params: {
    page?: number;
    page_size?: number;
    task_id?: string;
    patient_id?: string;
  }) => {
    const { data } = await client.get<{
      success: boolean;
      data: PaginatedResponse<FollowUpRecord>;
    }>('/health/follow-up-records', { params });
    return data.data;
  },

  createRecord: async (taskId: string, req: Omit<CreateFollowUpRecordReq, 'task_id'>) => {
    const { data } = await client.post<{
      success: boolean;
      data: FollowUpRecord;
    }>(`/health/follow-up-tasks/${taskId}/records`, { ...req, task_id: taskId });
    return data.data;
  },
};
