<?php
if (!defined('ABSPATH')) exit;

if (!function_exists('custausu_admin_gate_plan_label')) {
  function custausu_admin_gate_plan_label(string $plan, callable $T): string {
    if (function_exists('custausu_free_package_sanitize_plan')) {
      $plan = custausu_free_package_sanitize_plan($plan, true);
    } else {
      $plan = sanitize_key($plan);
      if ($plan === 'base') {
        $plan = 'advanced';
      }
    }

    return match ($plan) {
      'freemium' => $T('Free', 'Free'),
      'advanced' => $T('Advanced', 'Advanced'),
      'premium'  => $T('Premium', 'Premium'),
      default    => $plan !== '' ? ucfirst($plan) : $T('Unknown', 'Sconosciuto'),
    };
  }
}

if (!function_exists('custausu_admin_gate_badge_html')) {
  function custausu_admin_gate_badge_html(string $label, string $variant): string {
    $variant = sanitize_html_class($variant ?: 'muted');
    return '<span class="custausu-badge custausu-badge--' . esc_attr($variant) . '">' . esc_html($label) . '</span>';
  }
}

if (!function_exists('custausu_admin_gate_data')) {
  function custausu_admin_gate_data(string $feature, callable $T): array {
    $feature = sanitize_key($feature);
    $advanced = [
      'i18n_polylang',
      'recaptcha_enterprise',
      'recaptcha_cf7',
      'cf7_mathcheck',
      'site_profile',
      'settings_export_import',
      'assets_customization',
    ];
    $availability = in_array($feature, $advanced, true)
      ? 'advanced'
      : 'premium';

    $labels = [
      'i18n_polylang' => 'Polylang / i18n',
      'recaptcha_enterprise' => 'reCAPTCHA Enterprise',
      'recaptcha_cf7' => 'reCAPTCHA for Contact Form 7',
      'cf7_mathcheck' => 'CF7 Math Check',
      'site_profile' => 'Site profile',
      'settings_export_import' => 'Settings export/import',
      'assets_customization' => 'Assets customization',
      'advanced_debug_tools' => 'Advanced debug tools',
      'logs_export_tools' => 'Log export tools',
      'webauthn_passkeys' => 'WebAuthn / Passkeys',
    ];
    $label = (string)($labels[$feature] ?? $feature);

    return [
      'feature' => $feature,
      'available' => false,
      'preview' => true,
      'availability_plan' => $availability,
      'mode' => 'local_package_preview',
      'reason' => 'separate_package_required',
      'show_note' => true,
      'message' => sprintf(
        $T(
          '%1$s is available in the %2$s package. This preview lets you explore the settings in advance; the controls remain inactive in the package currently installed.',
          '%1$s è disponibile nel pacchetto %2$s. Questa anteprima ti permette di conoscere le impostazioni in anticipo; i controlli restano inattivi nel pacchetto attualmente installato.'
        ),
        $label,
        custausu_admin_gate_plan_label($availability, $T)
      ),
      'badges' => [
        custausu_admin_gate_badge_html(
          $T('Informational preview', 'Anteprima informativa'),
          'preview'
        ),
        custausu_admin_gate_badge_html(
          custausu_admin_gate_plan_label($availability, $T),
          $availability
        ),
      ],
    ];
  }
}

if (!function_exists('custausu_admin_render_plan_banner')) {
  function custausu_admin_render_plan_banner(callable $T): string {
    $classes = ['custausu-plan-banner', 'custausu-plan-banner--freemium'];
    $badges = [
      custausu_admin_gate_badge_html(
        custausu_admin_gate_plan_label('freemium', $T),
        'free'
      ),
    ];
    $message = $T(
      'Free package active. You can explore the Advanced and Premium options in the previews below.',
      'Pacchetto Free attivo. Nelle anteprime qui sotto puoi esplorare le opzioni disponibili con Advanced e Premium.'
    );

    $html  = '<div class="' . esc_attr(implode(' ', $classes)) . '">';
    $html .= '<div class="custausu-license-rosette custausu-license-rosette--freemium">'
      . esc_html(custausu_admin_gate_plan_label('freemium', $T))
      . '</div>';
    $html .= '<div class="custausu-plan-banner__badges">'
      . implode('', $badges)
      . '</div>';
    $html .= '<div class="custausu-plan-banner__message">'
      . esc_html($message)
      . '</div>';
    $html .= '</div>';
    return $html;
  }
}

if (!function_exists('custausu_admin_gate_wrap_start')) {
  function custausu_admin_gate_wrap_start(string $feature, callable $T): string {
    $gate = custausu_admin_gate_data($feature, $T);

    $classes = ['custausu-gated-section'];
    if (!empty($gate['preview'])) {
      $classes[] = 'custausu-gated-section--preview';
    }

    $html = '<div class="' . esc_attr(implode(' ', $classes)) . '" data-custausu-gate-feature="' . esc_attr($feature) . '">';

    if (!empty($gate['show_note'])) {
      $note_classes = ['custausu-gated-section__note'];

      if (!empty($gate['preview'])) {
        $note_classes[] = 'custausu-gated-section__note--preview';
      }

      $html .= '<div class="' . esc_attr(implode(' ', $note_classes)) . '">';
      $html .= '<div class="custausu-gated-section__badges">' . implode('', $gate['badges']) . '</div>';
      $html .= '<div class="custausu-gated-section__message">' . esc_html($gate['message']) . '</div>';
      $html .= '</div>';
    }

    $html .= '<div class="custausu-gated-section__content">';

    return $html;
  }
}

if (!function_exists('custausu_admin_gate_wrap_end')) {
  function custausu_admin_gate_wrap_end(): string {
    return '</div></div>';
  }
}

if (!function_exists('custausu_admin_gated_action_button')) {
  function custausu_admin_gated_action_button(
    string $feature,
    callable $T,
    string $url,
    string $label,
    string $class = 'button'
  ): string {
    $gate = custausu_admin_gate_data($feature, $T);

    if (empty($gate['preview'])) {
      return '<a class="' . esc_attr($class) . '" href="' . esc_url($url) . '">' . esc_html($label) . '</a>';
    }

    return '<span class="' . esc_attr($class . ' is-disabled') . '" aria-disabled="true">' . esc_html($label) . '</span>';
  }
}
