import { createParamDecorator } from '@nestjs/common';
import type { ExecutionContext } from '@nestjs/common';

export interface JwtPayload {
	sub: string;
	email: string;
	role?: string;
	app_id?: string;
	iat?: number;
	exp?: number;
}

export const CurrentUser = createParamDecorator(
	(data: keyof JwtPayload | undefined, ctx: ExecutionContext): JwtPayload | string => {
		const request = ctx.switchToHttp().getRequest();
		const user = request.user as JwtPayload;

		if (data) {
			return user[data] as string;
		}

		return user;
	}
);
