<?php

namespace Tests\Feature\Api\V1\Exams;

use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\DB;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;

class ExamDraftControllerTest extends TestCase
{
    use RefreshDatabase;

    public function test_teacher_store_creates_draft(): void
    {
        $this->seedConfig();
        $teacher = $this->seedTeacher();
        $classSectionId = $this->seedClassSection();
        $this->seedAssignment($teacher->id, $classSectionId);

        Sanctum::actingAs($teacher);
        $file = UploadedFile::fake()->create('draft.docx', 10, 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');

        $response = $this->post('/api/v1/exams/drafts/teacher', [
            'class_section_id' => $classSectionId,
            'exam_type' => 'Final Exam',
            'description' => 'Draft',
            'draft_file' => $file,
        ]);

        $response->assertStatus(201);
        $this->assertDatabaseHas('exam_drafts', [
            'teacher_id' => $teacher->id,
            'class_section_id' => $classSectionId,
            'status' => 'submitted',
        ]);
    }

    public function test_admin_review_updates_status(): void
    {
        $this->seedConfig();
        $admin = $this->seedAdmin();
        $draftId = $this->seedDraft();

        Sanctum::actingAs($admin);
        $response = $this->postJson('/api/v1/exams/drafts/admin/review', [
            'draft_id' => $draftId,
            'review_status' => 'reviewed',
        ]);

        $response->assertOk();
        $this->assertDatabaseHas('exam_drafts', [
            'id' => $draftId,
            'status' => 'reviewed',
        ]);
    }

    private function seedConfig(): void
    {
        DB::table('configuration')->insert([
            ['config_key' => 'school_year', 'config_value' => '2025-2026'],
            ['config_key' => 'semester', 'config_value' => 'Fall'],
        ]);
    }

    private function seedTeacher(): User
    {
        $userId = DB::table('users')->insertGetId([
            'firstname' => 'Teacher',
            'lastname' => 'User',
            'cellphone' => '1111111111',
            'email' => 'teacher.exam.feature@example.com',
            'address_street' => '123 Street',
            'city' => 'City',
            'state' => 'ST',
            'zip' => '12345',
            'accept_school_policy' => 1,
            'password' => bcrypt('password'),
            'user_type' => 'primary',
            'semester' => 'Fall',
            'school_year' => '2025-2026',
            'status' => 'Active',
        ]);

        return User::query()->findOrFail($userId);
    }

    private function seedAdmin(): User
    {
        $userId = DB::table('users')->insertGetId([
            'firstname' => 'Admin',
            'lastname' => 'User',
            'cellphone' => '2222222222',
            'email' => 'admin.exam.feature@example.com',
            'address_street' => '123 Street',
            'city' => 'City',
            'state' => 'ST',
            'zip' => '12345',
            'accept_school_policy' => 1,
            'password' => bcrypt('password'),
            'user_type' => 'primary',
            'semester' => 'Fall',
            'school_year' => '2025-2026',
            'status' => 'Active',
        ]);

        return User::query()->findOrFail($userId);
    }

    private function seedClassSection(): int
    {
        DB::table('classSection')->insert([
            'class_id' => 1,
            'class_section_id' => 802,
            'class_section_name' => '8B',
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);

        return 802;
    }

    private function seedAssignment(int $teacherId, int $classSectionId): void
    {
        DB::table('teacher_class')->insert([
            'teacher_id' => $teacherId,
            'class_section_id' => $classSectionId,
            'position' => 'main',
            'school_year' => '2025-2026',
            'created_at' => now(),
            'updated_at' => now(),
        ]);
    }

    private function seedDraft(): int
    {
        return DB::table('exam_drafts')->insertGetId([
            'teacher_id' => 1,
            'class_section_id' => 802,
            'semester' => 'Fall',
            'school_year' => '2025-2026',
            'exam_type' => 'Quiz',
            'draft_title' => 'Quiz Draft',
            'description' => 'Draft',
            'status' => 'submitted',
            'version' => 1,
            'created_at' => now(),
            'updated_at' => now(),
        ]);
    }
}
