<?php

namespace Tests\Unit\Services\Teachers;

use App\Services\Teachers\TeacherConfigService;
use App\Services\Teachers\TeacherDashboardService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class TeacherDashboardServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_class_view_returns_students(): void
    {
        $this->seedConfig();
        $teacherId = $this->seedTeacherUser();
        $classSectionId = $this->seedClassSection();
        $this->seedTeacherAssignment($teacherId, $classSectionId);
        $this->seedStudent($teacherId, $classSectionId);

        $service = new TeacherDashboardService(new TeacherConfigService());
        $data = $service->classView($teacherId, $classSectionId);

        $this->assertSame($classSectionId, $data['active_class_section_id']);
        $this->assertNotEmpty($data['classes']);
        $this->assertNotEmpty($data['students']);
    }

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

    private function seedTeacherUser(): int
    {
        $roleId = DB::table('roles')->insertGetId([
            'name' => 'teacher',
        ]);

        $userId = DB::table('users')->insertGetId([
            'firstname' => 'Tina',
            'lastname' => 'Teacher',
            'cellphone' => '1111111111',
            'email' => 'teacher.dashboard@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',
        ]);

        DB::table('user_roles')->insert([
            'user_id' => $userId,
            'role_id' => $roleId,
        ]);

        return $userId;
    }

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

        return 101;
    }

    private function seedTeacherAssignment(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 seedStudent(int $parentId, int $classSectionId): void
    {
        $studentId = DB::table('students')->insertGetId([
            'school_id' => 'S-500',
            'firstname' => 'Sam',
            'lastname' => 'Student',
            'dob' => '2014-09-01',
            'age' => 11,
            'gender' => 'Male',
            'photo_consent' => 1,
            'parent_id' => $parentId,
            'year_of_registration' => '2025',
            'school_year' => '2025-2026',
            'semester' => 'Fall',
        ]);

        DB::table('student_class')->insert([
            'student_id' => $studentId,
            'class_section_id' => $classSectionId,
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);
    }
}
