import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { PassportStrategy } from '@nestjs/passport';
import { ExtractJwt, Strategy } from 'passport-jwt';

@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
  constructor(private readonly $configService: ConfigService) {
    super({
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      ignoreExpiration: false,
      secretOrKey: $configService.get<string>('JWT_SECRET')
    });
  }

  public async validate(payload: any) {
    return {
      id: payload.sub,
      email: payload.email,
      role: payload.role,
      allowRefresh: payload.allowRefresh
    };
  }
}
