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

// --- Types ---
export interface Doctor {
  id: string;
  user_id?: string;
  name: string;
  department?: string;
  title?: string;
  specialty?: string;
  license_number?: string;
  bio?: string;
  online_status: string;
  created_at: string;
  updated_at: string;
  version: number;
}

export interface CreateDoctorReq {
  user_id?: string;
  name: string;
  department?: string;
  title?: string;
  specialty?: string;
  license_number?: string;
  bio?: string;
}

export interface UpdateDoctorReq {
  name?: string;
  department?: string;
  title?: string;
  specialty?: string;
  license_number?: string;
  bio?: string;
  online_status?: string;
}

// --- API ---
export const doctorApi = {
  list: async (params: {
    page?: number;
    page_size?: number;
    search?: string;
    department?: string;
    title?: string;
  }) => {
    const { data } = await client.get<{
      success: boolean;
      data: PaginatedResponse<Doctor>;
    }>('/health/doctors', { params });
    return data.data;
  },

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

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

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

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