import { NextResponse } from 'next/server'
import { getCurrentUser } from '@/lib/auth'
import { roleService } from '@/lib/services/role-service'

export async function POST() {
  try {
    const currentUser = await getCurrentUser()
    if (!currentUser) {
      return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
    }

    if (currentUser.role !== 'SUPERADMIN') {
      return NextResponse.json({ error: 'Only superadmins can initialize permissions' }, { status: 403 })
    }

    const result = await roleService.initializePermissions()
    return NextResponse.json(result)
  } catch (error) {
    console.error('[API] Failed to initialize permissions:', error)
    return NextResponse.json({ error: 'Failed to initialize permissions' }, { status: 500 })
  }
}
