<?php

namespace App\Modules\Core\Controllers;

use App\Http\Controllers\Controller;

use App\Modules\Inventory\Contracts\InventoryServiceInterface;
use App\Modules\Procurement\Contracts\ProcurementServiceInterface;
use Inertia\Inertia;
use Illuminate\Http\Request;

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

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

    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();

        return Inertia::render('Dashboard', [
            'stats' => [
                'totalItems' => $invStats['productsCount'],
                'lowStockCount' => $invStats['lowStockCount'],
                'negativeCount' => $invStats['negativeCount'] ?? 0,
                'expiringCount' => $invStats['expiringCount'] ?? 0,
            ],
            'abnormalItems' => $invStats['abnormalItems'] ?? [],
        ]);
    }
}

