<?php

namespace Tests\Unit\Services\Teachers;

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

class TeacherAssignmentServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_assign_creates_teacher_class_row(): void
    {
        $this->seedConfig();
        $teacherId = $this->seedTeacherUser();
        $classSectionId = $this->seedClassSection();

        $service = new TeacherAssignmentService(new TeacherConfigService());
        $result = $service->assign([
            'teacher_id' => $teacherId,
            'class_section_id' => $classSectionId,
            'teacher_role' => 'teacher',
        ]);

        $this->assertTrue($result['ok']);
        $this->assertDatabaseHas('teacher_class', [
            'teacher_id' => $teacherId,
            'class_section_id' => $classSectionId,
            'position' => 'main',
        ]);
    }

    public function test_delete_removes_assignment(): void
    {
        $this->seedConfig();
        $teacherId = $this->seedTeacherUser();
        $classSectionId = $this->seedClassSection();

        DB::table('teacher_class')->insert([
            'teacher_id' => $teacherId,
            'class_section_id' => $classSectionId,
            'position' => 'ta',
            'school_year' => '2025-2026',
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        $service = new TeacherAssignmentService(new TeacherConfigService());
        $result = $service->delete([
            'teacher_id' => $teacherId,
            'class_section_id' => $classSectionId,
            'position' => 'ta',
        ]);

        $this->assertTrue($result['ok']);
        $this->assertDatabaseMissing('teacher_class', [
            'teacher_id' => $teacherId,
            'class_section_id' => $classSectionId,
            'position' => 'ta',
        ]);
    }

    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' => 'Alex',
            'lastname' => 'Teacher',
            'cellphone' => '2222222222',
            'email' => 'teacher.assignment@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' => 201,
            'class_section_name' => '2A',
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);

        return 201;
    }
}
