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

$section = 'abonelikler';
$isCreator = (bool) ($isCreator ?? false);
$packages = $packages ?? [];
$subscribers = $subscribers ?? [];
$mySubscriptions = $mySubscriptions ?? [];
$money = static fn (mixed $value, string $currency = 'TRY'): string => number_format((float) $value, 2, ',', '.') . ' ' . $currency;
$dateLabel = static function (?string $value): string {
    $time = $value ? strtotime($value) : false;
    return $time ? date('d.m.Y', $time) : 'Suresiz';
};
$activeSubscriberCount = array_sum(array_map(static fn (array $package): int => (int) ($package['active_subscribers'] ?? 0), $packages));
$monthlyRevenue = array_sum(array_map(static fn (array $package): float => (float) ($package['monthly_revenue_placeholder'] ?? 0), $packages));
?>
<div class="panel studio-subscription-shell" data-studio-subscriptions data-csrf="<?= e(Csrf::token()) ?>">
    <?php require BASE_PATH . '/app/Views/partials/studio-sidebar.php'; ?>

    <section class="panel-body studio-subscriptions">
        <div class="studio-section-head">
            <div>
                <p class="eyebrow">Studio</p>
                <h1>Abonelik / Destek</h1>
                <p>Paket, destek ve premium erisim altyapisi burada yonetilir.</p>
            </div>
        </div>

        <?php if ($isCreator): ?>
            <div class="stat-grid studio-subscription-stats">
                <div>Paketler <strong><?= count($packages) ?></strong></div>
                <div>Aktif abone <strong><?= (int) $activeSubscriberCount ?></strong></div>
                <div>Gelir placeholder <strong><?= e($money($monthlyRevenue)) ?></strong></div>
            </div>

            <section class="studio-grid-2">
                <div class="studio-card">
                    <div class="studio-card-head">
                        <div>
                            <h2 data-package-form-mode>Yeni Paket</h2>
                            <p>Premium mod ve muzik erisimi icin creator destek paketi olustur.</p>
                        </div>
                    </div>
                    <?php $package = null; require BASE_PATH . '/app/Views/studio/package-form.php'; ?>
                </div>

                <div class="studio-card">
                    <div class="studio-card-head">
                        <div>
                            <h2>Paketlerin</h2>
                            <p>Aktif/pasif durumu, fiyatlar ve abone sayilari.</p>
                        </div>
                    </div>
                    <div class="studio-package-list" data-package-list>
                        <?php if ($packages === []): ?>
                            <div class="empty-state small">
                                <strong>Paket yok</strong>
                                <p>Ilk destek paketini formdan olusturabilirsin.</p>
                            </div>
                        <?php endif; ?>
                        <?php foreach ($packages as $package): ?>
                            <article
                                class="studio-package-card"
                                data-package-id="<?= (int) $package['id'] ?>"
                                data-package-name="<?= e($package['name']) ?>"
                                data-package-description="<?= e($package['description']) ?>"
                                data-package-monthly="<?= e((string) $package['price_monthly']) ?>"
                                data-package-six="<?= e((string) $package['price_6_month']) ?>"
                                data-package-twelve="<?= e((string) $package['price_12_month']) ?>"
                                data-package-status="<?= e($package['status']) ?>"
                            >
                                <div>
                                    <span class="package-status <?= $package['status'] === 'active' ? 'is-active' : 'is-inactive' ?>"><?= $package['status'] === 'active' ? 'Aktif' : 'Pasif' ?></span>
                                    <h3><?= e($package['name']) ?></h3>
                                    <p><?= e($package['description'] ?: 'Aciklama eklenmedi.') ?></p>
                                </div>
                                <div class="package-price-row">
                                    <span>1 ay <strong><?= e($money($package['price_monthly'], $package['currency'])) ?></strong></span>
                                    <span>6 ay <strong><?= e($money($package['price_6_month'], $package['currency'])) ?></strong></span>
                                    <span>12 ay <strong><?= e($money($package['price_12_month'], $package['currency'])) ?></strong></span>
                                </div>
                                <div class="package-meta-row">
                                    <span><?= (int) $package['active_subscribers'] ?> aktif abone</span>
                                    <span><?= e($money($package['monthly_revenue_placeholder'], $package['currency'])) ?> placeholder gelir</span>
                                </div>
                                <div class="package-actions">
                                    <button class="pill" type="button" data-package-edit>Duzenle</button>
                                    <button class="pill" type="button" data-package-toggle><?= $package['status'] === 'active' ? 'Pasife Al' : 'Aktif Yap' ?></button>
                                    <button class="pill" type="button" data-package-delete>Sil</button>
                                </div>
                            </article>
                        <?php endforeach; ?>
                    </div>
                </div>
            </section>

            <section class="studio-card">
                <div class="studio-card-head">
                    <div>
                        <h2>Aboneler</h2>
                        <p>Aktif ve gecmis abonelik kayitlari. Gelir netlestirme odeme bolumunde tamamlanacak.</p>
                    </div>
                </div>
                <div class="table-wrap">
                    <table>
                        <thead>
                            <tr>
                                <th>Kullanici</th>
                                <th>Paket</th>
                                <th>Durum</th>
                                <th>Baslangic</th>
                                <th>Bitis</th>
                            </tr>
                        </thead>
                        <tbody>
                        <?php if ($subscribers === []): ?>
                            <tr><td colspan="5">Henuz abone kaydi yok.</td></tr>
                        <?php endif; ?>
                        <?php foreach ($subscribers as $subscription): ?>
                            <tr>
                                <td><?= e($subscription['subscriber_name'] ?? $subscription['subscriber_username'] ?? 'User') ?></td>
                                <td><?= e($subscription['package_name']) ?></td>
                                <td><span class="package-status <?= !empty($subscription['is_active']) ? 'is-active' : 'is-inactive' ?>"><?= e($subscription['status']) ?></span></td>
                                <td><?= e($dateLabel($subscription['starts_at'] ?? null)) ?></td>
                                <td><?= e($dateLabel($subscription['ends_at'] ?? null)) ?></td>
                            </tr>
                        <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </section>
        <?php else: ?>
            <section class="studio-card support-empty">
                <h2>Creator paket yonetimi kapali</h2>
                <p>Bu hesap standart kullanici. Creator hesabi oldugunda paket olusturma ve abone listesi burada acilir.</p>
            </section>
        <?php endif; ?>

        <section class="studio-card">
            <div class="studio-card-head">
                <div>
                    <h2>Abone Oldugun Creatorlar</h2>
                    <p>Aktif aboneliklerin ve bitis tarihleri.</p>
                </div>
            </div>
            <div class="table-wrap">
                <table>
                    <thead>
                        <tr>
                            <th>Creator</th>
                            <th>Paket</th>
                            <th>Durum</th>
                            <th>Baslangic</th>
                            <th>Bitis</th>
                        </tr>
                    </thead>
                    <tbody data-my-subscriptions-body>
                    <?php if ($mySubscriptions === []): ?>
                        <tr><td colspan="5">Aktif abonelik bulunmuyor.</td></tr>
                    <?php endif; ?>
                    <?php foreach ($mySubscriptions as $subscription): ?>
                        <tr>
                            <td><?= e($subscription['creator_name'] ?? $subscription['creator_username'] ?? 'Creator') ?></td>
                            <td><?= e($subscription['package_name']) ?></td>
                            <td><span class="package-status <?= !empty($subscription['is_active']) ? 'is-active' : 'is-inactive' ?>"><?= e($subscription['status']) ?></span></td>
                            <td><?= e($dateLabel($subscription['starts_at'] ?? null)) ?></td>
                            <td><?= e($dateLabel($subscription['ends_at'] ?? null)) ?></td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </section>
    </section>
</div>
