import { NextRequest, NextResponse } from 'next/server';
import { getServices, addService } from '@/lib/data';
import type { Service } from '@/lib/types';

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

export async function POST(request: NextRequest) {
  try {
    const body = await request.json();
    const service: Service = {
      id: body.id || `service-${Date.now()}`,
      title: body.title || '',
      subtitle: body.subtitle || '',
      duration: body.duration || '',
      description: body.description || '',
      howItWorks: body.howItWorks || [],
      whoItIsFor: body.whoItIsFor || [],
      image: body.image || '',
      color: body.color || 'lavender',
      calendlyLink: body.calendlyLink || '',
      highlighted: body.highlighted || false,
      sortOrder: body.sortOrder ?? getServices().length + 1,
    };
    addService(service);
    return NextResponse.json(service, { status: 201 });
  } catch {
    return NextResponse.json({ error: 'Invalid request' }, { status: 400 });
  }
}
