<?php

declare(strict_types=1);

namespace Tests\Feature;

use App\Models\QuestionBank;
use App\Models\User;
use App\Services\QuestionImportService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Validation\ValidationException;
use Tests\TestCase;

final class QuestionImportTest extends TestCase
{
    use RefreshDatabase;

    public function test_question_json_import_detects_question_types(): void
    {
        $user = User::factory()->create(['role' => 'admin']);
        $bank = QuestionBank::create([
            'owner_id' => $user->id,
            'name' => '测试题库',
            'visibility' => 'private',
            'is_active' => true,
        ]);

        $json = file_get_contents(base_path('question.json'));
        $job = app(QuestionImportService::class)->importJsonText($bank, $user, $json);

        $this->assertSame(3, $job->success_count);
        $this->assertDatabaseHas('questions', ['question_bank_id' => $bank->id, 'type' => 'single']);
        $this->assertDatabaseHas('questions', ['question_bank_id' => $bank->id, 'type' => 'multiple']);
        $this->assertDatabaseHas('questions', ['question_bank_id' => $bank->id, 'type' => 'judge']);
    }

    public function test_duplicate_questions_are_skipped(): void
    {
        $user = User::factory()->create(['role' => 'admin']);
        $bank = QuestionBank::create([
            'owner_id' => $user->id,
            'name' => '测试题库',
            'visibility' => 'private',
            'is_active' => true,
        ]);

        $json = file_get_contents(base_path('question.json'));
        app(QuestionImportService::class)->importJsonText($bank, $user, $json);
        $job = app(QuestionImportService::class)->importJsonText($bank, $user, $json);

        $this->assertSame(0, $job->success_count);
        $this->assertSame(3, $job->skipped_count);
    }

    public function test_invalid_row_rolls_back_entire_import_batch(): void
    {
        $user = User::factory()->create(['role' => 'admin']);
        $bank = QuestionBank::create([
            'owner_id' => $user->id,
            'name' => '回滚题库',
            'visibility' => 'private',
            'is_active' => true,
        ]);

        $rows = [
            [
                'questionId' => 'ok-1',
                'questionText' => '有效题目',
                'options' => [
                    ['text' => 'A', 'correct' => true],
                    ['text' => 'B', 'correct' => false],
                ],
            ],
            [
                'questionId' => 'bad-1',
                'questionText' => '无正确答案题目',
                'options' => [
                    ['text' => 'A', 'correct' => false],
                    ['text' => 'B', 'correct' => false],
                ],
            ],
        ];

        try {
            app(QuestionImportService::class)->importRows($bank, $user, $rows, 'json');
            $this->fail('Expected import validation exception.');
        } catch (ValidationException) {
            $this->assertDatabaseCount('questions', 0);
            $this->assertDatabaseCount('import_jobs', 0);
        }
    }
}
