import { useState } from "react";
import { bookAppointment, AppointmentPayload } from "@/services/appointmentApi";

export function useAppointment() {
  const [loading, setLoading] = useState(false);
  const [error, setError]     = useState<string | null>(null);
  const [success, setSuccess] = useState(false);

  const submitAppointment = async (payload: AppointmentPayload) => {
    setLoading(true);
    setError(null);
    setSuccess(false);

    const { data, error } = await bookAppointment(payload);

    if (error) {
      setError(error);
    } else {
      setSuccess(true);
    }

    setLoading(false);
    return { data, error };
  };

  return { submitAppointment, loading, error, success };
}