<?php

declare(strict_types=1);

namespace Instinctor\Core;


if (!defined('ABSPATH')) exit;
use Instinctor\Core\Mailer\DriverInterface;
use Instinctor\Core\Mailer\WordPressDriver;
use Instinctor\Core\Mailer\StandaloneDriver;

/**
 * Mailer — static facade over the active email delivery driver.
 *
 * Driver selection:
 *   INSTINCTOR_ENV = 'wordpress'   → WordPressDriver  (relay → SendGrid)
 *   INSTINCTOR_ENV = 'standalone'  → StandaloneDriver (direct SendGrid via cURL)
 *
 * Usage:
 *   // Plain text
 *   Mailer::send('user@example.com', 'Your order', 'Hello, your order is ready!');
 *
 *   // HTML
 *   Mailer::send('user@example.com', 'Your order', '<p>Hello!</p>', ['html' => true]);
 *
 *   // With reply-to
 *   Mailer::send($adminEmail, 'Form submission', $body, ['reply_to' => $visitorEmail]);
 *
 *   // Auto-detect HTML (body contains <p>, <br>, <div>, etc.)
 *   Mailer::send($to, $subject, $htmlBody);
 *
 *   // Async (T6 queue — currently sends synchronously)
 *   Mailer::queue($to, $subject, $body);
 *
 * Options array keys:
 *   bool   html       Force HTML content-type (auto-detected from body if omitted)
 *   string reply_to   Reply-To address
 *   string store_name Override store name in relay metadata
 *   string from_name  Override sender display name (standalone only)
 */
class Mailer
{
    private static ?DriverInterface $driver = null;

    public static function driver(): DriverInterface
    {
        if (self::$driver === null) {
            self::$driver = self::makeDriver();
        }
        return self::$driver;
    }

    public static function setDriver(DriverInterface $driver): void
    {
        self::$driver = $driver;
    }

    private static function makeDriver(): DriverInterface
    {
        $env = defined('INSTINCTOR_ENV') ? INSTINCTOR_ENV : 'wordpress';
        return match ($env) {
            'standalone' => new StandaloneDriver(),
            default      => new WordPressDriver(),
        };
    }

    // -------------------------------------------------------------------------
    // Static API
    // -------------------------------------------------------------------------

    /**
     * Send an email immediately via the active driver.
     *
     * @param string $to      Recipient email address
     * @param string $subject Email subject line
     * @param string $body    Plain text or HTML body
     * @param array  $options See class docblock for available keys
     */
    public static function send(string $to, string $subject, string $body, array $options = []): bool
    {
        return self::driver()->send($to, $subject, $body, $options);
    }

    /**
     * Queue an email for async delivery via T6 Queue.
     * Until T6 is implemented, sends synchronously.
     */
    public static function queue(string $to, string $subject, string $body, array $options = []): bool
    {
        return self::driver()->queue($to, $subject, $body, $options);
    }

    /**
     * Convenience: send an HTML email (shorthand for ['html' => true]).
     */
    public static function html(string $to, string $subject, string $body, array $options = []): bool
    {
        return self::send($to, $subject, $body, array_merge($options, ['html' => true]));
    }
}
