<?php

declare(strict_types=1);

namespace App\Shared\Twig;

use App\Service\PlanFeatureService;
use App\Service\TenantContext;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

/**
 * Provides plan-related Twig functions for displaying plan badges,
 * checking feature availability, and showing upgrade prompts.
 */
class PlanExtension extends AbstractExtension
{
    public function __construct(
        private readonly TenantContext $tenantContext,
        private readonly PlanFeatureService $planFeatureService,
    ) {}

    public function getFunctions(): array
    {
        return [
            new TwigFunction('current_plan', [$this, 'getCurrentPlan']),
            new TwigFunction('plan_display_name', [$this, 'getPlanDisplayName']),
            new TwigFunction('is_feature_available', [$this, 'isFeatureAvailable']),
            new TwigFunction('is_module_allowed', [$this, 'isModuleAllowed']),
            new TwigFunction('plan_limits', [$this, 'getPlanLimits']),
            new TwigFunction('plan_features', [$this, 'getPlanFeatures']),
            new TwigFunction('feature_labels', [$this, 'getFeatureLabels']),
            new TwigFunction('plan_badge_class', [$this, 'getPlanBadgeClass']),
        ];
    }

    public function getCurrentPlan(): string
    {
        if (!$this->tenantContext->isResolved()) {
            return 'starter';
        }

        return $this->tenantContext->getPlan();
    }

    public function getPlanDisplayName(?string $plan = null): string
    {
        $plan = $plan ?? $this->getCurrentPlan();

        return $this->planFeatureService->getPlanDisplayName($plan);
    }

    public function isFeatureAvailable(string $feature): bool
    {
        if (!$this->tenantContext->isResolved()) {
            return false;
        }

        return $this->planFeatureService->isFeatureAvailable(
            $feature,
            $this->tenantContext->getPlan()
        );
    }

    public function isModuleAllowed(string $moduleSlug): bool
    {
        if (!$this->tenantContext->isResolved()) {
            return true;
        }

        return $this->planFeatureService->isModuleAllowed(
            $moduleSlug,
            $this->tenantContext->getPlan()
        );
    }

    /**
     * @return array{maxUsers: int, maxNews: int, maxStorageMb: int}
     */
    public function getPlanLimits(?string $plan = null): array
    {
        $plan = $plan ?? $this->getCurrentPlan();

        return $this->planFeatureService->getDefaultLimits($plan);
    }

    /**
     * @return array<string, bool>
     */
    public function getPlanFeatures(?string $plan = null): array
    {
        $plan = $plan ?? $this->getCurrentPlan();

        return $this->planFeatureService->getPlanFeatures($plan);
    }

    /**
     * @return array<string, string>
     */
    public function getFeatureLabels(): array
    {
        return $this->planFeatureService->getFeatureLabels();
    }

    /**
     * Returns Tailwind CSS classes for plan badges.
     */
    public function getPlanBadgeClass(?string $plan = null): string
    {
        $plan = $plan ?? $this->getCurrentPlan();

        return match ($plan) {
            'starter' => 'bg-green-100 text-green-700 border-green-200',
            'professional' => 'bg-blue-100 text-blue-700 border-blue-200',
            'enterprise' => 'bg-purple-100 text-purple-700 border-purple-200',
            default => 'bg-gray-100 text-gray-700 border-gray-200',
        };
    }
}
