import pool from '../db';
import { CalendarEventShort, CalendarSignup, CalendarEvent, CalendarAttendance } from "@app/shared/types/calendar"
import { toDateTime } from "@app/shared/utils/time"

export async function createEvent(eventObject: Omit<CalendarEvent, 'id' | 'created_at' | 'updated_at' | 'cancelled'>) {
    const sql = `
        INSERT INTO calendar_events 
            (name, start, end, location, color, description, creator)
        VALUES (?, ?, ?, ?, ?, ?, ?)
    `;
    const params = [
        eventObject.name,
        eventObject.start,
        eventObject.end,
        eventObject.location,
        eventObject.color,
        eventObject.description ?? null,
        eventObject.creator_id,
    ];

    const result = await pool.query(sql, params);
    return { id: result.insertId, ...eventObject };
}

export async function updateEvent(eventObject: CalendarEvent) {
    if (!eventObject.id) {
        throw new Error("updateEvent: Missing event ID.");
    }
    const sql = `
        UPDATE calendar_events
        SET
            name = ?,
            start = ?,
            end = ?,
            location = ?,
            color = ?,
            description = ?
        WHERE id = ?
    `;

    const params = [
        eventObject.name,
        toDateTime(eventObject.start),
        toDateTime(eventObject.end),
        eventObject.location,
        eventObject.color,
        eventObject.description ?? null,
        eventObject.id
    ];

    await pool.query(sql, params);
    return { success: true };
}

export async function setEventCancelled(eventID: number, cancelled: boolean) {
    const input = cancelled ? 1 : 0;
    const sql = `
        UPDATE calendar_events
        SET cancelled = ?
        WHERE id = ?
    `;
    await pool.query(sql, [input, eventID]);
    return { success: true };
}


export async function getShortEventsInRange(startDate: string, endDate: string): Promise<CalendarEventShort[]> {
    const sql = `
        SELECT id, name, start, end, color, cancelled, full_day
        FROM calendar_events
        WHERE start BETWEEN ? AND ?
        ORDER BY start ASC
    `;
    const res: CalendarEventShort[] = await pool.query(sql, [startDate, endDate]);
    return res;
}

export async function getEventDetails(eventID: number): Promise<CalendarEvent> {
    const sql = `
        SELECT 
            e.id,
            e.name,
            e.start,
            e.end,
            e.location,
            e.color,
            e.description,
            e.cancelled,
            e.created_at,
            e.updated_at,
            e.creator AS creator_id,
            m.name AS creator_name
        FROM calendar_events e
        LEFT JOIN members m ON e.creator = m.id
        WHERE e.id = ?
    `;
    let vals: CalendarEvent[] = await pool.query(sql, [eventID]);
    return vals[0];
}

export async function getUpcomingEvents(date: Date, limit: number) {
    const sql = `
        SELECT id, name, start, end, color
        FROM calendar_events
        WHERE start >= ?
          AND cancelled = 0
        ORDER BY start ASC
        LIMIT ?
    `;
    return await pool.query(sql, [date, limit]);
}


export async function setAttendanceStatus(memberID: number, eventID: number, status: CalendarAttendance) {
    const sql = `
        INSERT INTO calendar_events_signups (member_id, event_id, status)
        VALUES (?, ?, ?)
        ON DUPLICATE KEY UPDATE status = VALUES(status), updated_at = CURRENT_TIMESTAMP
    `;
    await pool.query(sql, [memberID, eventID, status]);

    return { success: true }
}

export async function getEventAttendance(eventID: number): Promise<CalendarSignup[]> {

    const sql = "CALL `sp_GetCalendarEventSignups`(?)"
    const res = await pool.query(sql, [eventID]);
    console.log(res[0]);
    return res[0];
}