<?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 InventoryMovementsControllerTest extends TestCase
{
    use RefreshDatabase;

    public function test_index_returns_movements(): void
    {
        $user = $this->seedUser();
        $itemId = $this->seedItem();
        $this->seedMovement($itemId);

        Sanctum::actingAs($user);
        $response = $this->getJson('/api/v1/inventory/movements');

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

    public function test_store_creates_movement(): void
    {
        $user = $this->seedUser();
        $itemId = $this->seedItem();

        Sanctum::actingAs($user);
        $response = $this->postJson('/api/v1/inventory/movements', [
            'item_id' => $itemId,
            'qty_change' => 2,
            'movement_type' => 'in',
        ]);

        $response->assertStatus(201);
        $this->assertDatabaseHas('inventory_movements', ['item_id' => $itemId, 'movement_type' => 'in']);
    }

    public function test_update_changes_movement(): void
    {
        $user = $this->seedUser();
        $itemId = $this->seedItem();
        $movementId = $this->seedMovement($itemId);

        Sanctum::actingAs($user);
        $response = $this->patchJson('/api/v1/inventory/movements/' . $movementId, [
            'qty_change' => 3,
            'movement_type' => 'in',
        ]);

        $response->assertOk();
        $this->assertDatabaseHas('inventory_movements', ['id' => $movementId, 'qty_change' => 3]);
    }

    public function test_destroy_deletes_movement(): void
    {
        $user = $this->seedUser();
        $itemId = $this->seedItem();
        $movementId = $this->seedMovement($itemId);

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

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

    public function test_bulk_delete_removes_movements(): void
    {
        $user = $this->seedUser();
        $itemId = $this->seedItem();
        $first = $this->seedMovement($itemId);
        $second = $this->seedMovement($itemId);

        Sanctum::actingAs($user);
        $response = $this->postJson('/api/v1/inventory/movements/bulk-delete', [
            'ids' => [$first, $second],
        ]);

        $response->assertOk();
        $this->assertDatabaseMissing('inventory_movements', ['id' => $first]);
        $this->assertDatabaseMissing('inventory_movements', ['id' => $second]);
    }

    private function seedUser(): User
    {
        $userId = DB::table('users')->insertGetId([
            'firstname' => 'User',
            'lastname' => 'One',
            'cellphone' => '9999999999',
            'email' => 'movement@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 seedItem(): int
    {
        $categoryId = DB::table('inventory_categories')->insertGetId([
            'type' => 'office',
            'name' => 'Office',
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        return DB::table('inventory_items')->insertGetId([
            'type' => 'office',
            'category_id' => $categoryId,
            'name' => 'Printer',
            'quantity' => 10,
            'semester' => 'Fall',
            'school_year' => '2025-2026',
            'created_at' => now(),
            'updated_at' => now(),
        ]);
    }

    private function seedMovement(int $itemId): int
    {
        return DB::table('inventory_movements')->insertGetId([
            'item_id' => $itemId,
            'qty_change' => 1,
            'movement_type' => 'in',
            'semester' => 'Fall',
            'school_year' => '2025-2026',
            'created_at' => now(),
            'updated_at' => now(),
        ]);
    }
}
