<?php

namespace Tests\Feature\Api\V1\ClassPrep;

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

class ClassPrepControllerTest extends TestCase
{
    use RefreshDatabase;

    public function test_sticker_counts_returns_payload(): void
    {
        $this->seedStickerData();

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

        $response = $this->getJson('/api/v1/class-prep/sticker-counts?school_year=2025-2026&semester=Fall');

        $response->assertOk();
        $response->assertJson([
            'ok' => true,
            'data' => [
                'totals' => [
                    'primary' => 6,
                    'secondary' => 1,
                    'students' => 2,
                ],
            ],
        ]);
    }

    public function test_students_by_class_returns_roster(): void
    {
        $this->seedStickerData();

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

        $response = $this->getJson('/api/v1/class-prep/classes/101/students?school_year=2025-2026');

        $response->assertOk();
        $this->assertCount(1, $response->json('students'));
        $this->assertSame('1-A', $response->json('students.0.registration_grade'));
    }

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

        DB::table('classes')->insert([
            [
                'id' => 1,
                'class_name' => 'Class 1',
                'schedule' => 'Sun',
                'capacity' => 20,
                'semester' => 'Fall',
                'school_year' => '2025-2026',
            ],
            [
                'id' => 2,
                'class_name' => 'Class 2',
                'schedule' => 'Sun',
                'capacity' => 20,
                'semester' => 'Fall',
                'school_year' => '2025-2026',
            ],
            [
                'id' => 3,
                'class_name' => 'Class 3',
                'schedule' => 'Sun',
                'capacity' => 20,
                'semester' => 'Fall',
                'school_year' => '2025-2026',
            ],
        ]);

        DB::table('classSection')->insert([
            [
                'id' => 1,
                'class_id' => 1,
                'class_section_id' => 101,
                'class_section_name' => '1-A',
                'semester' => 'Fall',
                'school_year' => '2025-2026',
            ],
            [
                'id' => 2,
                'class_id' => 2,
                'class_section_id' => 102,
                'class_section_name' => '5-B',
                'semester' => 'Fall',
                'school_year' => '2025-2026',
            ],
            [
                'id' => 3,
                'class_id' => 3,
                'class_section_id' => 103,
                'class_section_name' => 'Youth-1',
                '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,
                'is_new' => 0,
            ],
            [
                'school_id' => 'S-2',
                'firstname' => 'Student',
                'lastname' => 'Two',
                'age' => 10,
                'gender' => 'Female',
                'photo_consent' => 1,
                'parent_id' => 1,
                'year_of_registration' => '2025',
                'school_year' => '2025-2026',
                'semester' => 'Fall',
                'is_active' => 1,
                'is_new' => 0,
            ],
            [
                'school_id' => 'S-3',
                'firstname' => 'Student',
                'lastname' => 'Three',
                'age' => 12,
                'gender' => 'Female',
                'photo_consent' => 1,
                'parent_id' => 1,
                'year_of_registration' => '2025',
                'school_year' => '2025-2026',
                'semester' => 'Fall',
                'is_active' => 1,
                'is_new' => 1,
            ],
        ]);

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

    private function createUser(): User
    {
        DB::table('users')->insert([
            'school_id' => 1,
            'firstname' => 'Admin',
            'lastname' => 'User',
            'cellphone' => '5555555555',
            'email' => 'admin@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',
        ]);

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