<?php

namespace Tests\Feature\Api\V1\Inventory;

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

class InventoryCategoriesControllerTest extends TestCase
{
    use RefreshDatabase;

    public function test_index_returns_categories(): void
    {
        $user = $this->seedUser();
        $this->seedCategory('classroom');

        Sanctum::actingAs($user);
        $response = $this->getJson('/api/v1/inventory/categories?type=classroom');

        $response->assertOk();
        $response->assertJsonPath('status', true);
        $this->assertNotEmpty($response->json('data.categories'));
    }

    public function test_store_creates_category(): void
    {
        $user = $this->seedUser();

        Sanctum::actingAs($user);
        $response = $this->postJson('/api/v1/inventory/categories', [
            'type' => 'office',
            'name' => 'Supplies',
        ]);

        $response->assertStatus(201);
        $this->assertDatabaseHas('inventory_categories', ['name' => 'Supplies']);
    }

    public function test_update_changes_category(): void
    {
        $user = $this->seedUser();
        $categoryId = $this->seedCategory('office');

        Sanctum::actingAs($user);
        $response = $this->patchJson('/api/v1/inventory/categories/' . $categoryId, [
            'name' => 'Updated',
        ]);

        $response->assertOk();
        $this->assertDatabaseHas('inventory_categories', ['id' => $categoryId, 'name' => 'Updated']);
    }

    public function test_destroy_deletes_category(): void
    {
        $user = $this->seedUser();
        $categoryId = $this->seedCategory('office');

        Sanctum::actingAs($user);
        $response = $this->deleteJson('/api/v1/inventory/categories/' . $categoryId);

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

    public function test_validation_fails(): void
    {
        $user = $this->seedUser();

        Sanctum::actingAs($user);
        $response = $this->postJson('/api/v1/inventory/categories', []);

        $response->assertStatus(422);
    }

    private function seedUser(): User
    {
        $userId = DB::table('users')->insertGetId([
            'firstname' => 'User',
            'lastname' => 'One',
            'cellphone' => '9999999999',
            'email' => 'user@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 seedCategory(string $type): int
    {
        return DB::table('inventory_categories')->insertGetId([
            'type' => $type,
            'name' => ucfirst($type) . ' Category',
            'created_at' => now(),
            'updated_at' => now(),
        ]);
    }
}
