<?php

namespace Tests\Unit;

use Tests\TestCase;
use App\Filament\Pages\StudentKnowledgeGraphPage;
use App\Livewire\StudentKnowledgeGraph;
use Illuminate\Foundation\Testing\LazilyRefreshDatabase;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\DB;

class StudentKnowledgeGraphTest extends TestCase
{
    use LazilyRefreshDatabase;

    /** @test */
    public function page_can_be_accessed()
    {
        $response = $this->get('/admin/student-knowledge-graph-page');

        $response->assertStatus(200);
        $response->assertSee('学生知识图谱');
    }

    /** @test */
    public function page_has_correct_navigation()
    {
        $page = new StudentKnowledgeGraphPage();

        $this->assertEquals('学习分析', $page->getNavigationGroup());
        $this->assertEquals('学生知识图谱', $page->getNavigationLabel());
        $this->assertEquals(2, $page->getNavigationSort());
    }

    /** @test */
    public function page_renders_correct_view()
    {
        $page = new StudentKnowledgeGraphPage();

        $this->assertEquals('filament.pages.student-knowledge-graph-page', $page->getView());
    }

    /** @test */
    public function livewire_component_initializes_with_empty_data()
    {
        $component = new StudentKnowledgeGraph();

        $this->assertNull($component->selectedStudentId);
        $this->assertNull($component->selectedStudent);
        $this->assertEmpty($component->knowledgePoints);
        $this->assertEmpty($component->masteryData);
        $this->assertEmpty($component->statistics);
        $this->assertFalse($component->isLoading);
    }

    /** @test */
    public function load_students_populates_student_list()
    {
        // 创建测试数据
        DB::table('students')->insert([
            'student_id' => 1001,
            'name' => '张三',
            'grade' => '高一',
            'class_name' => '1班',
            'teacher_id' => 1,
        ]);

        DB::table('students')->insert([
            'student_id' => 1002,
            'name' => '李四',
            'grade' => '高一',
            'class_name' => '2班',
            'teacher_id' => 1,
        ]);

        $component = new StudentKnowledgeGraph();
        $component->loadStudents();

        $this->assertCount(2, $component->students);
        $this->assertEquals('张三 (高一-1班)', $component->students[0]['label']);
        $this->assertEquals(1001, $component->students[0]['id']);
    }

    /** @test */
    public function selecting_student_loads_data()
    {
        // 创建测试数据
        DB::table('students')->insert([
            'student_id' => 1001,
            'name' => '张三',
            'grade' => '高一',
            'class_name' => '1班',
            'teacher_id' => 1,
        ]);

        $component = new StudentKnowledgeGraph();

        // 模拟HTTP响应
        Http::fake([
            'localhost:5010/*' => Http::response([
                'masteries' => [
                    ['kp_code' => 'R01', 'mastery_level' => 0.85, 'confidence_level' => 0.8],
                ],
                'total_knowledge_points' => 1,
                'average_mastery' => 0.85,
                'high_mastery_count' => 1,
                'medium_mastery_count' => 0,
                'low_mastery_count' => 0,
            ], 200),
        ]);

        $component->loadStudentData(1001);

        $this->assertNotNull($component->selectedStudent);
        $this->assertEquals('张三', $component->selectedStudent->name);
        $this->assertNotEmpty($component->masteryData);
    }

    /** @test */
    public function mastery_color_returns_correct_colors()
    {
        $component = new StudentKnowledgeGraph();

        // 使用反射获取私有方法
        $reflection = new \ReflectionClass($component);
        $method = $reflection->getMethod('getMasteryColor');
        $method->setAccessible(true);

        $this->assertEquals('#10b981', $method->invoke($component, 0.9)); // 优秀
        $this->assertEquals('#3b82f6', $method->invoke($component, 0.7)); // 良好
        $this->assertEquals('#f59e0b', $method->invoke($component, 0.5)); // 中等
        $this->assertEquals('#f97316', $method->invoke($component, 0.3)); // 待提高
        $this->assertEquals('#ef4444', $method->invoke($component, 0.1)); // 薄弱
    }

    /** @test */
    public function mastery_size_returns_correct_sizes()
    {
        $component = new StudentKnowledgeGraph();

        // 使用反射获取私有方法
        $reflection = new \ReflectionClass($component);
        $method = $reflection->getMethod('getMasterySize');
        $method->setAccessible(true);

        $this->assertEquals(36, $method->invoke($component, 0.9)); // 36px
        $this->assertEquals(28, $method->invoke($component, 0.7)); // 28px
        $this->assertEquals(10, $method->invoke($component, 0.1)); // 最小10px
    }

    /** @test */
    public function mock_data_is_used_when_api_fails()
    {
        // 创建测试数据
        DB::table('students')->insert([
            'student_id' => 1001,
            'name' => '张三',
            'grade' => '高一',
            'class_name' => '1班',
            'teacher_id' => 1,
        ]);

        $component = new StudentKnowledgeGraph();

        // 模拟API调用失败
        Http::fake([
            'localhost:5010/*' => Http::response([], 500),
        ]);

        $component->loadStudentData(1001);

        // 应该使用模拟数据
        $this->assertNotEmpty($component->knowledgePoints);
        $this->assertArrayHasKey('nodes', $component->knowledgePoints);
        $this->assertArrayHasKey('links', $component->knowledgePoints);
        $this->assertNotEmpty($component->masteryData);
        $this->assertNotEmpty($component->statistics);
    }

    /** @test */
    public function knowledge_graph_data_structure_is_correct()
    {
        $component = new StudentKnowledgeGraph();

        // 设置模拟数据
        $component->knowledgePoints = [
            'nodes' => [
                [
                    'id' => 'R01',
                    'label' => '有理数',
                    'mastery' => 0.85,
                    'color' => '#10b981',
                    'size' => 34,
                ],
            ],
            'links' => [
                [
                    'source' => 'R01',
                    'target' => 'R02',
                    'strength' => 0.9,
                    'type' => 'must',
                ],
            ],
        ];

        $this->assertCount(1, $component->knowledgePoints['nodes']);
        $this->assertCount(1, $component->knowledgePoints['links']);
        $this->assertEquals('R01', $component->knowledgePoints['nodes'][0]['id']);
        $this->assertEquals('#10b981', $component->knowledgePoints['nodes'][0]['color']);
    }

    /** @test */
    public function reset_data_clears_all_properties()
    {
        $component = new StudentKnowledgeGraph();

        // 设置一些数据
        $component->selectedStudentId = 1001;
        $component->knowledgePoints = ['nodes' => []];
        $component->masteryData = ['test' => 'data'];
        $component->statistics = ['count' => 1];

        // 使用反射调用私有方法
        $reflection = new \ReflectionClass($component);
        $method = $reflection->getMethod('resetData');
        $method->setAccessible(true);
        $method->invoke($component);

        $this->assertNull($component->selectedStudent);
        $this->assertEmpty($component->knowledgePoints);
        $this->assertEmpty($component->masteryData);
        $this->assertEmpty($component->statistics);
        $this->assertEmpty($component->dependencies);
        $this->assertEmpty($component->learningPath);
    }
}
