<?php
// includes/core/SmtpSelector.php

class SmtpSelector {
    public static function getNextSmtp(): ?array {
        try {
            $db = Database::getInstance();

            $stmt = $db->query("
                SELECT * FROM smtp_accounts
                WHERE is_active = 1
                ORDER BY last_used ASC, id ASC
                LIMIT 1
            ");

            $smtp = $stmt->fetch();

            if ($smtp) {
                // Aktualisiere last_used für Rotation
                $update = $db->prepare("UPDATE smtp_accounts SET last_used = NOW() WHERE id = ?");
                $update->execute([$smtp['id']]);
            }

            return $smtp ?: null;

        } catch (PDOException $e) {
            ErrorLogger::logSqlError("SMTP-Auswahl fehlgeschlagen | table: smtp_accounts | error: " . $e->getMessage());
            return null;
        }
    }
}
