import { NextResponse } from 'next/server';
import prisma from '@/lib/prisma';

export async function POST(req: Request) {
    try {
        const body = await req.json();
        const { title, startDate, endDate, password, instructions, feedingPerDay, feedingInterval, litterInterval } = body;

        if (!title || !startDate || !endDate || !password) {
            return NextResponse.json({ error: 'Missing required fields' }, { status: 400 });
        }

        const plan = await prisma.plan.create({
            data: {
                title,
                startDate: new Date(startDate),
                endDate: new Date(endDate),
                password,
                instructions,
                feedingPerDay: feedingPerDay ? parseInt(feedingPerDay) : undefined,
                feedingInterval: feedingInterval ? parseInt(feedingInterval) : undefined,
                litterInterval: litterInterval ? parseInt(litterInterval) : undefined,
            },
        });

        return NextResponse.json({ planId: plan.id });
    } catch (error) {
        console.error('Error creating plan:', error);
        return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
    }
}
