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

// --- Types ---
export interface Appointment {
  id: string;
  patient_id: string;
  doctor_id?: string;
  appointment_type: string;
  appointment_date: string;
  start_time: string;
  end_time: string;
  status: string;
  cancel_reason?: string;
  notes?: string;
  patient_name?: string;
  doctor_name?: string;
  created_at: string;
  updated_at: string;
  version: number;
}

export interface CreateAppointmentReq {
  patient_id: string;
  doctor_id?: string;
  appointment_type?: string;
  appointment_date: string;
  start_time: string;
  end_time: string;
  notes?: string;
}

export interface UpdateAppointmentStatusReq {
  status: string;
  cancel_reason?: string;
}

export interface Schedule {
  id: string;
  doctor_id: string;
  schedule_date: string;
  period_type: string;
  start_time: string;
  end_time: string;
  max_appointments: number;
  current_appointments: number;
  status: string;
  created_at: string;
  updated_at: string;
  version: number;
}

export interface CreateScheduleReq {
  doctor_id: string;
  schedule_date: string;
  period_type?: string;
  start_time: string;
  end_time: string;
  max_appointments: number;
}

export interface UpdateScheduleReq {
  start_time?: string;
  end_time?: string;
  max_appointments?: number;
  status?: string;
}

export interface CalendarDay {
  date: string;
  schedules: Schedule[];
}

// --- API ---
export const appointmentApi = {
  list: async (params: {
    page?: number;
    page_size?: number;
    status?: string;
    patient_id?: string;
    doctor_id?: string;
    date?: string;
    search?: string;
    appointment_type?: string;
  }) => {
    const { data } = await client.get<{
      success: boolean;
      data: PaginatedResponse<Appointment>;
    }>('/health/appointments', { params });
    return data.data;
  },

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

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

  updateStatus: async (
    id: string,
    req: UpdateAppointmentStatusReq & { version: number },
  ) => {
    const { data } = await client.put<{
      success: boolean;
      data: Appointment;
    }>(`/health/appointments/${id}/status`, req);
    return data.data;
  },

  // Schedules
  listSchedules: async (params: {
    page?: number;
    page_size?: number;
    doctor_id?: string;
    date?: string;
  }) => {
    const { data } = await client.get<{
      success: boolean;
      data: PaginatedResponse<Schedule>;
    }>('/health/doctor-schedules', { params });
    return data.data;
  },

  createSchedule: async (req: CreateScheduleReq) => {
    const { data } = await client.post<{
      success: boolean;
      data: Schedule;
    }>('/health/doctor-schedules', req);
    return data.data;
  },

  updateSchedule: async (
    id: string,
    req: UpdateScheduleReq & { version: number },
  ) => {
    const { data } = await client.put<{
      success: boolean;
      data: Schedule;
    }>(`/health/doctor-schedules/${id}`, req);
    return data.data;
  },

  calendar: async (params: {
    start_date: string;
    end_date: string;
    doctor_id?: string;
  }) => {
    const { data } = await client.get<{
      success: boolean;
      data: CalendarDay[];
    }>('/health/doctor-schedules/calendar', { params });
    return data.data;
  },
};
