<?php

namespace Tests\Unit;

use App\Support\OptionLayoutDecider;
use PHPUnit\Framework\TestCase;

class OptionLayoutDeciderTest extends TestCase
{
    public function test_short_plain_options_use_four_columns_in_exam(): void
    {
        $decider = new OptionLayoutDecider();
        $result = $decider->decide(['-3', '3', 'x', '-x'], 'exam');

        $this->assertSame('options-grid-4', $result['class']);
    }

    public function test_complex_formula_options_do_not_use_four_columns(): void
    {
        $decider = new OptionLayoutDecider();
        $result = $decider->decide(
            ['若a^x=b，则x=log_a b', '若x^a=b，则x=log_a b', '若a=b^x，则x=log_a b', '若a^b=x，则x=log_a b'],
            'exam'
        );

        $this->assertNotSame('options-grid-4', $result['class']);
    }

    public function test_grading_context_is_more_conservative_than_exam(): void
    {
        $decider = new OptionLayoutDecider();
        $options = ['x+1', 'x-1', 'x^2', 'x^3'];

        $exam = $decider->decide($options, 'exam');
        $grading = $decider->decide($options, 'grading');

        $rank = [
            'options-grid-1' => 1,
            'options-grid-2' => 2,
            'options-grid-4' => 3,
        ];

        $this->assertLessThanOrEqual($rank[$exam['class']], $rank[$grading['class']]);
    }

    public function test_short_fraction_like_choices_prefer_four_columns(): void
    {
        $decider = new OptionLayoutDecider();
        $result = $decider->decide(['1/2', '-1/2', '√3/2', '-√3/2'], 'exam');

        $this->assertSame('options-grid-4', $result['class']);
    }

    public function test_compact_latex_fraction_choices_prefer_four_columns(): void
    {
        $decider = new OptionLayoutDecider();
        $result = $decider->decide(['5', '9', '\\frac{5}{2}', '\\frac{9}{2}'], 'exam');

        $this->assertSame('options-grid-4', $result['class']);
    }

    public function test_compact_degree_choices_prefer_four_columns(): void
    {
        $decider = new OptionLayoutDecider();
        $result = $decider->decide(['15^\\circ', '20^\\circ', '25^\\circ', '30^\\circ'], 'exam');

        $this->assertSame('options-grid-4', $result['class']);
    }

    public function test_long_chinese_text_choices_should_not_be_forced_into_four_columns(): void
    {
        $decider = new OptionLayoutDecider();
        $result = $decider->decide(
            ['充分不必要条件', '必要不充分条件', '充要条件', '既不充分又不必要条件'],
            'exam'
        );

        $this->assertNotSame('options-grid-4', $result['class']);
    }

    public function test_mixed_root_and_fraction_choices_prefer_two_columns_over_one(): void
    {
        $decider = new OptionLayoutDecider();
        $result = $decider->decide(
            ['\\sqrt{3}', '\\sqrt{2}', '\\frac{\\sqrt{3}}{3}', '\\frac{\\sqrt{2}}{2}'],
            'exam'
        );

        $this->assertSame('options-grid-2', $result['class']);
    }

    public function test_keep_standard_fraction_display_format(): void
    {
        $decider = new OptionLayoutDecider();
        $this->assertSame('\\frac{\\sqrt{3}}{2}', $decider->normalizeCompactMathForDisplay('\\frac{\\sqrt{3}}{2}'));
        $this->assertSame('-\\frac{\\sqrt{3}}{2}', $decider->normalizeCompactMathForDisplay('-\\frac{\\sqrt{3}}{2}'));
        $this->assertSame('\\frac{x+1}{2}', $decider->normalizeCompactMathForDisplay('\\frac{x+1}{2}'));
    }
}
