<?php

namespace Tests\Unit\Services\Inventory;

use App\Services\Inventory\InventoryContextService;
use App\Services\Inventory\InventoryMovementService;
use App\Services\Inventory\InventoryTeacherDistributionService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class InventoryTeacherDistributionServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_form_data_requires_teacher_role(): void
    {
        $this->seedConfig();
        $userId = $this->seedUser();

        $service = new InventoryTeacherDistributionService(
            new InventoryContextService(),
            new InventoryMovementService(new InventoryContextService())
        );

        $result = $service->formData($userId, null, null);

        $this->assertFalse($result['ok']);
    }

    public function test_distribute_success(): void
    {
        $this->seedConfig();
        $userId = $this->seedUser();
        $this->seedTeacherRole($userId);
        $classSectionId = $this->seedClassSection();
        $this->seedTeacherClass($userId, $classSectionId);
        $studentId = $this->seedStudent(100, $userId);
        $this->seedStudentClass($studentId, $classSectionId);
        $categoryId = $this->seedCategory();
        $bookId = $this->seedItem($categoryId);

        $service = new InventoryTeacherDistributionService(
            new InventoryContextService(),
            new InventoryMovementService(new InventoryContextService())
        );

        $result = $service->distribute($userId, [
            'item_id' => $bookId,
            'class_section_id' => $classSectionId,
            'student_ids' => [$studentId],
        ]);

        $this->assertTrue($result['ok']);
        $this->assertDatabaseHas('inventory_movements', ['item_id' => $bookId, 'movement_type' => 'distribution']);
    }

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

    private function seedUser(): int
    {
        return DB::table('users')->insertGetId([
            'firstname' => 'Teacher',
            'lastname' => 'User',
            'cellphone' => '9999999999',
            'email' => 'teacher@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',
        ]);
    }

    private function seedTeacherRole(int $userId): void
    {
        $roleId = DB::table('roles')->insertGetId([
            'name' => 'teacher',
            'dashboard_route' => 'teacher',
            'priority' => 1,
            'is_active' => 1,
        ]);

        DB::table('user_roles')->insert([
            'user_id' => $userId,
            'role_id' => $roleId,
            'created_at' => now(),
            'updated_at' => now(),
        ]);
    }

    private function seedClassSection(): int
    {
        DB::table('classes')->insert([
            'id' => 1,
            'class_name' => 'Grade 1',
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        DB::table('classSection')->insert([
            'class_section_id' => 200,
            'class_section_name' => 'Grade 1',
            'class_id' => 1,
            'semester' => 'Fall',
            'school_year' => '2025-2026',
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        return 200;
    }

    private function seedTeacherClass(int $userId, int $classSectionId): void
    {
        DB::table('teacher_class')->insert([
            'teacher_id' => $userId,
            'class_section_id' => $classSectionId,
            'position' => 'main',
            'school_year' => '2025-2026',
            'created_at' => now(),
            'updated_at' => now(),
        ]);
    }

    private function seedStudent(int $id, int $parentId): int
    {
        DB::table('students')->insert([
            'id' => $id,
            'school_id' => 'SCH1',
            'firstname' => 'Student',
            'lastname' => 'One',
            'age' => 10,
            'gender' => 'Male',
            'photo_consent' => 1,
            'parent_id' => $parentId,
            'year_of_registration' => '2025-2026',
            'semester' => 'Fall',
            'school_year' => '2025-2026',
            'is_active' => 1,
        ]);

        return $id;
    }

    private function seedStudentClass(int $studentId, int $classSectionId): void
    {
        DB::table('student_class')->insert([
            'student_id' => $studentId,
            'class_section_id' => $classSectionId,
            'school_year' => '2025-2026',
            'semester' => 'Fall',
            'is_event_only' => 0,
            'created_at' => now(),
            'updated_at' => now(),
        ]);
    }

    private function seedCategory(): int
    {
        return DB::table('inventory_categories')->insertGetId([
            'type' => 'book',
            'name' => 'Books',
            'created_at' => now(),
            'updated_at' => now(),
        ]);
    }

    private function seedItem(int $categoryId): int
    {
        return DB::table('inventory_items')->insertGetId([
            'type' => 'book',
            'category_id' => $categoryId,
            'name' => 'Book',
            'quantity' => 2,
            'semester' => 'Fall',
            'school_year' => '2025-2026',
            'created_at' => now(),
            'updated_at' => now(),
        ]);
    }
}
