# 🔧 Outlook Addon Troubleshooting - Azure AD Integration

## 🎯 Current Situation
- ✅ Microsoft OAuth authentication is working
- ✅ Azure AD application is configured
- ❌ Outlook addon manifest fails with "Falha de carregamento"

## 🔍 Root Cause Analysis

The "Falha de carregamento" error when Microsoft OAuth is already working typically indicates one of these issues:

### 1. **Azure AD Application Configuration**
Your current Azure AD app (`f7007bc3-3066-4281-b5e6-75b1129fee4f`) is configured for OAuth authentication, but Outlook addons require additional configuration.

### 2. **Missing API Permissions**
Outlook addons need specific Microsoft Graph permissions that might not be configured.

### 3. **Domain Validation Issues**
The manifest domain might not be properly validated in Azure AD.

## 🛠️ Solutions to Try

### **Solution 1: Update Azure AD Application**

1. **Go to Azure Portal**: https://portal.azure.com
2. **Navigate to**: Azure Active Directory → App registrations
3. **Find your app**: "Firmly CRM System" (ID: f7007bc3-3066-4281-b5e6-75b1129fee4f)
4. **Add API Permissions**:
   - Click "API permissions"
   - Add permission: Microsoft Graph
   - Select: Delegated permissions
   - Add: `Mail.ReadWrite`, `Mail.Send`, `User.Read`
5. **Grant admin consent**
6. **Update manifest**: Add Outlook addon configuration

### **Solution 2: Create Separate Azure AD App for Outlook**

1. **Create new app registration**:
   - Name: "Firmly Outlook Addon"
   - Redirect URI: `https://firmly.webiton.pt/outlook-addon/index.html`
   - Supported account types: Same as current app

2. **Configure API permissions**:
   - Microsoft Graph → Delegated permissions
   - Add: `Mail.ReadWrite`, `Mail.Send`, `User.Read`

3. **Update manifest with new Client ID**

### **Solution 3: Use Current App with Updated Manifest**

Try the Azure-compatible manifest: `public/outlook-addon/manifest-azure-compatible.xml`

This manifest is designed to work with your existing Azure AD setup.

## 📋 Step-by-Step Fix

### **Step 1: Update Azure AD App Permissions**

1. Go to https://portal.azure.com
2. Azure Active Directory → App registrations
3. Find: "Firmly CRM System" (f7007bc3-3066-4281-b5e6-75b1129fee4f)
4. API permissions → Add permission
5. Microsoft Graph → Delegated permissions
6. Add these permissions:
   - `Mail.ReadWrite`
   - `Mail.Send` 
   - `User.Read`
   - `Tasks.ReadWrite`
7. Click "Grant admin consent"

### **Step 2: Update Manifest Authentication**

The addon needs to authenticate with your existing Microsoft setup. Update the HTML file to include authentication:

```html
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Firmly CRM</title>
    <script src="https://appsforoffice.microsoft.com/lib/1/hosted/office.js"></script>
</head>
<body>
    <div id="container">
        <h1>Firmly CRM</h1>
        <p>Add-in loaded successfully!</p>
        <button onclick="authenticate()">Authenticate with Microsoft</button>
    </div>
    
    <script>
        function authenticate() {
            // Redirect to your existing Microsoft OAuth
            window.location.href = 'https://firmly.webiton.pt/microsoft/redirect';
        }
    </script>
</body>
</html>
```

### **Step 3: Try Different Manifest Versions**

Try these manifests in order:

1. **Azure Compatible**: `manifest-azure-compatible.xml`
2. **Complex**: `manifest-complex.xml`
3. **Outlook**: `manifest-outlook.xml`
4. **MS Sample**: `manifest-ms-sample.xml`

## 🔍 Alternative: Sideload Testing

If Admin Center upload fails, try sideloading:

1. **Go to**: https://outlook.office.com
2. **Open any email**
3. **Click "..."** → "Get Add-ins"
4. **"My add-ins"** → "Custom add-ins"
5. **"Add a custom add-in"** → "Add from file..."
6. **Upload**: `manifest-azure-compatible.xml`

## 🎯 Expected Outcome

After implementing these fixes:
- ✅ Manifest uploads successfully
- ✅ Addon appears in Outlook
- ✅ Users can authenticate with existing Microsoft accounts
- ✅ Addon can create tasks in your CRM system

## 📞 Next Steps

If the issue persists:
1. Check Azure AD app logs for errors
2. Verify domain ownership in Azure AD
3. Consider creating a separate Azure AD app for the addon
4. Test with Microsoft's manifest validation tool


















