import { NextResponse } from 'next/server';
import { readData, writeData, validateAuth } from '@/lib/cms';
import { featuredArtworks, allArtworks } from '@/lib/data/artworks';

export async function GET() {
  const featured = readData('artworks-featured', featuredArtworks);
  const all = readData('artworks', allArtworks);
  return NextResponse.json({ featured, all });
}

export async function POST(request: Request) {
  if (!validateAuth(request)) {
    return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
  }

  try {
    const { featured, all } = await request.json();
    if (featured) writeData('artworks-featured', featured);
    if (all) writeData('artworks', all);
    return NextResponse.json({ ok: true });
  } catch {
    return NextResponse.json({ error: 'Invalid data' }, { status: 400 });
  }
}
