<?php

namespace MintyPHP\Service\Settings;

class SettingsSmtpGateway
{
    public function __construct(private readonly SettingsMetadataGateway $settingsMetadataGateway)
    {
    }

    public function getSmtpHost(): ?string
    {
        $value = $this->settingsMetadataGateway->getValue(SettingKeys::SMTP_HOST_KEY);
        $value = $value !== null ? trim($value) : '';
        return $value !== '' ? $value : null;
    }

    public function setSmtpHost(?string $host, ?string $description = null): bool
    {
        $value = $host !== null ? trim($host) : '';
        if ($value === '') {
            $value = null;
        }

        $desc = $description ?? 'setting.smtp_host';
        return $this->settingsMetadataGateway->set(SettingKeys::SMTP_HOST_KEY, $value, $desc);
    }

    public function getSmtpPort(): ?int
    {
        $value = $this->settingsMetadataGateway->getValue(SettingKeys::SMTP_PORT_KEY);
        if ($value === null || $value === '') {
            return null;
        }

        $port = (int) $value;
        return $port > 0 ? $port : null;
    }

    public function setSmtpPort(?int $port, ?string $description = null): bool
    {
        $value = $port && $port > 0 ? (string) $port : null;
        $desc = $description ?? 'setting.smtp_port';
        return $this->settingsMetadataGateway->set(SettingKeys::SMTP_PORT_KEY, $value, $desc);
    }

    public function getSmtpUser(): ?string
    {
        $value = $this->settingsMetadataGateway->getValue(SettingKeys::SMTP_USER_KEY);
        $value = $value !== null ? trim($value) : '';
        return $value !== '' ? $value : null;
    }

    public function setSmtpUser(?string $user, ?string $description = null): bool
    {
        $value = $user !== null ? trim($user) : '';
        if ($value === '') {
            $value = null;
        }

        $desc = $description ?? 'setting.smtp_user';
        return $this->settingsMetadataGateway->set(SettingKeys::SMTP_USER_KEY, $value, $desc);
    }

    public function getSmtpPassword(): ?string
    {
        $value = $this->settingsMetadataGateway->getValue(SettingKeys::SMTP_PASSWORD_KEY);
        $value = $value !== null ? (string) $value : '';
        return $value !== '' ? $value : null;
    }

    public function setSmtpPassword(?string $password, ?string $description = null): bool
    {
        $value = $password !== null ? (string) $password : '';
        if ($value === '') {
            $value = null;
        }

        $desc = $description ?? 'setting.smtp_password';
        return $this->settingsMetadataGateway->set(SettingKeys::SMTP_PASSWORD_KEY, $value, $desc);
    }

    public function getSmtpSecure(): ?string
    {
        $value = $this->settingsMetadataGateway->getValue(SettingKeys::SMTP_SECURE_KEY);
        $value = $value !== null ? strtolower(trim($value)) : '';
        if (!in_array($value, ['tls', 'ssl', 'none'], true)) {
            return null;
        }

        return $value === 'none' ? null : $value;
    }

    public function setSmtpSecure(?string $secure, ?string $description = null): bool
    {
        $value = $secure !== null ? strtolower(trim($secure)) : '';
        if ($value === '' || $value === 'none') {
            $value = null;
        } elseif (!in_array($value, ['tls', 'ssl'], true)) {
            return false;
        }

        $desc = $description ?? 'setting.smtp_secure';
        return $this->settingsMetadataGateway->set(SettingKeys::SMTP_SECURE_KEY, $value, $desc);
    }

    public function getSmtpFrom(): ?string
    {
        $value = $this->settingsMetadataGateway->getValue(SettingKeys::SMTP_FROM_KEY);
        $value = $value !== null ? trim($value) : '';
        return $value !== '' ? $value : null;
    }

    public function setSmtpFrom(?string $from, ?string $description = null): bool
    {
        $value = $from !== null ? trim($from) : '';
        if ($value === '') {
            $value = null;
        }

        $desc = $description ?? 'setting.smtp_from';
        return $this->settingsMetadataGateway->set(SettingKeys::SMTP_FROM_KEY, $value, $desc);
    }

    public function getSmtpFromName(): ?string
    {
        $value = $this->settingsMetadataGateway->getValue(SettingKeys::SMTP_FROM_NAME_KEY);
        $value = $value !== null ? trim($value) : '';
        return $value !== '' ? $value : null;
    }

    public function setSmtpFromName(?string $fromName, ?string $description = null): bool
    {
        $value = $fromName !== null ? trim($fromName) : '';
        if ($value === '') {
            $value = null;
        }

        $desc = $description ?? 'setting.smtp_from_name';
        return $this->settingsMetadataGateway->set(SettingKeys::SMTP_FROM_NAME_KEY, $value, $desc);
    }
}
