<?php

namespace Tests\Feature;

use App\Models\ExamPaper;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class ExamPdfPreviewTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();
        
        // 创建测试用的 exam_papers 表
        \Illuminate\Support\Facades\Schema::connection('remote_mysql')->dropIfExists('exam_papers');
        \Illuminate\Support\Facades\Schema::connection('remote_mysql')->create('exam_papers', function (\Illuminate\Database\Schema\Blueprint $table) {
            $table->string('id', 191)->primary();
            $table->string('title');
            $table->integer('total_score');
            $table->integer('duration');
            $table->timestamps();
        });
    }

    /** @test */
    public function it_displays_pdf_preview_for_existing_exam()
    {
        $exam = ExamPaper::create([
            'id' => 'test_pdf_exam',
            'title' => 'PDF 测试试卷',
            'total_score' => 100,
            'duration' => 120,
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        $response = $this->get(route('filament.admin.auth.intelligent-exam.pdf', ['paper_id' => $exam->id]));

        $response->assertStatus(200)
                 ->assertSee('PDF 测试试卷')
                 ->assertSee('选择题')
                 ->assertSee('填空题')
                 ->assertSee('解答题');
    }

    /** @test */
    public function it_returns_404_for_non_existent_exam()
    {
        $response = $this->get(route('filament.admin.auth.intelligent-exam.pdf', ['paper_id' => 'non_existent']));

        $response->assertStatus(404);
    }

    /** @test */
    public function it_displays_omr_markers_for_all_question_types()
    {
        $exam = ExamPaper::create([
            'id' => 'omr_test_exam',
            'title' => 'OMR 标记测试',
            'total_score' => 100,
            'duration' => 120,
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        $response = $this->get(route('filament.admin.auth.intelligent-exam.pdf', ['paper_id' => $exam->id]));

        // 检查 OMR 标记的 CSS 类是否存在
        $response->assertSee('omr-marker', false);
    }

    /** @test */
    public function it_correctly_renders_newlines_in_questions()
    {
        $exam = ExamPaper::create([
            'id' => 'newline_test_exam',
            'title' => '换行测试试卷',
            'total_score' => 100,
            'duration' => 120,
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        $response = $this->get(route('filament.admin.auth.intelligent-exam.pdf', ['paper_id' => $exam->id]));

        // 检查换行符是否被转换为 <br> 标签
        $response->assertSee('<br', false);
    }

    /** @test */
    public function it_creates_exam_papers_table_if_not_exists()
    {
        // 删除表
        \Illuminate\Support\Facades\Schema::connection('remote_mysql')->dropIfExists('exam_papers');

        // 访问 PDF 页面应该自动创建表
        $response = $this->get(route('filament.admin.auth.intelligent-exam.pdf', ['paper_id' => 'test']));

        // 表应该已经被创建
        $this->assertTrue(\Illuminate\Support\Facades\Schema::connection('remote_mysql')->hasTable('exam_papers'));
    }

    protected function tearDown(): void
    {
        \Illuminate\Support\Facades\Schema::connection('remote_mysql')->dropIfExists('exam_papers');
        parent::tearDown();
    }
}
