<?php

final class DeletedHostsCleanupConfig
{
    public function __construct(
        public readonly bool $enabled,
        public readonly int $days,
    ) {
    }
}

final class DuplicateHostnameHostsCleanupConfig
{
    public function __construct(
        public readonly bool $enabled,
        public readonly bool $completelyClear,
    ) {
    }
}

final class DuplicateIpHostsCleanupConfig
{
    public function __construct(
        public readonly bool $enabled,
        public readonly bool $completelyClear,
    ) {
    }
}

final class GroupHostsCleanupConfig
{
    public function __construct(
        public readonly bool $enabled,
        public readonly ?int $groupId,
        public readonly bool $completelyClear,
    ) {
    }
}

final class InactiveHostsCleanupConfig
{
    public function __construct(
        public readonly bool $enabled,
        public readonly int $days,
        public readonly bool $completelyClear,
    ) {
    }
}

final class HostsCleanupSettingsDTO
{
    public function __construct(
        public readonly DeletedHostsCleanupConfig $deletedHostsCleanup,
        public readonly DuplicateHostnameHostsCleanupConfig $duplicateHostnameHostsCleanup,
        public readonly DuplicateIpHostsCleanupConfig $duplicateIpHostsCleanup,
        public readonly GroupHostsCleanupConfig $groupHostsCleanup,
        public readonly InactiveHostsCleanupConfig $inactiveHostsCleanup,
    ) {
    }

    public static function fromSettings(array $settings): self
    {
        $deleted = self::decode($settings, SettingsHelper::DELETED_HOSTS_CLEANUP);
        $duplicateHostname = self::decode($settings, SettingsHelper::DUPLICATE_HOSTNAME_HOSTS_CLEANUP);
        $duplicateIp = self::decode($settings, SettingsHelper::DUPLICATE_IP_HOSTS_CLEANUP);
        $group = self::decode($settings, SettingsHelper::GROUP_HOSTS_CLEANUP);
        $inactivity = self::decode($settings, SettingsHelper::INACTIVE_HOSTS_CLEANUP);

        return new self(
            new DeletedHostsCleanupConfig(
                enabled: (bool)$deleted['enabled'],
                days: (int)$deleted['days'],
            ),
            new DuplicateHostnameHostsCleanupConfig(
                enabled: (bool)$duplicateHostname['enabled'],
                completelyClear: (bool)$duplicateHostname['completelyClear'],
            ),
            new DuplicateIpHostsCleanupConfig(
                enabled: (bool)$duplicateIp['enabled'],
                completelyClear: (bool)$duplicateIp['completelyClear'],
            ),
            new GroupHostsCleanupConfig(
                enabled: (bool)$group['enabled'],
                groupId: $group['groupId'] === '' || $group['groupId'] === null ? null : (int)$group['groupId'],
                completelyClear: (bool)$group['completelyClear'],
            ),
            new InactiveHostsCleanupConfig(
                enabled: (bool)$inactivity['enabled'],
                days: (int)$inactivity['days'],
                completelyClear: (bool)$inactivity['completelyClear'],
            ),
        );
    }

    private static function decode(array $settings, string $key): array
    {
        $defaults = SettingsHelper::DEFAULTS[$key] ?? [];
        $raw = $settings[$key] ?? null;

        if (is_array($raw)) {
            return array_replace($defaults, $raw);
        }
        if (is_string($raw)) {
            $decoded = json_decode(json_decode($raw), true);
            if (is_array($decoded)) {
                return array_replace($defaults, $decoded);
            }
        }
        return $defaults;
    }
}
