<?php

declare(strict_types=1);

namespace App\Shared\Twig;

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

/**
 * Provides license/usage Twig functions for displaying
 * warning banners, usage stats, and limit info in admin panel.
 */
class LicenseExtension extends AbstractExtension
{
    public function __construct(
        private readonly TenantContext $tenantContext,
        private readonly LicenseEnforcementService $enforcementService,
    ) {}

    public function getFunctions(): array
    {
        return [
            new TwigFunction('license_warnings', [$this, 'getWarnings']),
            new TwigFunction('license_info', [$this, 'getLicenseInfo']),
            new TwigFunction('usage_summary', [$this, 'getUsageSummary']),
        ];
    }

    /**
     * @return string[] Warning messages to display as banners
     */
    public function getWarnings(): array
    {
        if (!$this->tenantContext->isResolved()) {
            return [];
        }

        try {
            $summary = $this->enforcementService->getUsageSummary();
            return $summary->getWarningMessages();
        } catch (\Exception) {
            return [];
        }
    }

    /**
     * @return array{plan: string, daysLeft: ?int, isGracePeriod: bool}
     */
    public function getLicenseInfo(): array
    {
        if (!$this->tenantContext->isResolved()) {
            return [
                'plan' => 'unknown',
                'daysLeft' => null,
                'isGracePeriod' => false,
            ];
        }

        return [
            'plan' => $this->tenantContext->getPlan(),
            'daysLeft' => $this->tenantContext->getDaysUntilExpiry(),
            'isGracePeriod' => $this->enforcementService->isInGracePeriod(),
        ];
    }

    /**
     * Full usage summary with limit check results.
     *
     * @return array{
     *     users: array{allowed: bool, current: int, max: int, percentUsed: float, message: string},
     *     news: array{allowed: bool, current: int, max: int, percentUsed: float, message: string},
     *     storage: array{allowed: bool, current: int, max: int, percentUsed: float, message: string},
     *     licenseStatus: string,
     *     daysUntilExpiry: ?int,
     *     plan: string
     * }
     */
    public function getUsageSummary(): array
    {
        if (!$this->tenantContext->isResolved()) {
            return [
                'users' => ['allowed' => true, 'current' => 0, 'max' => 0, 'percentUsed' => 0, 'message' => 'Sınırsız'],
                'news' => ['allowed' => true, 'current' => 0, 'max' => 0, 'percentUsed' => 0, 'message' => 'Sınırsız'],
                'storage' => ['allowed' => true, 'current' => 0, 'max' => 0, 'percentUsed' => 0, 'message' => 'Sınırsız'],
                'licenseStatus' => 'none',
                'daysUntilExpiry' => null,
                'plan' => 'starter',
            ];
        }

        try {
            $summary = $this->enforcementService->getUsageSummary();

            return [
                'users' => [
                    'allowed' => $summary->users->allowed,
                    'current' => $summary->users->current,
                    'max' => $summary->users->max,
                    'percentUsed' => $summary->users->percentUsed,
                    'message' => $summary->users->message,
                    'nearLimit' => $summary->users->isNearLimit(),
                    'atLimit' => $summary->users->isAtLimit(),
                ],
                'news' => [
                    'allowed' => $summary->news->allowed,
                    'current' => $summary->news->current,
                    'max' => $summary->news->max,
                    'percentUsed' => $summary->news->percentUsed,
                    'message' => $summary->news->message,
                    'nearLimit' => $summary->news->isNearLimit(),
                    'atLimit' => $summary->news->isAtLimit(),
                ],
                'storage' => [
                    'allowed' => $summary->storage->allowed,
                    'current' => $summary->storage->current,
                    'max' => $summary->storage->max,
                    'percentUsed' => $summary->storage->percentUsed,
                    'message' => $summary->storage->message,
                    'nearLimit' => $summary->storage->isNearLimit(),
                    'atLimit' => $summary->storage->isAtLimit(),
                ],
                'licenseStatus' => $summary->licenseStatus,
                'daysUntilExpiry' => $summary->daysUntilExpiry,
                'plan' => $summary->plan,
            ];
        } catch (\Exception) {
            return [
                'users' => ['allowed' => true, 'current' => 0, 'max' => 0, 'percentUsed' => 0, 'message' => '', 'nearLimit' => false, 'atLimit' => false],
                'news' => ['allowed' => true, 'current' => 0, 'max' => 0, 'percentUsed' => 0, 'message' => '', 'nearLimit' => false, 'atLimit' => false],
                'storage' => ['allowed' => true, 'current' => 0, 'max' => 0, 'percentUsed' => 0, 'message' => '', 'nearLimit' => false, 'atLimit' => false],
                'licenseStatus' => 'active',
                'daysUntilExpiry' => null,
                'plan' => $this->tenantContext->getPlan(),
            ];
        }
    }
}
