<?php
/**
 * FreshCloud Mail - composer autoload entrypoint (Oscar version)
 *
 * Wires the real Composer with concrete adapter implementations from Oscar's
 * 17 mail-engine modules. The bridge's namespaces point to:
 *   - FreshCloud\Mail\Log\InMemoryLogger         (mail-engine-log)
 *   - FreshCloud\Mail\SmtpClient\InMemorySmtpClient  (mail-engine-smtp)
 *   - FreshCloud\Mail\MailClient\InMemoryMailClient  (mail-engine-mail-client)
 *   - FreshCloud\Mail\Identity\InMemoryIdentityProvider  (mail-app-identity)
 *   - FreshCloud\Mail\Compose\Composer           (mail-app-compose)
 *   - FreshCloud\Mail\PocketBaseAdapter\InMemoryPb  (mail-app-pocketbase)
 *
 * The 4 mail-app-* modules are app-layer (not engine) — they wire the engine
 * modules together into a usable Composer. The 17 mail-engine-* modules are
 * the actual mechanics (parsing, transport, storage, etc.).
 *
 * PocketBase: in v0.1.0-test mode, uses a null adapter (no PB). For real
 * deployment, instantiate RealPocketBaseAdapter with credentials from env.
 */

declare(strict_types=1);

if (!defined('FRESHCLOUD_MAIL_BOOTSTRAPPED')) {
    define('FRESHCLOUD_MAIL_BOOTSTRAPPED', true);

    // Composer autoloader — looks in oscar-platform/vendor/autoload.php
    $autoload = __DIR__ . '/../../vendor/autoload.php';
    if (file_exists($autoload)) {
        require_once $autoload;
    } else {
        error_log('FreshCloud Mail: vendor/autoload.php not found at ' . $autoload);
    }
}

if (!function_exists('freshcloud_mail_logger')) {
    function freshcloud_mail_logger(): \FreshCloud\Mail\Log\Logger
    {
        static $logger = null;
        if ($logger === null) {
            $logger = new \FreshCloud\Mail\Log\InMemoryLogger();
        }
        return $logger;
    }
}

if (!function_exists('freshcloud_mail_pb')) {
    function freshcloud_mail_pb(): \FreshCloud\Mail\PocketBaseAdapter\PocketBaseAdapter
    {
        // In v0.1.0-test: no real PB. Use a null adapter that no-ops.
        // For production, this should construct RealPocketBaseAdapter with
        // env credentials.
        return new \FreshCloud\Mail\PocketBaseAdapter\InMemoryPb();
    }
}

if (!function_exists('freshcloud_mail_composer')) {
    /**
     * Get a wired Composer for the given user+account.
     * Returns null if the bridge isn't ready (defensive).
     */
    function freshcloud_mail_composer(?string $userId = null, ?string $accountId = null): ?\FreshCloud\Mail\Compose\Composer
    {
        try {
            $logger = freshcloud_mail_logger();
            $pb = freshcloud_mail_pb();
            $smtp = new \FreshCloud\Mail\SmtpClient\InMemorySmtpClient();
            $mailClient = new \FreshCloud\Mail\MailClient\InMemoryMailClient();
            $identity = new \FreshCloud\Mail\Identity\InMemoryIdentityProvider();

            return new \FreshCloud\Mail\Compose\Composer(
                $pb,
                $logger,
                $smtp,
                $mailClient,
                $identity
            );
        } catch (\Throwable $e) {
            error_log('freshcloud_mail_composer failed: ' . $e->getMessage());
            return null;
        }
    }
}

if (!function_exists('freshcloud_mail_json')) {
    function freshcloud_mail_json(int $status, array $payload): void
    {
        http_response_code($status);
        header('Content-Type: application/json');
        header('X-FreshCloud-Mail: v0.1.0');
        echo json_encode($payload, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
        exit;
    }
}

if (!function_exists('freshcloud_mail_error')) {
    function freshcloud_mail_error(int $status, string $error, string $message): void
    {
        freshcloud_mail_json($status, [
            'error' => $error,
            'message' => $message,
            'timestamp' => gmdate('c'),
        ]);
    }
}
