<?php

declare(strict_types=1);

namespace App\Service;

use App\Entity\Installation;

/**
 * Request-scoped value object holding the installation state.
 * Replaces the old multi-tenant TenantContext.
 * Set by InstallationSubscriber at the start of each request.
 */
class TenantContext
{
    private ?Installation $installation = null;

    public function getInstallation(): ?Installation
    {
        return $this->installation;
    }

    public function setInstallation(?Installation $installation): void
    {
        $this->installation = $installation;
    }

    public function isResolved(): bool
    {
        return $this->installation !== null;
    }

    public function getPlan(): string
    {
        return $this->installation?->getPlan() ?? 'starter';
    }

    public function isDemo(): bool
    {
        return $this->installation?->isDemo() ?? true;
    }

    public function isDemoExpired(): bool
    {
        return $this->installation?->isDemoExpired() ?? false;
    }

    public function isLicenseValid(): bool
    {
        return $this->installation?->isLicenseValid() ?? false;
    }

    public function isExpired(): bool
    {
        return $this->installation?->isExpired() ?? false;
    }

    public function getDaysUntilExpiry(): ?int
    {
        return $this->installation?->getDaysUntilExpiry();
    }

    public function getLicenseKey(): ?string
    {
        return $this->installation?->getLicenseKey();
    }

    public function getCachedLimits(): array
    {
        return $this->installation?->getCachedLimits() ?? ['maxUsers' => 3, 'maxNews' => 500, 'maxStorageMb' => 1024];
    }

    public function getCachedModules(): array
    {
        return $this->installation?->getCachedModules() ?? ['menu', 'comment', 'seo'];
    }

    public function getCachedFeatures(): array
    {
        return $this->installation?->getCachedFeatures() ?? [];
    }

    // Backward compat - old code calling getTenant()/getLicense() won't crash
    public function getTenant(): ?object { return null; }
    public function getLicense(): ?object { return null; }
    public function setTenant(?object $t): void {}
    public function setLicense(?object $l): void {}
    public function isTrialExpired(): bool { return $this->isDemoExpired(); }
}
