<?php

/**
 * JCOGS Image Pro - CpLicenseNoticeRenderer
 * =========================================
 * Renders CP inline notices for license governance states.
 *
 * @category   ExpressionEngine Add-on
 * @package    JCOGS Image Pro
 * @author     JCOGS Design <contact@jcogs.net>
 * @copyright  Copyright (c) 2021 - 2026 JCOGS Design
 * @license    https://jcogs.net/add-ons/license/jcogs_img_pro
 * @version    2.0.6
 * @link       https://jcogs.net/
 * @since      2.0.0
 */

namespace JCOGSDesign\JCOGSImagePro\Service\License;

class CpLicenseNoticeRenderer implements CpLicenseNoticeRendererInterface
{
    private function loadLanguage(): void
    {
        ee()->lang->load('jcogs_img_pro_license', ee()->session->get_language(), false, true, PATH_THIRD . 'jcogs_img_pro/');
    }

    private function t(string $key, string $fallback): string
    {
        $value = lang($key);
        return is_string($value) && $value !== $key ? $value : $fallback;
    }

    public function renderInlineNotice(array $policyDecision): ?array
    {
        if (empty($policyDecision['show_inline_notice'])) {
            return null;
        }

        $this->loadLanguage();

        $severity = (string) ($policyDecision['inline_severity'] ?? 'info');
        $rawStatus = (string) ($policyDecision['raw_status'] ?? 'na');
        $daysRemaining = $policyDecision['days_remaining'] ?? null;

        $title = $this->t('jcogs_img_pro_license_notice_title', 'License Notice');
        $body = $this->t(
            'jcogs_img_pro_license_notice_body',
            'License state affects Control Panel management actions only.'
        );

        if ($rawStatus === 'invalid' && is_numeric($daysRemaining)) {
            if ((int) $daysRemaining > 0) {
                $title = $this->t('jcogs_img_pro_license_required_soon_title', 'License Required Soon');
                $bodyTemplate = $this->t(
                    'jcogs_img_pro_license_required_soon_body',
                    'A valid license is required for future Control Panel changes. Days remaining: %d.'
                );
                $body = sprintf($bodyTemplate, (int) $daysRemaining);
            } else {
                $title = $this->t('jcogs_img_pro_license_required_title', 'License Required');
                $body = $this->t(
                    'jcogs_img_pro_license_required_body',
                    'Control Panel changes are currently restricted until a valid license is configured.'
                );
            }
        } elseif ($rawStatus === 'expired') {
            $title = $this->t('jcogs_img_pro_license_expired_title', 'License Expired');
            $body = $this->t(
                'jcogs_img_pro_license_expired_body',
                'Your licence has expired. Control Panel changes are restricted until your licence is renewed. Front-end image output is unaffected.'
            );
        } elseif ($rawStatus === 'trial') {
            $title = $this->t('jcogs_img_pro_license_trial_title', 'License Trial');
            $body = $this->t(
                'jcogs_img_pro_license_trial_body',
                'You are currently in trial status. Configure a full license to keep uninterrupted CP management.'
            );
        }

        $storeUrl = $this->t('jcogs_img_pro_license_store_url', 'https://expressionengine.com/store/licenses');
        $storeCta = $this->t('jcogs_img_pro_license_store_cta', 'Manage licenses');

        $bodyHtml = '<p>' . htmlspecialchars($body, ENT_QUOTES, 'UTF-8') . '</p>'
            . '<p><a href="' . htmlspecialchars($storeUrl, ENT_QUOTES, 'UTF-8') . '" target="_blank" rel="noopener">'
            . htmlspecialchars($storeCta, ENT_QUOTES, 'UTF-8') . '</a></p>';

        return [
            'title'    => $title,
            'body'     => $bodyHtml,
            'severity' => $severity,
        ];
    }

    public function shouldSuppressGenericBanner(array $policyDecision): bool
    {
        return !empty($policyDecision['dedupe_with_ee_invalid_banner']);
    }
}
