<?php

namespace Tests\Unit\Services\ClassProgress;

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

class ClassProgressQueryServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_list_reports_filters_by_class_section(): void
    {
        $user = $this->createUser();
        ClassProgressReport::factory()->create([
            'teacher_id' => $user->id,
            'class_section_id' => 101,
        ]);
        ClassProgressReport::factory()->create([
            'teacher_id' => $user->id,
            'class_section_id' => 202,
        ]);

        $service = new ClassProgressQueryService(new ClassProgressRuleService());
        $results = $service->listReports($user, ['class_section_id' => 101]);

        $this->assertSame(1, $results->total());
    }

    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();
    }
}
