<?php

namespace Tests\Unit;

use Tests\TestCase;
use App\Filament\Pages\IntelligentExamGeneration;
use App\Services\KnowledgeGraphService;
use App\Services\LearningAnalyticsService;
use App\Services\QuestionBankService;
use Illuminate\Support\Facades\Http;

class IntelligentExamGenerationTest extends TestCase
{
    protected function setUp(): void
    {
        parent::setUp();
    }

    /** @test */
    public function it_can_access_intelligent_exam_generation_page()
    {
        // 模拟访问智能出卷页面
        $response = $this->get('/admin/intelligent-exam-generation');

        $response->assertStatus(200);
    }

    /** @test */
    public function it_can_load_knowledge_points()
    {
        // 测试知识点获取功能
        $knowledgeGraphService = app(KnowledgeGraphService::class);

        // 模拟listKnowledgePoints方法返回
        $result = $knowledgeGraphService->listKnowledgePoints(1, 100);

        $this->assertIsArray($result);
        $this->assertArrayHasKey('data', $result);
    }

    /** @test */
    public function it_can_validate_form_before_generation()
    {
        // 创建页面实例
        $page = new IntelligentExamGeneration();

        // 设置必填字段为空，应该验证失败
        $this->assertFalse($page->paperName === 'required');
    }

    /** @test */
    public function it_can_generate_exam_with_basic_params()
    {
        // 模拟智能出卷参数
        $params = [
            'student_id' => 'test_student_001',
            'total_questions' => 10,
            'kp_codes' => ['KP001', 'KP002'],
            'skills' => [],
            'question_type_ratio' => [
                '选择题' => 40,
                '填空题' => 30,
                '解答题' => 30,
            ],
            'difficulty_ratio' => [
                '基础' => 50,
                '中等' => 35,
                '拔高' => 15,
            ],
        ];

        $learningAnalyticsService = app(LearningAnalyticsService::class);

        // 调用智能出卷服务
        $result = $learningAnalyticsService->generateIntelligentExam($params);

        $this->assertIsArray($result);
        $this->assertArrayHasKey('success', $result);
    }

    /** @test */
    public function it_can_save_exam_to_database()
    {
        $examData = [
            'paper_name' => '测试试卷',
            'paper_description' => '这是一份测试试卷',
            'difficulty_category' => '基础',
            'questions' => [],
            'total_score' => 100,
            'total_questions' => 10,
        ];

        $questionBankService = app(QuestionBankService::class);
        $paperId = $questionBankService->saveExamToDatabase($examData);

        $this->assertNotNull($paperId);
        $this->assertIsString($paperId);
    }

    /** @test */
    public function it_can_filter_by_student_weakness()
    {
        $studentId = 'test_student_001';

        $learningAnalyticsService = app(LearningAnalyticsService::class);

        // 获取学生薄弱点
        $weaknesses = $learningAnalyticsService->getStudentWeaknesses($studentId, 10);

        $this->assertIsArray($weaknesses);

        // 验证薄弱点数据结构
        if (!empty($weaknesses)) {
            $firstWeakness = $weaknesses[0];
            $this->assertArrayHasKey('kp_code', $firstWeakness);
            $this->assertArrayHasKey('mastery', $firstWeakness);
        }
    }

    /** @test */
    public function it_can_auto_generate_questions()
    {
        $filters = [
            'kp_code' => 'KP001',
            'count' => 5,
            'difficulty_distribution' => [
                '基础' => 50,
                '中等' => 35,
                '拔高' => 15,
            ],
        ];

        $questionBankService = app(QuestionBankService::class);
        $result = $questionBankService->generateIntelligentQuestions($filters);

        $this->assertIsArray($result);
        $this->assertArrayHasKey('success', $result);
    }

    /** @test */
    public function it_can_export_exam_to_pdf()
    {
        $paperId = 'test_paper_001';

        $questionBankService = app(QuestionBankService::class);

        // 模拟导出PDF
        $pdfUrl = $questionBankService->exportExamToPdf($paperId);

        // 返回URL（可能是null或字符串）
        $this->assertTrue($pdfUrl === null || is_string($pdfUrl));
    }

    /** @test */
    public function it_validates_question_type_ratios_sum_to_100()
    {
        $page = new IntelligentExamGeneration();

        $ratios = $page->questionTypeRatio;
        $total = array_sum($ratios);

        $this->assertEquals(100, $total, '题型配比总和必须为100%');
    }

    /** @test */
    public function it_validates_difficulty_ratios_sum_to_100()
    {
        $page = new IntelligentExamGeneration();

        $ratios = $page->difficultyRatio;
        $total = array_sum($ratios);

        $this->assertEquals(100, $total, '难度配比总和必须为100%');
    }
}
