<?php

declare(strict_types=1);

namespace Tests\Feature;

use App\Models\User;
use App\Models\Outlet;
use App\Models\Ingredient;
use App\Models\StockTransaction;
use App\Models\Supplier;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class IngredientStockHistoryTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();
        $this->seed();
    }

    public function test_authorized_user_can_view_stock_history_in_ingredients_index(): void
    {
        $admin = User::where('email', 'admin@example.com')->first();
        $this->assertNotNull($admin);

        // Create some stock transactions
        $outlet = Outlet::first();
        $ingredient = Ingredient::create(['name' => 'Kopi Arabika', 'unit' => 'g']);
        
        StockTransaction::create([
            'outlet_id' => $outlet->id,
            'user_id' => $admin->id,
            'item_type' => 'Ingredient',
            'item_id' => $ingredient->id,
            'qty' => 500,
            'type' => 'add',
            'notes' => 'Test restocking unique note',
        ]);

        $response = $this->actingAs($admin)
            ->get(route('ingredients.index'));

        $response->assertStatus(200);
        $response->assertInertia(fn ($page) => $page
            ->has('history')
            ->has('history.data')
        );

        $this->assertDatabaseHas('stock_transactions', [
            'notes' => 'Test restocking unique note',
        ]);
    }

    public function test_adjusting_stock_creates_stock_transaction_and_appears_in_history(): void
    {
        $admin = User::where('email', 'admin@example.com')->first();
        $this->assertNotNull($admin);

        $outlet = Outlet::first();
        $ingredient = Ingredient::create(['name' => 'Kopi Robusta', 'unit' => 'g']);
        $supplier = Supplier::create(['name' => 'Kopi Supplier', 'phone' => '12345']);

        $response = $this->actingAs($admin)
            ->post(route('ingredients.adjust'), [
                'outlet_id' => $outlet->id,
                'ingredient_id' => $ingredient->id,
                'stock_qty' => 1000,
                'threshold_qty' => 200,
                'action_type' => 'add',
                'supplier_id' => $supplier->id,
                'notes' => 'Bulk refill robusta beans',
            ]);

        $response->assertRedirect();
        
        $this->assertDatabaseHas('stock_transactions', [
            'outlet_id' => $outlet->id,
            'user_id' => $admin->id,
            'supplier_id' => $supplier->id,
            'item_type' => 'Ingredient',
            'item_id' => $ingredient->id,
            'qty' => 1000,
            'type' => 'add',
            'notes' => 'Bulk refill robusta beans',
        ]);
    }

    public function test_authorized_user_can_delete_stock_transaction_and_revert_stock(): void
    {
        $admin = User::where('email', 'admin@example.com')->first();
        $this->assertNotNull($admin);

        $outlet = Outlet::first();
        $ingredient = Ingredient::create(['name' => 'Susu Segar', 'unit' => 'ml']);
        
        // 1. Set current stock level to 1500ml
        $outletIngredient = \App\Models\OutletIngredient::create([
            'outlet_id' => $outlet->id,
            'ingredient_id' => $ingredient->id,
            'stock_qty' => 1500,
            'threshold_qty' => 300,
        ]);

        // 2. Log a restock of 500ml
        $stockTransaction = StockTransaction::create([
            'outlet_id' => $outlet->id,
            'user_id' => $admin->id,
            'item_type' => 'Ingredient',
            'item_id' => $ingredient->id,
            'qty' => 500,
            'type' => 'add',
            'notes' => 'Refill milk test',
        ]);

        // 3. Delete the transaction
        $response = $this->actingAs($admin)
            ->delete(route('ingredients.transactions.destroy', $stockTransaction->id));

        $response->assertRedirect();

        // 4. Verify database missing log
        $this->assertDatabaseMissing('stock_transactions', ['id' => $stockTransaction->id]);

        // 5. Verify stock has been reverted (1500ml - 500ml = 1000ml)
        $outletIngredient->refresh();
        $this->assertEquals(1000.0, (float)$outletIngredient->stock_qty);
    }
}
