<?php

namespace Tests\Feature\Api\V1\Classes;

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

class ClassSectionControllerTest extends TestCase
{
    use RefreshDatabase;

    public function test_index_returns_sections(): void
    {
        $user = $this->createUser();
        Sanctum::actingAs($user);

        DB::table('classes')->insert([
            'class_name' => 'Class 1',
            'schedule' => 'Monday',
            'capacity' => 30,
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);

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

        $response = $this->getJson('/api/v1/class-sections');

        $response->assertOk();
        $response->assertJsonPath('data.sections.0.class_section_name', '1-A');
    }

    public function test_store_requires_admin(): void
    {
        $user = $this->createUser();
        Sanctum::actingAs($user);

        $response = $this->postJson('/api/v1/class-sections', [
            'class_id' => 1,
            'class_section_id' => 101,
            'class_section_name' => '1-A',
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);

        $response->assertForbidden();
    }

    public function test_store_creates_class_section(): void
    {
        $admin = $this->createUser('admin');
        Sanctum::actingAs($admin);

        DB::table('classes')->insert([
            'class_name' => 'Class 1',
            'schedule' => 'Monday',
            'capacity' => 30,
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);

        $response = $this->postJson('/api/v1/class-sections', [
            'class_id' => 1,
            'class_section_id' => 101,
            'class_section_name' => '1-A',
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);

        $response->assertCreated();
        $this->assertDatabaseHas('classSection', [
            'class_section_id' => 101,
            'class_section_name' => '1-A',
        ]);
    }

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

        $user = $this->createUser();
        Sanctum::actingAs($user);

        $response = $this->putJson('/api/v1/class-sections/1', [
            'class_section_name' => '1-B',
        ]);

        $response->assertForbidden();
    }

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

        $user = $this->createUser();
        Sanctum::actingAs($user);

        $response = $this->getJson('/api/v1/class-sections/1');

        $response->assertOk();
        $response->assertJsonPath('data.section.class_section_name', '1-A');
    }

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

        $admin = $this->createUser('admin');
        Sanctum::actingAs($admin);

        $response = $this->putJson('/api/v1/class-sections/1', [
            'class_section_name' => '1-B',
        ]);

        $response->assertOk();
        $this->assertDatabaseHas('classSection', [
            'id' => 1,
            'class_section_name' => '1-B',
        ]);
    }

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

        $admin = $this->createUser('admin');
        Sanctum::actingAs($admin);

        $response = $this->deleteJson('/api/v1/class-sections/1');

        $response->assertOk();
        $this->assertDatabaseMissing('classSection', [
            'id' => 1,
        ]);
    }

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

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

        DB::table('students')->insert([
            'school_id' => 'S-1',
            'firstname' => 'Student',
            'lastname' => 'One',
            'age' => 8,
            'gender' => 'Male',
            'photo_consent' => 1,
            'parent_id' => 1,
            'year_of_registration' => '2025',
            'school_year' => '2025-2026',
            'semester' => 'Fall',
            'is_active' => 1,
        ]);

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

        $teacher = $this->createUser();
        Sanctum::actingAs($teacher);

        $response = $this->getJson('/api/v1/class-sections/101/attendance');

        $response->assertOk();
        $response->assertJsonPath('data.attendance.class_section_id', 101);
        $response->assertJsonCount(1, 'data.attendance.students');
    }

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

        $admin = $this->createUser('admin');
        Sanctum::actingAs($admin);

        $response = $this->postJson('/api/v1/class-sections/seed-defaults');

        $response->assertOk();
        $this->assertDatabaseHas('classes', [
            'class_name' => 'Class 1',
        ]);
    }

    public function test_validation_rejects_invalid_payload(): void
    {
        $admin = $this->createUser('admin');
        Sanctum::actingAs($admin);

        $response = $this->postJson('/api/v1/class-sections', [
            'class_id' => 'bad',
        ]);

        $response->assertStatus(422);
        $response->assertJsonStructure(['message', 'errors']);
    }

    private function createUser(string $roleName = 'teacher'): User
    {
        DB::table('roles')->insert([
            'id' => 1,
            'name' => $roleName,
            'priority' => 1,
        ]);

        DB::table('users')->insert([
            'school_id' => 1,
            'firstname' => 'Test',
            'lastname' => 'User',
            'cellphone' => '5555555555',
            'email' => $roleName . '@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('user_roles')->insert([
            'user_id' => 1,
            'role_id' => 1,
        ]);

        return User::query()->findOrFail(1);
    }
}
