<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\DI\Validation;

use PpPhp\Core\Platform\DI\Definition\ServiceDefinition;
use PpPhp\Core\Platform\DI\Metadata\DefinitionKind;
use PpPhp\Core\Platform\DI\Metadata\DefinitionType;

final class DefinitionModelFinalizer
{
    public function validate(): array
    {
        $issues = [];

        if (!class_exists(ServiceDefinition::class)) {
            $issues[] = 'Legacy ServiceDefinition shim is missing.';
        }

        if (!class_exists(DefinitionKind::class)) {
            $issues[] = 'Legacy DefinitionKind shim is missing.';
        }

        $reservedConstants = ['CLASS', 'FACTORY', 'INSTANCE', 'ALIAS', 'VALUE', 'SYNTHETIC'];
        $reflection = new \ReflectionClass(DefinitionKind::class);
        $constants = array_keys($reflection->getConstants());

        foreach ($reservedConstants as $constant) {
            if (in_array($constant, $constants, true)) {
                $issues[] = 'Reserved legacy constant remains: DefinitionKind::' . $constant;
            }
        }

        foreach ([
            DefinitionType::CLASS_SERVICE,
            DefinitionType::FACTORY_SERVICE,
            DefinitionType::INSTANCE_SERVICE,
            DefinitionType::ALIAS_SERVICE,
            DefinitionType::VALUE_SERVICE,
            DefinitionType::SYNTHETIC_SERVICE,
        ] as $type) {
            if ($type === '') {
                $issues[] = 'Empty definition type detected.';
            }
        }

        return $issues;
    }

    public function ok(): bool
    {
        return $this->validate() === [];
    }
}
