<?php

namespace Tests\Unit\Services\Inventory;

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

class InventoryMovementServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_create_movement(): void
    {
        $this->seedConfig();
        $itemId = $this->seedItem();
        $service = new InventoryMovementService(new InventoryContextService());

        $result = $service->create([
            'item_id' => $itemId,
            'qty_change' => 2,
            'movement_type' => 'in',
        ], 1);

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

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

    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(),
        ]);
    }
}
