import { NextRequest, NextResponse } from 'next/server';
import { getArtworks, addArtwork } from '@/lib/data';
import type { Artwork } from '@/lib/types';

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

export async function POST(request: NextRequest) {
  try {
    const body = await request.json();
    const artwork: Artwork = {
      id: body.id || `art-${Date.now()}`,
      title: body.title || '',
      medium: body.medium || '',
      dimensions: body.dimensions || '',
      year: body.year || '',
      price: body.price || '',
      image: body.image || '',
      category: body.category || 'available',
      featured: body.featured || false,
      sortOrder: body.sortOrder ?? getArtworks().length + 1,
    };
    addArtwork(artwork);
    return NextResponse.json(artwork, { status: 201 });
  } catch {
    return NextResponse.json({ error: 'Invalid request' }, { status: 400 });
  }
}
