<?php
use App\Core\Auth;
use function App\Core\{e, url};

$package = $package ?? [];
$creator = $creator ?? [];
$viewerHasActive = (bool) ($viewerHasActive ?? false);
$viewerIsGuest = (bool) ($viewerIsGuest ?? !Auth::check());
$isOwnProfile = (bool) ($isOwnProfile ?? false);
$currency = (string) (($package['currency'] ?? '') ?: 'TRY');
$money = static fn (mixed $value): string => number_format((float) $value, 2, ',', '.') . ' ' . $currency;
$creatorId = (int) (($package['user_id'] ?? null) ?: ($package['creator_id'] ?? ($creator['id'] ?? 0)));
$isActive = ($package['status'] ?? 'active') === 'active';
$activeSubscribers = (int) ($package['active_subscribers'] ?? 0);
$benefits = [];
if (!empty($package['benefits'])) {
    $decoded = json_decode((string) $package['benefits'], true);
    $benefits = is_array($decoded) ? array_values(array_filter(array_map('strval', $decoded))) : [];
}
?>
<article class="package-card premium-package-card" data-package-card data-package-id="<?= (int) ($package['id'] ?? 0) ?>" data-creator-id="<?= $creatorId ?>">
    <div class="package-card-top">
        <span class="package-status <?= $isActive ? 'is-active' : 'is-inactive' ?>"><?= $isActive ? 'Aktif' : 'Pasif' ?></span>
        <?php if ($viewerHasActive): ?><span class="package-status is-owned">Abonesin</span><?php endif; ?>
    </div>

    <h3><?= e((string) ($package['name'] ?? 'Destek Paketi')) ?></h3>
    <p><?= e((string) (($package['description'] ?? '') ?: 'Premium mod ve premium muzik erisimini destekleyen creator paketi.')) ?></p>

    <div class="package-price-row">
        <span>1 ay <strong><?= e($money($package['price_monthly'] ?? 0)) ?></strong></span>
        <span>6 ay <strong><?= e($money($package['price_6_month'] ?? 0)) ?></strong></span>
        <span>12 ay <strong><?= e($money($package['price_12_month'] ?? 0)) ?></strong></span>
    </div>

    <div class="package-meta-row">
        <span>Premium <strong>Mod + muzik</strong></span>
        <span>Aktif abone <strong><?= number_format($activeSubscribers, 0, ',', '.') ?></strong></span>
    </div>

    <?php if ($benefits !== []): ?>
        <ul class="package-benefits">
            <?php foreach (array_slice($benefits, 0, 4) as $benefit): ?>
                <li><?= e($benefit) ?></li>
            <?php endforeach; ?>
        </ul>
    <?php else: ?>
        <p class="package-access-note">Premium mod ve premium muzik erisimi bu paket altyapisiyla kontrol edilir.</p>
    <?php endif; ?>

    <?php if (!$isActive): ?>
        <button class="pill" type="button" disabled>Pasif Paket</button>
    <?php elseif ($isOwnProfile): ?>
        <a class="pill primary" href="<?= url('/studyo/abonelikler') ?>">Studio'da Yonet</a>
    <?php elseif ($viewerHasActive): ?>
        <button class="pill premium-locked-pill" type="button" disabled>Abonesin</button>
    <?php elseif ($viewerIsGuest): ?>
        <div class="package-subscribe-row">
            <a class="pill primary" href="<?= url('/giris-yap') ?>" data-auth-required data-auth-title="Giris gerekli" data-auth-message="Abone olmak icin giris yapmalisin.">Abone Ol</a>
            <a class="pill" href="<?= url('/giris-yap') ?>" data-auth-required data-auth-title="Giris gerekli" data-auth-message="6 aylik abonelik icin giris yapmalisin.">6 Ay</a>
            <a class="pill" href="<?= url('/giris-yap') ?>" data-auth-required data-auth-title="Giris gerekli" data-auth-message="12 aylik abonelik icin giris yapmalisin.">12 Ay</a>
        </div>
    <?php else: ?>
        <div class="package-subscribe-row">
            <a class="pill primary" href="<?= url('/checkout/' . (int) ($package['id'] ?? 0)) ?>?period=monthly">Abone Ol</a>
            <a class="pill" href="<?= url('/checkout/' . (int) ($package['id'] ?? 0)) ?>?period=6_month">6 Ay</a>
            <a class="pill" href="<?= url('/checkout/' . (int) ($package['id'] ?? 0)) ?>?period=12_month">12 Ay</a>
        </div>
    <?php endif; ?>
</article>
