import { NextRequest, NextResponse } from 'next/server'
import { requireAdmin } from '@/lib/auth'
import { getPlayerLinkStatus } from '@/lib/services/player-access-service'

/**
 * GET /api/admin/mappings/[discordId]
 * 
 * Get detailed link status for a specific Discord ID
 */
export async function GET(
  request: NextRequest,
  { params }: { params: Promise<{ discordId: string }> }
) {
  try {
    await requireAdmin()
    
    const { discordId } = await params

    if (!discordId) {
      return NextResponse.json(
        { error: 'discordId is required' },
        { status: 400 }
      )
    }

    const status = await getPlayerLinkStatus(discordId)
    return NextResponse.json(status)
  } catch (error) {
    if (error instanceof Error && error.message === 'Unauthorized') {
      return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
    }
    if (error instanceof Error && error.message === 'Forbidden') {
      return NextResponse.json({ error: 'Forbidden' }, { status: 403 })
    }
    console.error('[API Admin Mapping Detail] Error:', error)
    return NextResponse.json({ error: 'Internal server error' }, { status: 500 })
  }
}
