<?php

namespace Tests\Unit\Services\ClassProgress;

use App\Models\ClassProgressReport;
use App\Models\User;
use App\Services\ClassProgress\ClassProgressAttachmentService;
use App\Services\ClassProgress\ClassProgressMutationService;
use App\Services\ClassProgress\ClassProgressRuleService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class ClassProgressMutationServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_create_reports_persists_multiple_subjects(): void
    {
        $user = $this->createUser();
        $this->seedClassSection();
        $this->assignTeacher($user->id, 101);

        $service = new ClassProgressMutationService(
            new ClassProgressRuleService(),
            new ClassProgressAttachmentService()
        );

        $payload = [
            'class_section_id' => 101,
            'week_start' => '2025-09-07',
            'week_end' => '2025-09-13',
            'covered_islamic' => 'Lesson A',
            'covered_quran' => 'Lesson B',
        ];

        $reports = $service->createReports($user, $payload, []);

        $this->assertCount(2, $reports);
        $this->assertDatabaseCount('class_progress_reports', 2);
    }

    public function test_update_report_changes_status(): void
    {
        $report = ClassProgressReport::factory()->create([
            'status' => 'on_track',
        ]);

        $service = new ClassProgressMutationService(
            new ClassProgressRuleService(),
            new ClassProgressAttachmentService()
        );

        $updated = $service->updateReport($report, ['status' => 'behind'], []);

        $this->assertSame('behind', $updated->status);
    }

    public function test_delete_report_removes_record(): void
    {
        $report = ClassProgressReport::factory()->create();
        $service = new ClassProgressMutationService(
            new ClassProgressRuleService(),
            new ClassProgressAttachmentService()
        );

        $service->deleteReport($report);

        $this->assertDatabaseMissing('class_progress_reports', ['id' => $report->id]);
    }

    private function seedClassSection(): void
    {
        DB::table('classSection')->insert([
            'id' => 1,
            'class_id' => 1,
            'class_section_id' => 101,
            'class_section_name' => '1-A',
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);
    }

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

    private function createUser(): User
    {
        DB::table('users')->insert([
            'school_id' => 1,
            'firstname' => 'Teacher',
            'lastname' => 'User',
            'cellphone' => '5555555555',
            'email' => 'teacher@example.com',
            'address_street' => '123 Main',
            'city' => 'City',
            'state' => 'ST',
            'zip' => '12345',
            'accept_school_policy' => 1,
            'is_verified' => 1,
            'status' => 'Active',
            'is_suspended' => 0,
            'failed_attempts' => 0,
            'password' => bcrypt('secret'),
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);

        return User::query()->where('email', 'teacher@example.com')->firstOrFail();
    }
}
