const { google } = require('googleapis');
const fs = require('fs');
const path = require('path');

// Path to the JSON key file
const keyFilePath = path.join('~/.openclaw/secret/client_secret_1045917636561-fnpb5mpvpfkcte2t4ho62adt4qqubgvp.apps.googleusercontent.com.json');

// Load the JSON key file
const auth = new google.auth.GoogleAuth({
  keyFile: keyFilePath,
  scopes: [
    'https://www.googleapis.com/auth/gmail.readonly',
    'https://www.googleapis.com/auth/calendar.events'
  ]
});

// Create a Google Auth client
async function run() {
  const authClient = await auth.getClient();

  // Use the Gmail API to list the latest 10 emails
  const gmail = google.gmail({ version: 'v1', auth: authClient });
  const response = await gmail.users.messages.list({
    userId: 'me',
    maxResults: 10
  });
  console.log('Latest 10 emails:');
  console.log(response.data);

  // Use the Calendar API to add a new event
  const calendar = google.calendar({ version: 'v3', auth: authClient });
  const event = {
    summary: 'Meeting with John',
    start: {
      dateTime: '2026-03-15T10:00:00+08:00',
    },
    end: {
      dateTime: '2026-03-15T11:00:00+08:00',
    },
  };
  const calendarResponse = await calendar.events.insert({
    calendarId: 'primary',
    resource: event,
  });
  console.log('New event added:');
  console.log(calendarResponse.data);
}

run().catch(console.error);