<?php

namespace App\Modules\Operations\Services;

use App\Modules\Inventory\Stock\Services\InventoryStockRebuildService;
use App\Modules\Operations\Repositories\OperationalControlCenterQueryRepository;
use App\Modules\Operations\Repositories\OperationalControlEventRepository;
use App\Modules\Procurement\Services\GoodsReceiptIntegrityAuditService;

final class OperationalControlCenterDashboardService
{
    public function __construct(
        private readonly OperationalControlCenterQueryRepository $queries,
        private readonly OperationalControlEventRepository $events,
        private readonly GoodsReceiptIntegrityAuditService $integrityAudit,
        private readonly InventoryStockRebuildService $stockRebuild,
    ) {}

    /**
     * @return array<string, mixed>
     */
    public function assemble(int $tenantId): array
    {
        $days = (int) config('operations-control.dashboard.event_lookback_days', 14);
        $limit = (int) config('operations-control.dashboard.recent_event_limit', 100);
        $draftH = (int) config('operations-control.stale.draft_hours', 48);
        $valH = (int) config('operations-control.stale.validated_hours', 24);

        return [
            'failedReceipts' => $this->queries->failedReceipts($tenantId),
            'quarantinedReceipts' => $this->queries->receiptsWithQuarantinedLines($tenantId),
            'staleDraftReceipts' => $this->queries->staleDraftReceipts($tenantId, $draftH),
            'staleValidatedReceipts' => $this->queries->staleValidatedReceipts($tenantId, $valH),
            'recentSignals' => $this->events->recentUnacknowledged($tenantId, $days, $limit),
            'integrityAudit' => $this->integrityAudit->auditTenant($tenantId),
            'projectionDrift' => $this->stockRebuild->detectMismatch($tenantId),
            'thresholds' => [
                'draft_hours' => $draftH,
                'validated_hours' => $valH,
            ],
        ];
    }
}
