import { NextRequest, NextResponse } from 'next/server'
import { getSession } from '@/lib/auth'
import { testWebhook } from '@/lib/services/job-panel-webhooks'

export async function POST(
  request: NextRequest,
  { params }: { params: Promise<{ id: string }> }
) {
  try {
    const session = await getSession()
    if (!session || session.role !== 'SUPERADMIN') {
      return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
    }

    const body = await request.json()
    const { webhookUrl, webhookType } = body

    if (!webhookUrl || !webhookType) {
      return NextResponse.json({ error: 'Webhook URL and type are required' }, { status: 400 })
    }

    const result = await testWebhook(webhookUrl, webhookType)

    if (result.success) {
      return NextResponse.json({ success: true, message: 'Test webhook sent successfully' })
    } else {
      return NextResponse.json({ success: false, error: result.error }, { status: 400 })
    }
  } catch (error) {
    console.error('[Webhooks:Test] Error:', error)
    return NextResponse.json({ error: 'Failed to test webhook' }, { status: 500 })
  }
}
