import { NextRequest, NextResponse } from 'next/server';
import { getEvents, addEvent } from '@/lib/data';
import type { ArtEvent } from '@/lib/types';

export async function GET() {
  return NextResponse.json(getEvents());
}

export async function POST(request: NextRequest) {
  try {
    const body = await request.json();
    const event: ArtEvent = {
      id: body.id || `event-${Date.now()}`,
      title: body.title || '',
      description: body.description || '',
      image: body.image || '',
      date: body.date || '',
      link: body.link || '',
      section: body.section || 'reevolution',
      objectPosition: body.objectPosition || undefined,
      sortOrder: body.sortOrder ?? getEvents().length + 1,
    };
    addEvent(event);
    return NextResponse.json(event, { status: 201 });
  } catch {
    return NextResponse.json({ error: 'Invalid request' }, { status: 400 });
  }
}
