<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\InventoryMovement;
use App\Models\Product;
use Illuminate\Http\Request;
use Illuminate\View\View;

class InventoryController extends Controller
{
    public function index(Request $request): View
    {
        $query = InventoryMovement::query()
            ->with(['product', 'variant', 'admin', 'order'])
            ->latest();

        if ($request->filled('product_id')) {
            $query->where('product_id', $request->integer('product_id'));
        }

        if ($request->filled('type')) {
            $query->where('type', $request->string('type')->toString());
        }

        return view('admin.inventory.index', [
            'movements' => $query->paginate(20)->withQueryString(),
            'products' => Product::query()->orderBy('name')->get(['id', 'name']),
            'selectedProductId' => $request->integer('product_id') ?: null,
            'selectedType' => $request->string('type')->toString(),
            'types' => [
                InventoryMovement::TYPE_INITIAL => 'Initial Stock',
                InventoryMovement::TYPE_MANUAL_ADJUSTMENT => 'Manual Adjustment',
                InventoryMovement::TYPE_ORDER_DEDUCTION => 'Order Deduction',
            ],
        ]);
    }
}
