import { NextRequest, NextResponse } from 'next/server';
import { db } from '@/lib/db';

// GET all Polres with their Polseks for SPKT form
export async function GET(request: NextRequest) {
  try {
    const polresList = await db.polres.findMany({
      where: {
        isActive: true
      },
      orderBy: {
        order: 'asc'
      },
      select: {
        id: true,
        name: true,
        address: true,
        phone: true,
        email: true,
        kapolresName: true,
        kapolresPangkat: true,
        polseks: {
          where: {
            isActive: true
          },
          orderBy: {
            order: 'asc'
          },
          select: {
            id: true,
            name: true,
            address: true,
            phone: true,
            email: true,
            kapolsekName: true,
            kapolsekPangkat: true
          }
        }
      }
    });

    return NextResponse.json({
      success: true,
      data: polresList
    });
  } catch (error) {
    console.error('Error fetching police data for SPKT:', error);
    return NextResponse.json(
      { success: false, message: 'Terjadi kesalahan saat mengambil data kepolisian' },
      { status: 500 }
    );
  }
}