<?php

namespace MintyPHP\Tests\Architecture;

use MintyPHP\App\Module\ModuleManifest;
use PHPUnit\Framework\TestCase;

abstract class AbstractModuleStructureContractTestCase extends TestCase
{
    use ProjectFileAssertionSupport;

    /** @var list<array{id: string, path: string, manifest: ModuleManifest, raw: array<string, mixed>}> */
    protected static array $modules = [];

    public static function setUpBeforeClass(): void
    {
        $modulesDir = realpath(__DIR__ . '/../../modules');
        if ($modulesDir === false || !is_dir($modulesDir)) {
            return;
        }

        $entries = scandir($modulesDir);
        if ($entries === false) {
            return;
        }

        self::$modules = [];

        foreach ($entries as $entry) {
            if ($entry === '.' || $entry === '..' || !is_dir($modulesDir . '/' . $entry)) {
                continue;
            }

            $manifestFile = $modulesDir . '/' . $entry . '/module.php';
            if (!is_file($manifestFile)) {
                continue;
            }

            $raw = require $manifestFile;
            if (!is_array($raw)) {
                continue;
            }

            $manifest = ModuleManifest::fromArray($raw, $modulesDir . '/' . $entry);
            self::$modules[] = [
                'id' => $entry,
                'path' => $modulesDir . '/' . $entry,
                'manifest' => $manifest,
                'raw' => $raw,
            ];
        }
    }
}
