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

export interface JwtPayload {
  sub: string; // userId
}

@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
  constructor() {
    super({
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      ignoreExpiration: false,
      secretOrKey: process.env.JWT_ACCESS_SECRET ?? 'CHANGE_ME_IN_PRODUCTION',
    });
  }

  async validate(payload: JwtPayload) {
    // Kept intentionally minimal — no DB hit on every request. Anything
    // needing fresh user state (status, etc.) should look it up explicitly.
    return { userId: payload.sub };
  }
}
