<?php
declare(strict_types=1);

if (!defined('ABSPATH')) {
    exit;
}

/**
 * @var array<string, mixed> $lolacore_addon_card
 * @var string               $lolacore_addon_card_context 'expansion'|'catalog'
 */

$lolacore_card          = $lolacore_addon_card;
$lolacore_context       = $lolacore_addon_card_context ?? 'expansion';
$lolacore_state         = (string) ($lolacore_card['state'] ?? '');
$lolacore_is_licensed   = $lolacore_state === \LolaCore\Marketing\AddonCatalog::STATE_LICENSED;
$lolacore_is_free       = !empty($lolacore_card['is_free']);
$lolacore_show_installed = $lolacore_context === 'catalog' && $lolacore_is_licensed;
$lolacore_wporg_slug    = (string) ($lolacore_card['wporg_slug'] ?? '');
$lolacore_not_installed = $lolacore_state === \LolaCore\Marketing\AddonCatalog::STATE_NONE;
$lolacore_is_active     = !$lolacore_not_installed;
$lolacore_can_install   = $lolacore_not_installed && $lolacore_is_free && $lolacore_wporg_slug !== '';
?>
<article class="lc-addon-card">
    <div class="lc-addon-card-header">
        <h4 class="lc-addon-card-title"><?php echo esc_html((string) $lolacore_card['name']); ?></h4>
        <div class="lc-addon-card-badges">
            <?php if ($lolacore_show_installed) : ?>
                <span class="lc-pill mint"><?php echo esc_html__('Installed', 'lolacore'); ?></span>
            <?php elseif ($lolacore_is_free) : ?>
                <span class="lc-pill mint"><?php echo esc_html__('FREE ADDON', 'lolacore'); ?></span>
            <?php else : ?>
                <span class="lc-pill amber"><?php echo esc_html__('PREMIUM ADDON', 'lolacore'); ?></span>
            <?php endif; ?>
            <?php if (!empty($lolacore_card['base_plugin_active']) && !$lolacore_is_licensed) : ?>
                <span class="lc-pill plum"><?php echo esc_html__('Works with your site', 'lolacore'); ?></span>
            <?php endif; ?>
        </div>
    </div>
    <p class="lc-addon-card-count"><?php
        printf(
            /* translators: %s: ability count */
            esc_html__('%s abilities', 'lolacore'),
            esc_html((string) $lolacore_card['ability_count'])
        );
    ?></p>
    <p class="lc-addon-card-tagline"><?php echo esc_html((string) $lolacore_card['tagline']); ?></p>
    <div class="lc-addon-card-actions">
        <?php if ($lolacore_show_installed && !empty($lolacore_card['management_url'])) : ?>
            <a class="lc-addon-card-link" href="<?php echo esc_url((string) $lolacore_card['management_url']); ?>">
                <?php echo $lolacore_is_free
                    ? esc_html__('Configure →', 'lolacore')
                    : esc_html__('Manage license →', 'lolacore'); ?>
            </a>
        <?php elseif ($lolacore_can_install && current_user_can('install_plugins')) : ?>
            <a class="lc-addon-card-link" href="<?php echo esc_url(self_admin_url('plugin-install.php?tab=plugin-information&plugin=' . $lolacore_wporg_slug)); ?>">
                <?php echo esc_html__('Install from WordPress.org →', 'lolacore'); ?>
            </a>
        <?php elseif ($lolacore_can_install) : ?>
            <a class="lc-addon-card-link" href="<?php echo esc_url('https://wordpress.org/plugins/' . $lolacore_wporg_slug . '/'); ?>" target="_blank" rel="noopener noreferrer">
                <?php echo esc_html__('View on WordPress.org →', 'lolacore'); ?>
            </a>
        <?php elseif ($lolacore_is_active && !empty($lolacore_card['learn_more_url'])) : ?>
            <a class="lc-addon-card-link" href="<?php echo esc_url((string) $lolacore_card['learn_more_url']); ?>" target="_blank" rel="noopener noreferrer">
                <?php echo esc_html__('Learn more →', 'lolacore'); ?>
            </a>
        <?php elseif (!empty($lolacore_card['learn_more_url'])) : ?>
            <a class="lc-addon-card-link" href="<?php echo esc_url((string) $lolacore_card['learn_more_url']); ?>" target="_blank" rel="noopener noreferrer">
                <?php echo $lolacore_is_free
                    ? esc_html__('Download free →', 'lolacore')
                    : esc_html__('Learn more →', 'lolacore'); ?>
            </a>
        <?php endif; ?>
    </div>
</article>
