<?php

declare(strict_types=1);

namespace App\Models;

use App\Core\Model;
use PDO;

final class ParametreMail extends Model
{
    /** @return array<string,mixed> */
    public function get(): array
    {
        if (!$this->tableExists()) {
            return $this->defaults();
        }

        $row = $this->db
            ->query('SELECT * FROM parametres_mail WHERE id = 1 LIMIT 1')
            ->fetch(PDO::FETCH_ASSOC);

        return is_array($row) ? $row : $this->defaults();
    }

    /** @param array<string,mixed> $data */
    public function save(array $data): void
    {
        if (!$this->tableExists()) {
            throw new \RuntimeException(
                'La table parametres_mail est absente. Exécutez la migration 024_parametres_mail.sql.'
            );
        }

        $sql = <<<'SQL'
INSERT INTO parametres_mail (
    id,
    smtp_host,
    smtp_port,
    smtp_secure,
    smtp_username,
    smtp_password_encrypted,
    smtp_from_name,
    smtp_from_email,
    smtp_reply_to,
    smtp_test_email,
    actif
) VALUES (
    1,
    :smtp_host,
    :smtp_port,
    :smtp_secure,
    :smtp_username,
    :smtp_password_encrypted,
    :smtp_from_name,
    :smtp_from_email,
    :smtp_reply_to,
    :smtp_test_email,
    :actif
)
ON DUPLICATE KEY UPDATE
    smtp_host = VALUES(smtp_host),
    smtp_port = VALUES(smtp_port),
    smtp_secure = VALUES(smtp_secure),
    smtp_username = VALUES(smtp_username),
    smtp_password_encrypted = VALUES(smtp_password_encrypted),
    smtp_from_name = VALUES(smtp_from_name),
    smtp_from_email = VALUES(smtp_from_email),
    smtp_reply_to = VALUES(smtp_reply_to),
    smtp_test_email = VALUES(smtp_test_email),
    actif = VALUES(actif)
SQL;

        $statement = $this->db->prepare($sql);
        $statement->execute([
            'smtp_host' => (string)($data['smtp_host'] ?? ''),
            'smtp_port' => (int)($data['smtp_port'] ?? 587),
            'smtp_secure' => (string)($data['smtp_secure'] ?? 'tls'),
            'smtp_username' => (string)($data['smtp_username'] ?? ''),
            'smtp_password_encrypted' => $data['smtp_password_encrypted'] ?? null,
            'smtp_from_name' => (string)($data['smtp_from_name'] ?? ''),
            'smtp_from_email' => (string)($data['smtp_from_email'] ?? ''),
            'smtp_reply_to' => (string)($data['smtp_reply_to'] ?? ''),
            'smtp_test_email' => (string)($data['smtp_test_email'] ?? ''),
            'actif' => !empty($data['actif']) ? 1 : 0,
        ]);
    }

    private function tableExists(): bool
    {
        $statement = $this->db->prepare(
            'SELECT COUNT(*) FROM information_schema.tables '
            . 'WHERE table_schema = DATABASE() AND table_name = :table'
        );
        $statement->execute(['table' => 'parametres_mail']);

        return (int)$statement->fetchColumn() > 0;
    }

    /** @return array<string,mixed> */
    private function defaults(): array
    {
        return [
            'id' => 1,
            'smtp_host' => '',
            'smtp_port' => 587,
            'smtp_secure' => 'tls',
            'smtp_username' => '',
            'smtp_password_encrypted' => null,
            'smtp_from_name' => '',
            'smtp_from_email' => '',
            'smtp_reply_to' => '',
            'smtp_test_email' => '',
            'actif' => 0,
            'date_creation' => null,
            'date_modification' => null,
        ];
    }
}
