<?php

declare(strict_types=1);

/*
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */

namespace TYPO3\CMS\Core\Settings\Type;

use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Attribute\AsTaggedItem;
use TYPO3\CMS\Core\Settings\SettingDefinition;
use TYPO3\CMS\Core\Settings\SettingsTypeInterface;
use TYPO3\CMS\Core\Settings\SettingsTypeOption;
use TYPO3\CMS\Core\Settings\SettingsTypeOptionAwareInterface;
use TYPO3\CMS\Core\Utility\MathUtility;

#[AsTaggedItem(index: 'int')]
readonly class IntType implements SettingsTypeInterface, SettingsTypeOptionAwareInterface
{
    public function __construct(
        protected LoggerInterface $logger,
    ) {}

    public function validate(mixed $value, SettingDefinition $definition): bool
    {
        // Normalize to integer if possible
        if (is_int($value)) {
            $intValue = $value;
        } elseif (is_string($value) && MathUtility::canBeInterpretedAsInteger($value)) {
            $intValue = (int)$value;
        } else {
            return false;
        }

        // Check optional constraints
        if (array_key_exists('min', $definition->options) &&
            $intValue < $definition->options['min']
        ) {
            return false;
        }
        if (array_key_exists('max', $definition->options) &&
            $intValue > $definition->options['max']
        ) {
            return false;
        }
        if (array_key_exists('step', $definition->options)) {
            $stepBase = array_key_exists('min', $definition->options) ? $definition->options['min'] : 0;
            if (($stepBase - $value) % $definition->options['step'] !== 0) {
                return false;
            }
        }

        return true;
    }

    public function transformValue(mixed $value, SettingDefinition $definition): int
    {
        if (!$this->validate($value, $definition)) {
            $this->logger->warning('Setting validation field, reverting to default: {key}', ['key' => $definition->key]);
            return $definition->default;
        }

        return (int)$value;
    }

    public function getSupportedOptions(): array
    {
        return [
            'min' => new SettingsTypeOption(
                type: 'int',
                description: 'Minimum value allowed',
                required: false,
            ),
            'max' => new SettingsTypeOption(
                type: 'int',
                description: 'Maximum value allowed',
                required: false,
            ),
            'step' => new SettingsTypeOption(
                type: 'int',
                description: 'Step size',
                required: false,
            ),
        ];
    }

    public function validateOptions(SettingDefinition $definition): bool
    {
        $min = $definition->options['min'] ?? null;
        $max = $definition->options['max'] ?? null;
        $step = $definition->options['step'] ?? null;
        if ($min !== null && $max !== null && $min > $max) {
            return false;
        }
        if ($min !== null && $max !== null && $step !== null) {
            if ($max - $min < $step) {
                return false;
            }
            if (($max - $min) % $step !== 0) {
                return false;
            }
        }
        return true;
    }

    public function getJavaScriptModule(): string
    {
        return '@typo3/backend/settings/type/int.js';
    }
}
