<?php

namespace Tests\Unit;

use App\Filament\Pages\QuestionDetailPage;
use Tests\TestCase;

class QuestionDetailPageUnitTest extends TestCase
{
    /**
     * 测试难度等级计算
     * Test Case ID: TC-QD-UNIT-001
     */
    public function test_get_difficulty_label(): void
    {
        $page = new QuestionDetailPage();

        // 测试不同难度值对应的标签
        $testCases = [
            ['difficulty' => 0.2, 'expected' => '简单'],
            ['difficulty' => 0.3, 'expected' => '简单'],
            ['difficulty' => 0.4, 'expected' => '中等'],
            ['difficulty' => 0.5, 'expected' => '中等'],
            ['difficulty' => 0.6, 'expected' => '中等'],
            ['difficulty' => 0.7, 'expected' => '困难'],
            ['difficulty' => 0.8, 'expected' => '困难'],
            ['difficulty' => 0.9, 'expected' => '困难'],
        ];

        foreach ($testCases as $case) {
            $page->questionData = ['difficulty' => $case['difficulty']];
            $this->assertEquals($case['expected'], $page->getDifficultyLabel());
        }
    }

    /**
     * 测试难度颜色类
     * Test Case ID: TC-QD-UNIT-002
     */
    public function test_get_difficulty_color(): void
    {
        $page = new QuestionDetailPage();

        // 测试不同难度值对应的颜色类
        $testCases = [
            ['difficulty' => 0.2, 'expected' => 'bg-green-100 text-green-700'],
            ['difficulty' => 0.3, 'expected' => 'bg-green-100 text-green-700'],
            ['difficulty' => 0.4, 'expected' => 'bg-yellow-100 text-yellow-700'],
            ['difficulty' => 0.5, 'expected' => 'bg-yellow-100 text-yellow-700'],
            ['difficulty' => 0.6, 'expected' => 'bg-yellow-100 text-yellow-700'],
            ['difficulty' => 0.7, 'expected' => 'bg-red-100 text-red-700'],
            ['difficulty' => 0.8, 'expected' => 'bg-red-100 text-red-700'],
            ['difficulty' => 0.9, 'expected' => 'bg-red-100 text-red-700'],
        ];

        foreach ($testCases as $case) {
            $page->questionData = ['difficulty' => $case['difficulty']];
            $this->assertEquals($case['expected'], $page->getDifficultyColor());
        }
    }

    /**
     * 测试默认难度值
     * Test Case ID: TC-QD-UNIT-003
     */
    public function test_default_difficulty(): void
    {
        $page = new QuestionDetailPage();

        // 测试没有难度数据时的默认值
        $page->questionData = [];
        $this->assertEquals('中等', $page->getDifficultyLabel());
        $this->assertEquals('bg-yellow-100 text-yellow-700', $page->getDifficultyColor());
    }

    /**
     * 测试题库模式标题生成
     * Test Case ID: TC-QD-UNIT-004
     */
    public function test_get_title_for_bank_question(): void
    {
        $page = new QuestionDetailPage();
        $page->sourceType = 'bank';
        $page->questionData = [
            'stem' => '这是一道用于测试的数学题目，它比较长，需要截断显示'
        ];

        $title = $page->getTitle();
        $this->assertStringContainsString('题库详情', $title);
        $this->assertStringContainsString('...', $title);
    }

    /**
     * 测试错题模式标题生成
     * Test Case ID: TC-QD-UNIT-005
     */
    public function test_get_title_for_mistake(): void
    {
        $page = new QuestionDetailPage();
        $page->sourceType = 'mistake';
        $page->mistakeData = [
            'question' => [
                'question_number' => '第5题'
            ]
        ];
        $page->mistakeId = 'test-mid-123';

        $title = $page->getTitle();
        $this->assertStringContainsString('错题分析', $title);
        $this->assertStringContainsString('第5题', $title);
    }

    /**
     * 测试默认标题
     * Test Case ID: TC-QD-UNIT-006
     */
    public function test_get_default_title(): void
    {
        $page = new QuestionDetailPage();
        $page->sourceType = 'unknown';

        $title = $page->getTitle();
        $this->assertEquals('题目详情', $title);
    }

    /**
     * 测试题库模式的面包屑导航
     * Test Case ID: TC-QD-UNIT-007
     */
    public function test_get_breadcrumbs_for_bank(): void
    {
        $page = new QuestionDetailPage();
        $page->sourceType = 'bank';

        $breadcrumbs = $page->getBreadcrumbs();

        $this->assertCount(3, $breadcrumbs);
        $this->assertEquals('首页', $breadcrumbs[0]['name']);
        $this->assertEquals('题库管理', $breadcrumbs[1]['name']);
        $this->assertEquals(url('/admin'), $breadcrumbs[0]['url']);
        $this->assertEquals(url('/admin/question-management'), $breadcrumbs[1]['url']);
        $this->assertEmpty($breadcrumbs[2]['url']);
    }

    /**
     * 测试错题模式的面包屑导航
     * Test Case ID: TC-QD-UNIT-008
     */
    public function test_get_breadcrumbs_for_mistake(): void
    {
        $page = new QuestionDetailPage();
        $page->sourceType = 'mistake';

        $breadcrumbs = $page->getBreadcrumbs();

        $this->assertCount(3, $breadcrumbs);
        $this->assertEquals('首页', $breadcrumbs[0]['name']);
        $this->assertEquals('错题本', $breadcrumbs[1]['name']);
        $this->assertEquals(url('/admin'), $breadcrumbs[0]['url']);
        $this->assertEquals(url('/admin/mistake-book'), $breadcrumbs[1]['url']);
        $this->assertEmpty($breadcrumbs[2]['url']);
    }

    /**
     * 测试知识点名称返回（当数据为空时）
     * Test Case ID: TC-QD-UNIT-009
     */
    public function test_get_knowledge_point_name_empty(): void
    {
        $page = new QuestionDetailPage();
        $page->questionData = [];
        $page->mistakeData = [];

        $name = $page->getKnowledgePointName();
        $this->assertEquals('未知知识点', $name);
    }

    /**
     * 测试从题库数据获取知识点名称
     * Test Case ID: TC-QD-UNIT-010
     */
    public function test_get_knowledge_point_name_from_question_data(): void
    {
        $page = new QuestionDetailPage();
        $page->questionData = ['kp_code' => 'P01'];

        $name = $page->getKnowledgePointName();
        // 知识点代码应该被转换为实际名称
        $this->assertEquals('整式的概念', $name);
    }

    /**
     * 测试从错题数据获取知识点名称
     * Test Case ID: TC-QD-UNIT-011
     */
    public function test_get_knowledge_point_name_from_mistake_data(): void
    {
        $page = new QuestionDetailPage();
        $page->mistakeData = [
            'question' => ['kp_code' => 'P02']
        ];

        $name = $page->getKnowledgePointName();
        $this->assertEquals('同类项合并', $name);
    }

    /**
     * 测试题库数据优先级高于错题数据
     * Test Case ID: TC-QD-UNIT-012
     */
    public function test_question_data_priority_over_mistake_data(): void
    {
        $page = new QuestionDetailPage();
        $page->questionData = ['kp_code' => 'P01'];
        $page->mistakeData = [
            'question' => ['kp_code' => 'P02']
        ];

        $name = $page->getKnowledgePointName();
        // 题库数据优先级更高
        $this->assertEquals('整式的概念', $name);
    }
}