<?php

namespace App\Modules\Core\Controllers;

use App\Http\Controllers\Controller;

use App\Modules\Inventory\Contracts\InventoryServiceInterface;
use App\Modules\Procurement\Contracts\ProcurementServiceInterface;
use App\Modules\Sales\Contracts\SalesServiceInterface;
use App\Modules\Production\Contracts\ProductionServiceInterface;
use Inertia\Inertia;
use Illuminate\Http\Request;

class DashboardController extends Controller
{
    protected $inventoryService;
    protected $procurementService;
    protected $salesService;
    protected $productionService;

    public function __construct(
        InventoryServiceInterface $inventoryService,
        ProcurementServiceInterface $procurementService,
        SalesServiceInterface $salesService,
        ProductionServiceInterface $productionService
    ) {
        $this->inventoryService = $inventoryService;
        $this->procurementService = $procurementService;
        $this->salesService = $salesService;
        $this->productionService = $productionService;
    }

    public function index()
    {
        $centralDomains = config('tenancy.central_domains', []);

        $demoPort = config('tenancy.demo_tenant_port');
        if ((!$demoPort || request()->getPort() != $demoPort) && in_array(request()->getHost(), $centralDomains)) {
            return redirect()->route('landlord.dashboard');
        }

        $invStats = $this->inventoryService->getDashboardStats();
        $procStats = $this->procurementService->getDashboardStats();

        // 銷售統計 (本月營收)
        $thisMonthRevenue = $this->salesService->getThisMonthRevenue();

        // 生產統計 (待核准工單)
        $pendingProductionCount = $this->productionService->getPendingProductionCount();
        
        // 生產狀態分佈
        // 近30日銷售趨勢 (Area Chart)
        $salesTrend = $this->salesService->getSalesTrend();

        // 本月熱銷商品 Top 5 (Bar Chart)
        $topSellingItems = $this->salesService->getTopSellingProducts();
        $productIds = $topSellingItems->pluck('product_id')->filter()->unique()->toArray();
        $productsMap = $this->inventoryService->getProductsByIds($productIds)->keyBy('id');

        $topSellingProducts = $topSellingItems->map(function ($item) use ($productsMap) {
            $product = $productsMap->get($item->product_id);
            return [
                'name' => $product ? $product->name : $item->product_code,
                'amount' => (int)$item->total_amount,
            ];
        });

        // 庫存積壓排行 (Top Inventory Value)
        $topInventoryValueItems = $this->inventoryService->getTopInventoryValue();
        $invProductIds = $topInventoryValueItems->pluck('product_id')->filter()->unique()->toArray();
        $invProductsMap = $this->inventoryService->getProductsByIds($invProductIds)->keyBy('id');

        $topInventoryValue = $topInventoryValueItems->map(function ($item) use ($invProductsMap) {
            $product = $invProductsMap->get($item->product_id);
            return [
                'name' => $product ? $product->name : 'Unknown Product',
                'code' => $product ? $product->code : '',
                'value' => (int)$item->total_value,
            ];
        });

        // 熱銷數量排行 (Top Selling by Quantity)
        $topSellingQtyItems = $this->salesService->getTopSellingByQuantity();
        $qtyProductIds = $topSellingQtyItems->pluck('product_id')->filter()->unique()->toArray();
        $qtyProductsMap = $this->inventoryService->getProductsByIds($qtyProductIds)->keyBy('id');

        $topSellingByQuantity = $topSellingQtyItems->map(function ($item) use ($qtyProductsMap) {
            $product = $qtyProductsMap->get($item->product_id);
            return [
                'name' => $product ? $product->name : $item->product_code,
                'code' => $item->product_code,
                'value' => (int)$item->total_quantity,
            ];
        });

        // 即將過期商品 (Expiring Soon)
        $expiringItems = $this->inventoryService->getExpiringSoon();
        $expiringProductIds = $expiringItems->pluck('product_id')->filter()->unique()->toArray();
        $expiringProductsMap = $this->inventoryService->getProductsByIds($expiringProductIds)->keyBy('id');

        $expiringSoon = $expiringItems->map(function ($item) use ($expiringProductsMap) {
            $product = $expiringProductsMap->get($item->product_id);
            return [
                'name' => $product ? $product->name : 'Unknown Product',
                'batch_number' => $item->batch_number,
                'expiry_date' => $item->expiry_date->format('Y-m-d'),
                'quantity' => (int)$item->quantity,
            ];
        });

        return Inertia::render('Dashboard', [
            'stats' => [
                'totalItems' => $invStats['productsCount'],
                'lowStockCount' => $invStats['lowStockCount'],
                'negativeCount' => $invStats['negativeCount'] ?? 0,
                'expiringCount' => $invStats['expiringCount'] ?? 0,
                'totalInventoryValue' => $invStats['totalInventoryValue'] ?? 0,
                'thisMonthRevenue' => $thisMonthRevenue,
                'pendingOrdersCount' => $procStats['pendingOrdersCount'] ?? 0,
                'pendingTransferCount' => $invStats['pendingTransferCount'] ?? 0,
                'pendingProductionCount' => $pendingProductionCount,
                'todoCount' => ($procStats['pendingOrdersCount'] ?? 0) + ($invStats['pendingTransferCount'] ?? 0) + $pendingProductionCount,
                'salesTrend' => $salesTrend,
                'topSellingProducts' => $topSellingProducts,
                'topInventoryValue' => $topInventoryValue,
                'topSellingByQuantity' => $topSellingByQuantity,
                'expiringSoon' => $expiringSoon,
            ],
        ]);
    }
}

