import { NextRequest, NextResponse } from 'next/server';
import { executeReseed } from '@/features/setup/server/reseed.service';
import { AuthError, requireSystemRole } from '@/lib/auth/session';

export async function POST(request: NextRequest) {
  try {
    const session = await requireSystemRole('super_admin');
    const body = await request.json();
    const report = await executeReseed({ ...body, actorId: session.user.id });

    return NextResponse.json(report, { status: report.executed ? 200 : 400 });
  } catch (error) {
    if (error instanceof AuthError) {
      return NextResponse.json({ message: error.message }, { status: error.status });
    }

    return NextResponse.json({ message: 'Unable to execute setup reseed' }, { status: 500 });
  }
}
