import { google } from 'googleapis';

const GMAIL_REDIRECT_URI = 'http://localhost';
const GMAIL_SCOPES = [
  // 'https://www.googleapis.com/auth/gmail.modify',
  'https://www.googleapis.com/auth/gmail.readonly'
];

export class GmailAuthUtils {
  private constructor() {}

  static async getAuthUrl(clientId: string, clientSecret: string): Promise<string> {
    const oAuth2Client = new google.auth.OAuth2(
      clientId,
      clientSecret,
      GMAIL_REDIRECT_URI
    );    return oAuth2Client.generateAuthUrl({
      access_type: 'offline',
      scope: GMAIL_SCOPES,
      prompt: 'consent',
      include_granted_scopes: true
    });
  }

  static async getRefreshToken(clientId: string, clientSecret: string, code: string): Promise<string | null> {
    try {
      const oAuth2Client = new google.auth.OAuth2(
        clientId,
        clientSecret,
        GMAIL_REDIRECT_URI
      );

      const { tokens } = await oAuth2Client.getToken(code);
      return tokens.refresh_token || null;
    } catch (error) {
      console.error('Error getting refresh token:', error);
      return null;
    }
  }
}
