<?php

namespace Tests\Unit\Services\Assignment;

use App\Models\StudentClass;
use App\Models\TeacherClass;
use App\Services\Assignment\AssignmentDataLoaderService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class AssignmentDataLoaderServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_load_teacher_classes_includes_teacher_name(): void
    {
        $this->seedTeacherData();

        $service = new AssignmentDataLoaderService(new TeacherClass(), new StudentClass());
        $rows = $service->loadTeacherClasses('2025-2026');

        $this->assertCount(1, $rows);
        $this->assertSame('Teacher One', $rows->first()->teacher_full_name);
    }

    public function test_load_student_classes_filters_inactive(): void
    {
        $this->seedStudentData();

        $service = new AssignmentDataLoaderService(new TeacherClass(), new StudentClass());
        $rows = $service->loadStudentClasses('2025-2026');

        $this->assertCount(1, $rows);
        $this->assertSame(1, $rows->first()->student_id);
    }

    private function seedTeacherData(): void
    {
        DB::table('users')->insert([
            'id' => 1,
            'school_id' => 1,
            'firstname' => 'Teacher',
            'lastname' => 'One',
            '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',
        ]);

        DB::table('teacher_class')->insert([
            'teacher_id' => 1,
            'class_section_id' => 101,
            'position' => 'main',
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);
    }

    private function seedStudentData(): void
    {
        DB::table('students')->insert([
            [
                'id' => 1,
                'school_id' => 'S-1',
                'firstname' => 'Student',
                'lastname' => 'Active',
                'age' => 8,
                'gender' => 'Male',
                'photo_consent' => 1,
                'parent_id' => 1,
                'year_of_registration' => '2025',
                'school_year' => '2025-2026',
                'semester' => 'Fall',
                'is_active' => 1,
            ],
            [
                'id' => 2,
                'school_id' => 'S-2',
                'firstname' => 'Student',
                'lastname' => 'Inactive',
                'age' => 9,
                'gender' => 'Male',
                'photo_consent' => 1,
                'parent_id' => 1,
                'year_of_registration' => '2025',
                'school_year' => '2025-2026',
                'semester' => 'Fall',
                'is_active' => 0,
            ],
        ]);

        DB::table('student_class')->insert([
            [
                'student_id' => 1,
                'class_section_id' => 101,
                'semester' => 'Fall',
                'school_year' => '2025-2026',
            ],
            [
                'student_id' => 2,
                'class_section_id' => 101,
                'semester' => 'Fall',
                'school_year' => '2025-2026',
            ],
        ]);
    }
}
