import { Injectable } from "@nestjs/common";
import { PassportStrategy } from "@nestjs/passport";
import { Strategy, VerifyCallback } from "passport-google-oauth20";
import { ConfigService } from "@nestjs/config";

@Injectable()
export class GoogleStrategy extends PassportStrategy(Strategy, "google") {
  constructor(config: ConfigService) {
    super({
      clientID: config.get<string>("GOOGLE_CLIENT_ID") || "",
      clientSecret: config.get<string>("GOOGLE_CLIENT_SECRET") || "",
      callbackURL: config.get<string>("GOOGLE_CALLBACK_URL") || "http://localhost:4000/api/auth/google/callback",
      scope: ["email", "profile"],
    });
  }

  async validate(
    _accessToken: string,
    _refreshToken: string,
    profile: any,
    done: VerifyCallback
  ) {
    const { id, displayName, emails, photos } = profile;

    const user = {
      googleId: id,
      email: emails?.[0]?.value || "",
      name: displayName,
      picture: photos?.[0]?.value || null,
    };

    done(null, user);
  }
}
