<?php

namespace Mtc\Plugins\BlockManager\Tests\Unit;

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

class VariantRawValueAuditTest extends TestCase
{
    private VariantRawValueAuditor $auditor;

    protected function setUp(): void
    {
        parent::setUp();
        $this->auditor = app(VariantRawValueAuditor::class);
    }

    public function test_returns_zero_for_empty_tree(): void
    {
        $result = $this->auditor->audit([]);

        $this->assertSame(
            ['raw_count' => 0, 'token_count' => 0],
            $result,
            "Empty property tree must report zero raw and zero token values."
        );
    }

    public function test_counts_raw_values_in_default_cell(): void
    {
        $tree = ['base' => ['default' => ['desktop' => [
            'typography' => [
                'font_size'    => ['raw' => '16px'],
                'font_weight'  => ['raw' => '600'],
            ],
            'box' => [
                'padding_x' => ['raw' => '24px'],
            ],
        ]]]];

        $result = $this->auditor->audit($tree);

        $this->assertSame(3, $result['raw_count'], "Three {raw: ...} wraps must be counted as 3 raw values.");
        $this->assertSame(0, $result['token_count'], "No {token: ...} wraps present — token count must be 0.");
    }

    public function test_counts_token_values_in_default_cell(): void
    {
        $tree = ['base' => ['default' => ['desktop' => [
            'typography' => ['color' => ['token' => '@primary_color']],
            'border'     => ['border_color' => ['token' => '@secondary_color']],
        ]]]];

        $result = $this->auditor->audit($tree);

        $this->assertSame(0, $result['raw_count'], "No raw wraps — raw_count must be 0.");
        $this->assertSame(
            2,
            $result['token_count'],
            "Two {token: ...} wraps must be counted as 2 token values."
        );
    }

    public function test_walks_all_states_and_breakpoints(): void
    {
        $tree = ['base' => [
            'default' => [
                'desktop' => [
                    'typography' => ['color' => ['token' => '@primary_color']],            // token
                    'box'        => ['padding_x' => ['raw' => '24px']],                    // raw
                ],
                'mobile' => [
                    'typography' => ['font_size' => ['raw' => '14px']],                    // raw
                ],
            ],
            'hover' => [
                'desktop' => [
                    'background' => ['color' => ['token' => '@secondary_color']],          // token
                    'effects'    => ['opacity' => ['raw' => '0.85']],                      // raw
                ],
            ],
            'disabled' => [
                'desktop' => [
                    'background' => ['color' => ['raw' => '#cccccc']],                     // raw
                    'effects'    => ['opacity' => ['raw' => '0.6']],                       // raw
                ],
            ],
        ]];

        $result = $this->auditor->audit($tree);

        $this->assertSame(
            5,
            $result['raw_count'],
            "Auditor must walk every (state × breakpoint) cell — five raw values were seeded across the matrix."
        );
        $this->assertSame(
            2,
            $result['token_count'],
            "Auditor must walk every (state × breakpoint) cell — two token values were seeded across the matrix."
        );
    }
}
