import { type AxiosError, type AxiosResponse } from "axios";
import Appointment from "@/ApiClasses/Appointment";
import { axiosInstance, defaultApiErrorHandler, defaultApiSuccessHandler } from "@/services/api"


// Shared API
function getSectionCellsByDate(
    projectId: number,
    sectionId: number,
    date: string, // Use string for date in 'YYYY-MM-DD HH:mm' format
    onSuccessHandler?: (response: AxiosResponse) => void,
    onErrorHandler?: (error: AxiosError) => void
): void {
    axiosInstance
        .get(`/shared/projects/sectionCells/${projectId}/${sectionId}/${date}`)
        .then((response) => {
            if (onSuccessHandler) {
                onSuccessHandler(response);
            } else {
                defaultApiSuccessHandler(response);
            }
        })
        .catch((error: AxiosError) => {
            if (onErrorHandler) {
                onErrorHandler(error);
            } else {
                defaultApiErrorHandler(error);
            }
        });
}

function getProjectEntrepreneurs(
    projectId: number,
    onSuccessHandler?: (response: AxiosResponse) => void,
    onErrorHandler?: (error: AxiosError) => void
): void {
    axiosInstance
        .get(`/shared/projects/entrepreneurs/${projectId}`)
        .then((response) => {
            if (onSuccessHandler) {
                onSuccessHandler(response);
            } else {
                defaultApiSuccessHandler(response);
            }
        })
        .catch((error: AxiosError) => {
            if (onErrorHandler) {
                onErrorHandler(error);
            } else {
                defaultApiErrorHandler(error);
            }
        });
}

function getProjectAdmin(
    projectId: number,
    onSuccessHandler?: (response: AxiosResponse) => void,
    onErrorHandler?: (error: AxiosError) => void
): void {
    axiosInstance
        .get(`/shared/projects/admin/${projectId}`)
        .then((response) => {
            if (onSuccessHandler) {
                onSuccessHandler(response);
            } else {
                defaultApiSuccessHandler(response);
            }
        })
        .catch((error: AxiosError) => {
            if (onErrorHandler) {
                onErrorHandler(error);
            } else {
                defaultApiErrorHandler(error);
            }
        });
}

function getProjectAppointments(
    projectId: number,
    onSuccessHandler?: (response: AxiosResponse) => void,
    onErrorHandler?: (error: AxiosError) => void
): void {
    axiosInstance
        .get(`/shared/projects/appointments/${projectId}`)
        .then((response) => {
            if (onSuccessHandler) {
                onSuccessHandler(response);
            } else {
                defaultApiSuccessHandler(response);
            }
        })
        .catch((error: AxiosError) => {
            if (onErrorHandler) {
                onErrorHandler(error);
            } else {
                defaultApiErrorHandler(error);
            }
        });
}

function getAppointmentReport(
    appointmentId: number,
    onSuccessHandler?: (response: AxiosResponse) => void,
    onErrorHandler?: (error: AxiosError) => void
): void {
    axiosInstance
        .get(`/shared/appointments/report/${appointmentId}`)
        .then((response) => {
            if (onSuccessHandler) {
                onSuccessHandler(response);
            } else {
                defaultApiSuccessHandler(response);
            }
        })
        .catch((error: AxiosError) => {
            if (onErrorHandler) {
                onErrorHandler(error);
            } else {
                defaultApiErrorHandler(error);
            }
        });
}

function requestAppointment(
    appointmentDetails: Appointment, // Replace 'any' with a proper type for appointment details if available
    onSuccessHandler?: (response: AxiosResponse) => void,
    onErrorHandler?: (error: AxiosError) => void
): void {
    axiosInstance
        .post("/shared/appointments/request", appointmentDetails)
        .then((response) => {
            if (onSuccessHandler) {
                onSuccessHandler(response);
            } else {
                defaultApiSuccessHandler(response);
            }
        })
        .catch((error: AxiosError) => {
            if (onErrorHandler) {
                onErrorHandler(error);
            } else {
                defaultApiErrorHandler(error);
            }
        });
}

export {
    getSectionCellsByDate,
    getProjectEntrepreneurs,
    getProjectAdmin,
    getProjectAppointments,
    getAppointmentReport,
    requestAppointment,
};