import { NextRequest, NextResponse } from 'next/server'
import { getCountries } from '@/lib/rumahotp'
import prisma from '@/lib/prisma'

export const dynamic = 'force-dynamic'

export async function GET(req: NextRequest) {
  try {
    const { searchParams } = new URL(req.url)
    const serviceId = parseInt(searchParams.get('service_id') || '13')

    const countries = await getCountries(serviceId)

    // Get OTP markup from settings
    const markupSetting = await prisma.setting.findUnique({ where: { key: 'otp_markup' } })
    const markup = parseFloat(markupSetting?.value || '10')

    return NextResponse.json({ countries, markup })
  } catch (error) {
    console.error('[OTP COUNTRIES]', error)
    return NextResponse.json({ error: 'Gagal memuat daftar negara' }, { status: 500 })
  }
}
