<?php
// Minimal mail service for the workspace app (VM).
// Usage:
//   require_once __DIR__ . '/MailService.php';
//   // Generic:
//   MailService::sendMail($to, $subject, $htmlBody, $textBody = null, $opts = []);
//   // Contact form helper:
//   MailService::sendContactMessage($name, $email, $message, $to = null, $subject = 'New contact form');

class MailService
{
    // Universal mail sender (no attachments by design)
    public static function sendMail($to, string $subject, string $htmlBody, ?string $textBody = null, array $opts = [])
    {
        $cfg = self::loadConfig();

        // Check if enabled (db-backed settings)
        if (isset($cfg['is_enabled']) && !$cfg['is_enabled']) {
            self::logEmail($to, $subject, 'failure', 'SMTP is currently disabled due to repeated failures.');
            return [ 'success' => false, 'error' => 'SMTP is disabled' ];
        }

        $autoload = __DIR__ . '/../vendor/autoload.php';
        if (file_exists($autoload)) {
            require_once $autoload;
        }
        if (!class_exists('PHPMailer\PHPMailer\PHPMailer')) {
            @require_once 'libphp-phpmailer/autoload.php';
            if (!class_exists('PHPMailer\PHPMailer\PHPMailer')) {
                @require_once 'libphp-phpmailer/src/Exception.php';
                @require_once 'libphp-phpmailer/src/SMTP.php';
                @require_once 'libphp-phpmailer/src/PHPMailer.php';
            }
            if (!class_exists('PHPMailer\PHPMailer\PHPMailer')) {
                @require_once 'PHPMailer/src/Exception.php';
                @require_once 'PHPMailer/src/SMTP.php';
                @require_once 'PHPMailer/src/PHPMailer.php';
            }
            if (!class_exists('PHPMailer\PHPMailer\PHPMailer')) {
                @require_once 'PHPMailer/Exception.php';
                @require_once 'PHPMailer/SMTP.php';
                @require_once 'PHPMailer/PHPMailer.php';
            }
        }

        if (!class_exists('PHPMailer\PHPMailer\PHPMailer')) {
            self::logEmail($to, $subject, 'failure', 'PHPMailer not available');
            return [ 'success' => false, 'error' => 'PHPMailer not available' ];
        }

        $mail = new PHPMailer\PHPMailer\PHPMailer(true);
        try {
            $mail->isSMTP();
            $mail->Host       = $cfg['smtp_host'] ?? '';
            $mail->Port       = (int)($cfg['smtp_port'] ?? 587);
            $secure = $cfg['smtp_secure'] ?? 'tls';
            if ($secure === 'ssl') $mail->SMTPSecure = PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_SMTPS;
            elseif ($secure === 'tls') $mail->SMTPSecure = PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_STARTTLS;
            else $mail->SMTPSecure = false;
            $mail->SMTPAuth   = true;
            $mail->Username   = $cfg['smtp_user'] ?? '';
            $mail->Password   = $cfg['smtp_pass'] ?? '';
            
            // Set timeout to 10 seconds to prevent long hangs
            $mail->Timeout    = 10;
            $mail->SMTPKeepAlive = false;

            $fromEmail = $opts['from_email'] ?? ($cfg['from_email'] ?? 'no-reply@localhost');
            $fromName  = $opts['from_name']  ?? ($cfg['from_name']  ?? 'App');
            $mail->setFrom($fromEmail, $fromName);
            if (!empty($opts['reply_to']) && filter_var($opts['reply_to'], FILTER_VALIDATE_EMAIL)) {
                $mail->addReplyTo($opts['reply_to']);
            } elseif (!empty($cfg['reply_to'])) {
                $mail->addReplyTo($cfg['reply_to']);
            }

            // Recipients
            $toList = [];
            if ($to) {
                if (is_string($to)) $toList = array_map('trim', explode(',', $to));
                elseif (is_array($to)) $toList = $to;
            } elseif (!empty(getenv('MAIL_TO'))) {
                $toList = array_map('trim', explode(',', getenv('MAIL_TO')));
            }
            $added = 0;
            foreach ($toList as $addr) {
                if (filter_var($addr, FILTER_VALIDATE_EMAIL)) { $mail->addAddress($addr); $added++; }
            }
            if ($added === 0) {
                self::logEmail($to, $subject, 'failure', 'No recipients defined');
                return [ 'success' => false, 'error' => 'No recipients defined (set MAIL_TO or pass $to)' ];
            }

            foreach ((array)($opts['cc'] ?? []) as $cc)  { if (filter_var($cc,  FILTER_VALIDATE_EMAIL)) $mail->addCC($cc); }
            foreach ((array)($opts['bcc'] ?? []) as $bcc){ if (filter_var($bcc, FILTER_VALIDATE_EMAIL)) $mail->addBCC($bcc); }

            // Optional DKIM
            if (!empty($cfg['dkim_domain']) && !empty($cfg['dkim_selector']) && !empty($cfg['dkim_private_key_path'])) {
                $mail->DKIM_domain = $cfg['dkim_domain'];
                $mail->DKIM_selector = $cfg['dkim_selector'];
                $mail->DKIM_private = $cfg['dkim_private_key_path'];
            }

            $mail->isHTML(true);
            $mail->Subject = $subject;
            $mail->Body    = $htmlBody;
            $mail->AltBody = $textBody ?? strip_tags($htmlBody);
            $ok = $mail->send();
            
            self::logEmail($to, $subject, 'success');
            self::resetFailures();

            return [ 'success' => $ok ];
        } catch (\Throwable $e) {
            $error = $e->getMessage();
            self::logEmail($to, $subject, 'failure', $error);
            self::incrementFailures();
            return [ 'success' => false, 'error' => 'PHPMailer error: ' . $error ];
        }
    }

    private static function loadConfig(): array
    {
        $configPath = __DIR__ . '/config.php';
        if (!file_exists($configPath)) {
            throw new \RuntimeException('Mail config not found.');
        }
        $cfg = require $configPath;
        if (!is_array($cfg)) {
            throw new \RuntimeException('Invalid mail config format.');
        }

        // Try to load extra from DB
        try {
            $stmt = db()->query("SELECT * FROM smtp_settings LIMIT 1");
            $dbCfg = $stmt->fetch();
            if ($dbCfg) {
                // Merge DB settings if not set in config or env
                foreach ($dbCfg as $key => $val) {
                    if (!isset($cfg[$key])) $cfg[$key] = $val;
                }
                // Specifically override enablement
                $cfg['is_enabled'] = (bool)$dbCfg['is_enabled'];
                $cfg['max_failures'] = (int)$dbCfg['max_failures'];
            }
        } catch (\Exception $e) {}

        return $cfg;
    }

    private static function logEmail($to, $subject, $status, $error = null)
    {
        try {
            if (is_array($to)) $to = implode(', ', $to);
            $stmt = db()->prepare("INSERT INTO email_logs (recipient, subject, status, error_message) VALUES (?, ?, ?, ?)");
            $stmt->execute([$to, $subject, $status, $error]);
        } catch (\Exception $e) {}
    }

    private static function incrementFailures()
    {
        try {
            db()->query("UPDATE smtp_settings SET consecutive_failures = consecutive_failures + 1 WHERE id = 1");
            // Check if threshold reached
            $stmt = db()->query("SELECT consecutive_failures, max_failures FROM smtp_settings WHERE id = 1");
            $res = $stmt->fetch();
            if ($res && $res['consecutive_failures'] >= $res['max_failures']) {
                db()->query("UPDATE smtp_settings SET is_enabled = 0 WHERE id = 1");
            }
        } catch (\Exception $e) {}
    }

    private static function resetFailures()
    {
        try {
            db()->query("UPDATE smtp_settings SET consecutive_failures = 0 WHERE id = 1");
        } catch (\Exception $e) {}
    }

    // Send a contact message
    public static function sendContactMessage(string $name, string $email, string $message, $to = null, string $subject = 'New contact form')
    {
        // For simplicity, let's just use sendMail for everything
        $safeName  = htmlspecialchars($name, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
        $safeEmail = htmlspecialchars($email, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
        $safeBody  = nl2br(htmlspecialchars($message, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'));
        $html = "<p><strong>Name:</strong> {$safeName}</p><p><strong>Email:</strong> {$safeEmail}</p><hr>{$safeBody}";
        
        return self::sendMail($to, $subject, $html, $message, ['reply_to' => $email]);
    }
}