<?php

declare(strict_types=1);

namespace PpPhp\Core\Manifest;

final class ManifestValidator
{
    private const ALLOWED_TYPES = [
        'package',
        'library',
        'module',
        'plugin',
        'theme',
        'ui',
        'ui-foundation',
        'component',
        'layout',
        'icon-set',
        'documentation',
        'resource',
        'asset',
        'service',
        'provider',
        'platform-bundle'
    ];

    /**
     * @return array<int, string>
     */
    public function validate(Manifest $manifest): array
    {
        $errors = [];

        if ($manifest->id() === '') {
            $errors[] = 'Manifest id is required.';
        }

        if ($manifest->type() === '') {
            $errors[] = 'Manifest type is required.';
        }

        if ($manifest->type() !== '' && !in_array($manifest->type(), self::ALLOWED_TYPES, true)) {
            $errors[] = 'Unsupported manifest type: ' . $manifest->type();
        }

        if ($manifest->label() === '') {
            $errors[] = 'Manifest label/name is required.';
        }

        if ($manifest->version() === '') {
            $errors[] = 'Manifest version is required.';
        }

        foreach ($manifest->dependencies() as $dependency) {
            if (!is_string($dependency) || trim($dependency) === '') {
                $errors[] = 'Manifest dependency values must be non-empty strings.';
                break;
            }
        }

        return $errors;
    }
}
