// export interface CalendarEvent {
//     name: string,
//     start: Date,
//     end: Date,
//     location: string,
//     color: string,
//     description: string,
//     creator: any | null, // user object
//     id: number | null
// }

export enum CalendarAttendance {
    Attending = "attending",
    NotAttending = "not_attending",
    Maybe = "maybe"
}

export interface CalendarSignup {
    memberID: number,
    eventID: number,
    state: CalendarAttendance
}

import { CalendarEventShort, CalendarEvent } from "@shared/types/calendar";

//@ts-ignore
const addr = import.meta.env.VITE_APIHOST;

export async function getMonthCalendarEvents(viewedMonth: Date): Promise<CalendarEventShort[]> {

    const year = viewedMonth.getFullYear();
    const month = viewedMonth.getMonth();

    // Base range: first and last day of the month
    const firstOfMonth = new Date(year, month, 1);
    const lastOfMonth = new Date(year, month + 1, 0);

    // --- Apply 10 day padding ---
    const start = new Date(firstOfMonth);
    start.setDate(start.getDate() - 10);

    const end = new Date(lastOfMonth);
    end.setDate(end.getDate() + 10);
    end.setHours(23, 59, 59, 999);

    const from = start.toISOString();
    const to = end.toISOString();

    const url = `${addr}/calendar?from=${encodeURIComponent(from)}&to=${encodeURIComponent(to)}`;

    const res = await fetch(url);

    if (!res.ok) {
        throw new Error(`Failed to fetch events: ${res.status} ${res.statusText}`);
    }

    return res.json();
}

export async function getCalendarEvent(id: number): Promise<CalendarEvent> {
    let res = await fetch(`${addr}/calendar/${id}`);

    if (res.ok) {
        return await res.json();
    } else {
        throw new Error(`Failed to fetch event: ${res.status} ${res.statusText}`);
    }
}

export async function createCalendarEvent(eventData: CalendarEvent) {
    let res = await fetch(`${addr}/calendar`, {
        method: "POST",
        credentials: "include",
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(eventData)
    });

    if (res.ok) {
        return;
    } else {
        throw new Error(`Failed to set attendance: ${res.status} ${res.statusText}`);
    }
}

export async function editCalendarEvent(eventData: CalendarEvent) {
    let res = await fetch(`${addr}/calendar`, {
        method: "PUT",
        credentials: "include",
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(eventData)
    });

    if (res.ok) {
        return;
    } else {
        throw new Error(`Failed to set attendance: ${res.status} ${res.statusText}`);
    }
}

export async function setCancelCalendarEvent(eventID: number, cancel: boolean) {
    let route = cancel ? "cancel" : "uncancel";

    console.log(route);
    let res = await fetch(`${addr}/calendar/${eventID}/${route}`, {
        method: "POST",
        credentials: "include"
    });

    if (res.ok) {
        return;
    } else {
        throw new Error(`Failed to set attendance: ${res.status} ${res.statusText}`);
    }
}

export async function setCalendarEventAttendance(eventID: number, state: CalendarAttendance) {
    let res = await fetch(`${addr}/calendar/${eventID}/attendance?state=${state}`, {
        method: "POST",
        credentials: "include",
    });

    if (res.ok) {
        return;
    } else {
        throw new Error(`Failed to set attendance: ${res.status} ${res.statusText}`);
    }
}