import { NextResponse } from 'next/server';
import { readData, writeData, validateAuth } from '@/lib/cms';
import { services, processComponents } from '@/lib/data/services';

export async function GET() {
  const svc = readData('services', services);
  const steps = readData('process-components', processComponents);
  return NextResponse.json({ services: svc, processComponents: steps });
}

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

  try {
    const body = await request.json();
    if (body.services) writeData('services', body.services);
    if (body.processComponents) writeData('process-components', body.processComponents);
    return NextResponse.json({ ok: true });
  } catch {
    return NextResponse.json({ error: 'Invalid data' }, { status: 400 });
  }
}
