import { NextResponse } from 'next/server';
import { getUpcomingEvents } from '@/lib/apple-calendar';

export async function POST(req: Request) {
    try {
        const body = await req.json();
        const { email, password, calendarId, timeMin, timeMax } = body;

        if (!email || !password || !calendarId || !timeMin || !timeMax) {
            return NextResponse.json(
                { error: 'Missing required parameters' },
                { status: 400 }
            );
        }

        // Fetch events
        const events = await getUpcomingEvents(
            email,
            password,
            calendarId,
            timeMin,
            timeMax
        );

        return NextResponse.json({ success: true, events });
    } catch (error: any) {
        console.error('Error syncing Apple Calendar:', error);
        return NextResponse.json(
            { error: error.message || 'Failed to sync Apple Calendar' },
            { status: 500 }
        );
    }
}
