<?php

namespace App\Services;

use App\Services\MicrosoftGraph\MicrosoftGraphApiClient;
use App\Services\MicrosoftGraph\MicrosoftGraphSyncOrchestrator;

/**
 * Main Microsoft Graph Sync Service - Facade for the new architecture
 *
 * This service now acts as a facade/wrapper around the new modular architecture.
 * It maintains backward compatibility while delegating to the specialized services.
 */
class MicrosoftGraphSyncService
{
    protected $orchestrator;

    public function __construct()
    {
        $this->orchestrator = new MicrosoftGraphSyncOrchestrator();
    }

    /**
     * Check if Microsoft Graph operations are available
     */
    public static function isAvailable(): bool
    {
        return MicrosoftGraphApiClient::isAvailable();
    }

    /**
     * Initialize the service with connection
     */
    public function initialize(): self
    {
        $this->orchestrator->initialize();
        return $this;
    }

    /**
     * Complete synchronization with clean slate approach
     */
    public function syncAll(bool $dryRun = false): array
    {
        return $this->orchestrator->syncAll($dryRun);
    }

    /**
     * Sync contact folders only
     */
    public function syncContactFolders(bool $dryRun = false): array
    {
        return $this->orchestrator->syncContactFolders($dryRun);
    }

    /**
     * Sync contacts only
     */
    public function syncContacts(bool $dryRun = false, bool $onlyNewUsers = false): array
    {
        return $this->orchestrator->syncContacts($dryRun, $onlyNewUsers);
    }

    /**
     * Get comprehensive statistics
     */
    public function getStats(): array
    {
        return $this->orchestrator->getStats();
    }

    /**
     * Test Microsoft Graph connection
     */
    public function testConnection(): array
    {
        return $this->orchestrator->testConnection();
    }

    /**
     * Create a test contact folder
     */
    public function createTestContactFolder(string $folderName = 'Test-Contact-Folder'): array
    {
        return $this->orchestrator->createTestContactFolder($folderName);
    }

    /**
     * Delete a test contact folder
     */
    public function deleteTestContactFolder(string $folderId): array
    {
        return $this->orchestrator->deleteTestContactFolder($folderId);
    }

    /**
     * Delete all contact folders and contacts
     */
    public function deleteAllContactFolders(bool $dryRun = false): array
    {
        return $this->orchestrator->deleteAllContactFolders($dryRun);
    }

    /**
     * Delete all contact folders and contacts from Microsoft Graph and clean local database
     * This is a DESTRUCTIVE operation that removes everything
     */
    public function deleteAllContactFoldersAndContacts(bool $dryRun = false): array
    {
        return $this->orchestrator->deleteAllContactFoldersAndContacts($dryRun);
    }

    /**
     * List all contact folders
     */
    public function listContactFolders(string $userEmail = null, bool $withContactCount = false): array
    {
        return $this->orchestrator->listContactFolders($userEmail, $withContactCount);
    }

    /**
     * List contacts in a specific folder
     */
    public function listContactsInFolder(string $folderIdOrName): array
    {
        return $this->orchestrator->listContactsInFolder($folderIdOrName);
    }

    /**
     * List all contacts across all folders
     */
    public function listAllContacts(): array
    {
        return $this->orchestrator->listAllContacts();
    }

    /**
     * Clear all contacts but keep folders
     */
    public function clearAllContacts(bool $dryRun = false): array
    {
        return $this->orchestrator->clearAllContacts($dryRun);
    }

    /**
     * Run comprehensive diagnostics
     */
    public function runDiagnostics(): array
    {
        return $this->orchestrator->runDiagnostics();
    }

    /**
     * Force reconnection to Microsoft Graph
     */
    public function forceReconnection(): void
    {
        $this->orchestrator->forceReconnection();
    }

    /**
     * Set dry run mode
     */
    public function setDryRun(bool $dryRun): self
    {
        $this->orchestrator->setDryRun($dryRun);
        return $this;
    }

    /**
     * Get dry run status
     */
    public function isDryRun(): bool
    {
        return $this->orchestrator->isDryRun();
    }

    // Additional helper methods for backward compatibility

    /**
     * Sync existing folder IDs (for migration purposes)
     */
    public function syncExistingFolderIds(bool $dryRun = false): array
    {
        // This could be implemented as a migration utility
        // For now, delegate to the full sync
        return $this->orchestrator->syncContactFolders($dryRun);
    }
}
