import { createParamDecorator, ExecutionContext } from '@nestjs/common';
import { Locale } from '@domain/value-objects/locale.vo';

/**
 * User payload interface extracted from JWT
 */
export interface UserPayload {
  id: string;
  email: string;
  role: string;
  organizationId: string;
  firstName: string;
  lastName: string;
  preferredLanguage?: Locale;
}

/**
 * CurrentUser Decorator
 *
 * Extracts the authenticated user from the request object.
 * Must be used with JwtAuthGuard.
 *
 * Usage:
 * @UseGuards(JwtAuthGuard)
 * @Get('me')
 * getProfile(@CurrentUser() user: UserPayload) {
 *   return user;
 * }
 *
 * You can also extract a specific property:
 * @Get('my-bookings')
 * getMyBookings(@CurrentUser('id') userId: string) {
 *   return this.bookingService.findByUserId(userId);
 * }
 */
export const CurrentUser = createParamDecorator(
  (data: keyof UserPayload | undefined, ctx: ExecutionContext) => {
    const request = ctx.switchToHttp().getRequest();
    const user = request.user;

    // If a specific property is requested, return only that property
    return data ? user?.[data] : user;
  }
);
