<?php

namespace Mtc\Plugins\BlockManager\Tests\Unit;

use Mtc\Plugins\BlockManager\Services\VariantStyleAggregator;
use Tests\TestCase;

class VariantStyleAggregatorTest extends TestCase
{
    private VariantStyleAggregator $service;

    private string $aggregatorPath;

    private ?string $originalContent = null;

    protected function setUp(): void
    {
        parent::setUp();
        $this->service = app(VariantStyleAggregator::class);
        $this->aggregatorPath = base_path('sites/default/css/block_manager_variants.less');
        $this->originalContent = is_file($this->aggregatorPath)
            ? file_get_contents($this->aggregatorPath)
            : null;
    }

    protected function tearDown(): void
    {
        // Restore the committed aggregator so the test doesn't leave drift.
        if ($this->originalContent !== null) {
            file_put_contents($this->aggregatorPath, $this->originalContent);
        } elseif (is_file($this->aggregatorPath)) {
            @unlink($this->aggregatorPath);
        }
        parent::tearDown();
    }

    public function test_rebuild_writes_aggregator_file(): void
    {
        $path = $this->service->rebuild();

        $this->assertSame($this->aggregatorPath, $path);
        $this->assertFileExists($path);
    }

    public function test_rebuild_imports_known_hand_authored_variants(): void
    {
        $this->service->rebuild();
        $contents = file_get_contents($this->aggregatorPath);

        // Phase 2 starter variants that ship on disk.
        $this->assertStringContainsString(
            "plugins/BlockManager/elements/button/styles/primary.less",
            $contents,
            'Aggregator must import the button primary stylesheet.'
        );
        $this->assertStringContainsString(
            "plugins/BlockManager/elements/heading/styles/default.less",
            $contents
        );
    }

    public function test_rebuild_uses_relative_path_climbing_to_project_root(): void
    {
        $this->service->rebuild();
        $contents = file_get_contents($this->aggregatorPath);

        // Aggregator sits at sites/default/css/ → ../../../ reaches the root.
        $this->assertStringContainsString(
            "@import '../../../plugins/BlockManager/elements/button/styles/primary.less';",
            $contents,
            'Import paths must be relative to the aggregator location (sites/default/css/).'
        );
    }

    public function test_rebuild_excludes_bak_files(): void
    {
        $this->service->rebuild();
        $contents = file_get_contents($this->aggregatorPath);

        $this->assertStringNotContainsString(
            '.less.bak',
            $contents,
            'Backup files left by the publish .bak retention must not be imported.'
        );
    }

    public function test_rebuild_output_is_deterministic(): void
    {
        $this->service->rebuild();
        $first = file_get_contents($this->aggregatorPath);

        $this->service->rebuild();
        $second = file_get_contents($this->aggregatorPath);

        $this->assertSame(
            $first,
            $second,
            'Repeated rebuilds must produce identical output (sorted) so git sees no churn.'
        );
    }
}
