<?php

namespace Tests\Feature\Api\V1\ClassPreparation;

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

class ClassPreparationControllerTest extends TestCase
{
    use RefreshDatabase;

    public function test_index_returns_prep_payload(): void
    {
        $this->seedPrepData();

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

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

        $response->assertOk();
        $response->assertJson([
            'ok' => true,
            'schoolYear' => '2025-2026',
            'semester' => 'Fall',
        ]);
        $this->assertNotEmpty($response->json('results'));
    }

    public function test_mark_printed_creates_snapshots(): void
    {
        $this->seedPrepData();

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

        $response = $this->postJson('/api/v1/class-prep/mark-printed', [
            'school_year' => '2025-2026',
            'semester' => 'Fall',
            'class_section_ids' => [101],
        ]);

        $response->assertOk();
        $response->assertJson([
            'ok' => true,
        ]);

        $this->assertDatabaseHas('class_preparation_log', [
            'class_section_id' => 101,
            'school_year' => '2025-2026',
        ]);
    }

    private function seedPrepData(): 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('classSection')->insert([
            'id' => 1,
            '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',
        ]);

        DB::table('inventory_categories')->insert([
            'type' => 'classroom',
            'name' => 'Small Table',
        ]);

        DB::table('inventory_items')->insert([
            'type' => 'classroom',
            'category_id' => 1,
            'name' => 'Small Table',
            'quantity' => 10,
            'good_qty' => 10,
            'school_year' => '2025-2026',
            'semester' => 'Fall',
        ]);
    }

    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);
    }
}
