<?php

namespace Modules\WMSInventoryCore\App\Http\Controllers;

use App\Helpers\FormattingHelper;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Modules\SystemCore\App\Models\CoreProduct;
use Modules\WMSInventoryCore\App\Models\Inventory;
use Modules\WMSInventoryCore\App\Models\Warehouse;

class ReportController extends Controller
{
    /**
     * Display inventory valuation report
     */
    public function inventoryValuation(Request $request)
    {
        $this->authorize('wmsinventory.view-inventory-valuation');
        $warehouses = Warehouse::all();
        $selectedWarehouse = $request->input('warehouse_id');

        $query = Inventory::with(['product', 'warehouse']);

        if ($selectedWarehouse) {
            $query->where('warehouse_id', $selectedWarehouse);
        }

        $inventories = $query->get();

        $totalValue = 0;
        foreach ($inventories as $inventory) {
            if ($inventory->product) {
                $totalValue += $inventory->stock_level * $inventory->product->base_price;
            }
        }

        return view('wmsinventorycore::reports.inventory-valuation', [
            'warehouses' => $warehouses,
            'selectedWarehouse' => $selectedWarehouse,
            'warehouseProducts' => $inventories,
            'totalValue' => FormattingHelper::formatCurrency($totalValue),
        ]);
    }

    /**
     * Display stock movement report
     */
    public function stockMovement(Request $request)
    {
        $this->authorize('wmsinventory.view-stock-movement');
        $products = CoreProduct::all();
        $warehouses = Warehouse::all();
        $selectedProduct = $request->input('core_product_id');
        $selectedWarehouse = $request->input('warehouse_id');
        $startDate = $request->input('start_date');
        $endDate = $request->input('end_date');

        $movements = collect();

        if ($selectedProduct) {
            // Get all inventory transactions for the selected product
            $query = DB::table('inventory_transactions')
                ->join('core_products', 'inventory_transactions.core_product_id', '=', 'core_products.id')
                ->join('warehouses', 'inventory_transactions.warehouse_id', '=', 'warehouses.id')
                ->select(
                    'inventory_transactions.*',
                    'core_products.name as product_name',
                    'core_products.sku',
                    'warehouses.name as warehouse_name'
                )
                ->where('inventory_transactions.core_product_id', $selectedProduct);

            if ($selectedWarehouse) {
                $query->where('inventory_transactions.warehouse_id', $selectedWarehouse);
            }

            if ($startDate) {
                $query->whereDate('inventory_transactions.created_at', '>=', $startDate);
            }

            if ($endDate) {
                $query->whereDate('inventory_transactions.created_at', '<=', $endDate);
            }

            $movements = $query->orderBy('inventory_transactions.created_at', 'desc')->get();
        }

        return view('wmsinventorycore::reports.stock-movement', [
            'products' => $products,
            'warehouses' => $warehouses,
            'selectedProduct' => $selectedProduct,
            'selectedWarehouse' => $selectedWarehouse,
            'startDate' => $startDate,
            'endDate' => $endDate,
            'movements' => $movements,
        ]);
    }

    /**
     * Display low stock report
     */
    public function lowStock(Request $request)
    {
        $this->authorize('wmsinventory.view-low-stock');
        $warehouses = Warehouse::all();
        $selectedWarehouse = $request->input('warehouse_id');
        $threshold = $request->input('threshold', 10);

        $query = Inventory::with(['product', 'warehouse'])
            ->join('core_products', 'inventories.core_product_id', '=', 'core_products.id')
            ->whereRaw('inventories.stock_level <= core_products.reorder_level')
            ->where('core_products.reorder_level', '>', 0)
            ->select('inventories.*');

        if ($selectedWarehouse) {
            $query->where('warehouse_id', $selectedWarehouse);
        }

        $lowStockInventories = $query->get();

        return view('wmsinventorycore::reports.low-stock', [
            'warehouses' => $warehouses,
            'selectedWarehouse' => $selectedWarehouse,
            'threshold' => $threshold,
            'lowStockProducts' => $lowStockInventories,
        ]);
    }
}
