# Google Meet Backend Setup Guide

This guide explains how to set up the Google Meet backend integration using Google Calendar API.

## Prerequisites

1. A Google Cloud Project
2. Google Calendar API enabled
3. OAuth 2.0 credentials configured

## Step 1: Create Google Cloud Project

1. Go to [Google Cloud Console](https://console.cloud.google.com/)
2. Create a new project or select an existing one
3. Enable the **Google Calendar API**:
   - Navigate to "APIs & Services" > "Library"
   - Search for "Google Calendar API"
   - Click "Enable"

## Step 2: Create OAuth 2.0 Credentials

1. Go to "APIs & Services" > "Credentials"
2. Click "Create Credentials" > "OAuth client ID"
3. If prompted, configure the OAuth consent screen:
   - Choose "External" (unless you have a Google Workspace)
   - Fill in required fields (App name, User support email, etc.)
   - Add scopes: `https://www.googleapis.com/auth/calendar`
   - Add test users if needed
4. Create OAuth 2.0 Client ID:
   - Application type: "Web application"
   - Name: "Google Meet Scheduler"
   - Authorized redirect URIs: `https://developers.google.com/oauthplayground`
   - Click "Create"
5. Save the **Client ID** and **Client Secret**

## Step 3: Get Refresh Token

1. Go to [OAuth 2.0 Playground](https://developers.google.com/oauthplayground/)
2. Click the gear icon (⚙️) in the top right
3. Check "Use your own OAuth credentials"
4. Enter your **Client ID** and **Client Secret**
5. In the left panel, find "Calendar API v3"
6. Select the scope: `https://www.googleapis.com/auth/calendar`
7. Click "Authorize APIs"
8. Sign in with the Google account that will be used for creating meetings
9. Click "Allow" to grant permissions
10. Click "Exchange authorization code for tokens"
11. Copy the **Refresh token** from the response

## Step 4: Configure Environment Variables

Add the following to your `Backend/.env` file:

```env
# Google Calendar API Configuration
GOOGLE_CLIENT_ID=your_client_id_here
GOOGLE_CLIENT_SECRET=your_client_secret_here
GOOGLE_REFRESH_TOKEN=your_refresh_token_here
GOOGLE_CALENDAR_EMAIL=your_calendar_email@example.com

# SMTP Configuration (for sending emails)
SMTP_HOST=smtp.gmail.com
SMTP_PORT=465
SMTP_USER=your_email@gmail.com
SMTP_PASS=your_app_password
SMTP_SECURE=true

# Company Email
COMPANY_EMAIL=contact@aibitsoft.com
```

### Notes:
- `GOOGLE_CALENDAR_EMAIL`: The email address of the Google account used for creating meetings (should match the account used in OAuth Playground)
- `SMTP_PASS`: For Gmail, use an [App Password](https://support.google.com/accounts/answer/185833) instead of your regular password

## Step 5: Test the Integration

You can test the Google Meet backend by:

1. Starting the backend server:
   ```bash
   cd Backend
   npm run dev
   ```

2. Making a test API call:
   ```bash
   curl -X POST http://localhost:4000/api/create-google-meet \
     -H "Content-Type: application/json" \
     -d '{
       "fullName": "Test User",
       "email": "test@example.com",
       "preferredMeetingTime": "2024-12-25 14:00",
       "meetingType": "consultation"
     }'
   ```

## API Endpoints

### Create Google Meet
- **POST** `/api/create-google-meet`
- Creates a Google Meet meeting with calendar event

### Schedule Google Meet (from UI)
- **POST** `/api/schedule-google-meet`
- Creates a Google Meet from ScheduleGoogleMeet UI component

### List Meetings
- **GET** `/api/google-meet`
- Query parameters: `status`, `email`, `startDate`, `endDate`

### Get Meeting by ID
- **GET** `/api/google-meet/:id`

### Get Meeting by Event ID
- **GET** `/api/google-meet/event/:eventId`

### Update Meeting
- **PUT** `/api/google-meet/:id`

### Cancel Meeting
- **PUT** `/api/google-meet/:id/cancel?deleteFromCalendar=true`

### Delete Meeting
- **DELETE** `/api/google-meet/:id`

## Features

- ✅ Creates Google Calendar events with Google Meet links
- ✅ Sends email notifications to admin and participants
- ✅ Stores meeting data in MongoDB
- ✅ Supports multiple attendees
- ✅ Automatic calendar reminders
- ✅ Timezone support
- ✅ Source detection (abitsol/abitsoft)

## Troubleshooting

### Error: "Google Calendar credentials are not configured"
- Make sure all Google environment variables are set in `Backend/.env`
- Verify the refresh token is valid (not expired)

### Error: "Failed to create Google Meet"
- Check that Google Calendar API is enabled in your Google Cloud project
- Verify the OAuth scopes include `https://www.googleapis.com/auth/calendar`
- Ensure the refresh token was generated with the correct account

### Meetings not appearing in Google Calendar
- Verify `GOOGLE_CALENDAR_EMAIL` matches the account used in OAuth Playground
- Check that the account has calendar access

### Emails not sending
- Verify SMTP credentials are correct
- For Gmail, ensure you're using an App Password, not your regular password
- Check that SMTP_HOST, SMTP_USER, and SMTP_PASS are set correctly

## Security Notes

- Never commit `.env` file to version control
- Keep your Client Secret and Refresh Token secure
- Regularly rotate credentials if compromised
- Use environment-specific credentials for production

