import { NextRequest, NextResponse } from 'next/server';
import { getServerSession } from 'next-auth';
import { authOptions } from "@/lib/auth";
import { prisma } from '@/lib/prisma';
import { createCalendarEvent, updateCalendarEvent, deleteCalendarEvent, CalendarConnection } from '@/lib/calendar-events';
import { upsertCachedEvent, deleteCachedEvent } from '@/lib/calendar-cache';

// Helper to find connection by calendarId
async function findConnectionForCalendar(userId: string, calendarId: string) {
    const user = await prisma.user.findUnique({
        where: { id: userId },
        include: { calendarConnections: true }
    });

    if (!user) return null;

    for (const conn of user.calendarConnections) {
        if (conn.calendars && Array.isArray(conn.calendars)) {
            const calendars = conn.calendars as any[];
            if (calendars.some(c => c.id === calendarId)) {
                return {
                    id: conn.id,
                    provider: conn.provider as 'google' | 'apple' | 'outlook',
                    accessToken: conn.accessToken,
                    refreshToken: conn.refreshToken || undefined,
                    expiresAt: conn.expiresAt || undefined,
                    calendars: conn.calendars
                } as CalendarConnection;
            }
        }
    }
    return null;
}

// POST - Create event
export async function POST(request: NextRequest) {
    try {
        const session = await getServerSession(authOptions);
        if (!session?.user?.email) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });

        const body = await request.json();
        const { calendarId, title, description, start, end, location, allDay, recurrence, url,
                reminders, busyStatus, visibility, attendees, attachments } = body;

        console.log('[API] Creating event:', { calendarId, title, start, end });

        if (!calendarId || !title || !start || !end) {
            return NextResponse.json({ error: 'Missing required fields' }, { status: 400 });
        }

        const userId = (session.user as any).id;
        const connection = await findConnectionForCalendar(userId, calendarId);

        if (!connection) {
            return NextResponse.json({ error: 'Calendar connection not found' }, { status: 404 });
        }

        const event = await createCalendarEvent(connection, calendarId, {
            title,
            description,
            start,
            end,
            location,
            allDay: !!allDay,
            recurrence,
            url,
            reminders,
            busyStatus,
            visibility,
            attendees,
            attachments,
        } as any);

        // Update cache - await to ensure it's ready before client refreshes
        try {
            await upsertCachedEvent(userId, connection.id, connection.provider, event);
        } catch (e) {
            console.error('[CACHE] Failed to cache created event:', e);
        }

        return NextResponse.json({ event });
    } catch (error: any) {
        console.error('Error creating event:', error);
        return NextResponse.json({ error: error.message || 'Failed to create event' }, { status: 500 });
    }
}

// PATCH - Update event
export async function PATCH(request: NextRequest) {
    try {
        const session = await getServerSession(authOptions);
        if (!session?.user?.email) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });

        const body = await request.json();
        const { calendarId, eventId, title, description, start, end, location, allDay, recurrence, url,
                reminders, busyStatus, visibility, attendees, attachments } = body;

        console.log('[API] Updating event:', { calendarId, eventId, title });

        if (!calendarId || !eventId) {
            return NextResponse.json({ error: 'Missing required fields' }, { status: 400 });
        }

        const userId = (session.user as any).id;
        const connection = await findConnectionForCalendar(userId, calendarId);

        if (!connection) {
            return NextResponse.json({ error: 'Calendar connection not found' }, { status: 404 });
        }

        const event = await updateCalendarEvent(connection, calendarId, eventId, {
            title,
            description,
            start,
            end,
            location,
            allDay: allDay !== undefined ? !!allDay : undefined,
            recurrence,
            url,
            reminders,
            busyStatus,
            visibility,
            attendees,
            attachments,
        } as any);

        // Update cache - await to ensure it's ready before client refreshes
        try {
            await upsertCachedEvent(userId, connection.id, connection.provider, event);
        } catch (e) {
            console.error('[CACHE] Failed to cache updated event:', e);
        }

        return NextResponse.json({ event });
    } catch (error: any) {
        console.error('Error updating event:', error);
        return NextResponse.json({ error: error.message || 'Failed to update event' }, { status: 500 });
    }
}

// DELETE - Delete event
export async function DELETE(request: NextRequest) {
    try {
        const session = await getServerSession(authOptions);
        if (!session?.user?.email) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });

        const { searchParams } = new URL(request.url);
        const calendarId = searchParams.get('calendarId');
        const eventId = searchParams.get('eventId');

        console.log('[API] Deleting event:', { calendarId, eventId });

        if (!calendarId || !eventId) {
            return NextResponse.json({ error: 'Missing required fields' }, { status: 400 });
        }

        const userId = (session.user as any).id;
        const connection = await findConnectionForCalendar(userId, calendarId);

        if (!connection) {
            return NextResponse.json({ error: 'Calendar connection not found' }, { status: 404 });
        }

        await deleteCalendarEvent(connection, calendarId, eventId);

        // Remove from cache - await to ensure consistency
        try {
            await deleteCachedEvent(userId, eventId, connection.provider);
        } catch (e) {
            console.error('[CACHE] Failed to delete cached event:', e);
        }

        return NextResponse.json({ success: true });
    } catch (error: any) {
        console.error('Error deleting event:', error);
        return NextResponse.json({ error: error.message || 'Failed to delete event' }, { status: 500 });
    }
}
