<?php

namespace Tests\Feature\Api\V1\Administrator;

use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;

class TeacherClassAssignmentControllerTest extends TestCase
{
    use RefreshDatabase;

    public function test_index_returns_teachers_and_classes(): void
    {
        $this->seedConfig();
        $admin = $this->seedAdminUser();
        $this->seedTeacherUser();
        $this->seedClassSection();

        Sanctum::actingAs($admin);
        $response = $this->getJson('/api/v1/administrator/teacher-class/assignments?school_year=2025-2026');

        $response->assertOk();
        $response->assertJson(['ok' => true]);
        $this->assertNotEmpty($response->json('teachers'));
        $this->assertNotEmpty($response->json('classes'));
    }

    public function test_store_and_delete_assignment(): void
    {
        $this->seedConfig();
        $admin = $this->seedAdminUser();
        $teacher = $this->seedTeacherUser();
        $classSectionId = $this->seedClassSection();

        Sanctum::actingAs($admin);
        $storeResponse = $this->postJson('/api/v1/administrator/teacher-class/assign', [
            'teacher_id' => $teacher->id,
            'class_section_id' => $classSectionId,
            'teacher_role' => 'teacher',
            'school_year' => '2025-2026',
        ]);

        $storeResponse->assertOk();
        $this->assertDatabaseHas('teacher_class', [
            'teacher_id' => $teacher->id,
            'class_section_id' => $classSectionId,
            'position' => 'main',
        ]);

        $deleteResponse = $this->postJson('/api/v1/administrator/teacher-class/delete', [
            'teacher_id' => $teacher->id,
            'class_section_id' => $classSectionId,
            'position' => 'main',
            'school_year' => '2025-2026',
        ]);

        $deleteResponse->assertOk();
        $this->assertDatabaseMissing('teacher_class', [
            'teacher_id' => $teacher->id,
            'class_section_id' => $classSectionId,
            'position' => 'main',
        ]);
    }

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

    private function seedAdminUser(): User
    {
        $userId = DB::table('users')->insertGetId([
            'firstname' => 'Admin',
            'lastname' => 'User',
            'cellphone' => '5555555555',
            'email' => 'admin.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',
        ]);

        return User::query()->findOrFail($userId);
    }

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

        $userId = DB::table('users')->insertGetId([
            'firstname' => 'Olivia',
            'lastname' => 'Teacher',
            'cellphone' => '6666666666',
            'email' => 'teacher.assignment.admin@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 User::query()->findOrFail($userId);
    }

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

        return 401;
    }
}
