import { ExecutionContext, createParamDecorator } from '@nestjs/common'
import type { AuthentikJwtPayload } from './jwt-payload.interface.js'

// Extract the verified JWT payload that JwtAuthGuard attached to the request.
export const CurrentUser = createParamDecorator(
  (_data: unknown, ctx: ExecutionContext): AuthentikJwtPayload => {
    const req = ctx.switchToHttp().getRequest<{ user?: AuthentikJwtPayload }>()
    if (!req.user) {
      throw new Error('CurrentUser used without JwtAuthGuard — guard not applied to this route')
    }
    return req.user
  },
)
