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

$money = static fn (mixed $amount, string $currency = 'TRY'): string => number_format((float) $amount, 2, ',', '.') . ' ' . $currency;
$currency = (string) ($balance['currency'] ?? $settings['currency'] ?? 'TRY');
$totalEarned = array_sum(array_map(static fn ($row) => (float) ($row['creator_net_amount'] ?? 0), $sales ?? []));
?>
<div class="panel creator-shell"><?php require BASE_PATH . '/app/Views/partials/studio-sidebar.php'; ?>
<section class="panel-body finance-page" data-studio-finance data-csrf="<?= e($csrfToken ?? '') ?>">
    <div class="finance-head">
        <div>
            <h1>Kazanclarim</h1>
            <p>Creator satis gelirleri, kesintiler, bakiye ve cekim gecmisi.</p>
        </div>
        <a class="pill primary" href="<?= url('/studyo/para-cek') ?>">Para Cek</a>
    </div>

    <?php if (!empty($loadError)): ?><div class="finance-alert"><?= e($loadError) ?></div><?php endif; ?>

    <div class="finance-stat-grid">
        <article><span>Toplam kazanc</span><strong><?= e($money($totalEarned, $currency)) ?></strong></article>
        <article><span>Bekleyen bakiye</span><strong><?= e($money($balance['pending_balance'] ?? 0, $currency)) ?></strong></article>
        <article><span>Cekilebilir bakiye</span><strong><?= e($money($balance['available_balance'] ?? 0, $currency)) ?></strong></article>
        <article><span>Cekilmis bakiye</span><strong><?= e($money($balance['withdrawn_balance'] ?? 0, $currency)) ?></strong></article>
        <article><span>Reddedilen/iade donen</span><strong><?= e($money($balance['rejected_balance'] ?? 0, $currency)) ?></strong></article>
    </div>

    <div class="finance-grid">
        <section class="finance-card">
            <div class="finance-card-head">
                <h2>Son Satislar</h2>
                <span><?= count($sales ?? []) ?> kayit</span>
            </div>
            <div class="table-wrap">
                <table class="finance-table">
                    <thead><tr><th>Siparis</th><th>Paket</th><th>Abone</th><th>Brut</th><th>Provider</th><th>Komisyon</th><th>Vergi</th><th>Net</th><th>Durum</th><th>Tarih</th></tr></thead>
                    <tbody>
                    <?php foreach (($sales ?? []) as $sale): ?>
                        <tr>
                            <td><?= e($sale['order_no'] ?? '-') ?></td>
                            <td><?= e($sale['package_name'] ?? '-') ?></td>
                            <td><?= e($sale['subscriber_name'] ?? $sale['subscriber_username'] ?? '-') ?></td>
                            <td><?= e($money($sale['gross_amount'] ?? 0, $sale['currency'] ?? $currency)) ?></td>
                            <td><?= e($money($sale['provider_fee_amount'] ?? 0, $sale['currency'] ?? $currency)) ?></td>
                            <td><?= e($money($sale['commission_amount'] ?? 0, $sale['currency'] ?? $currency)) ?></td>
                            <td><?= e($money($sale['tax_amount'] ?? 0, $sale['currency'] ?? $currency)) ?></td>
                            <td><strong><?= e($money($sale['creator_net_amount'] ?? 0, $sale['currency'] ?? $currency)) ?></strong></td>
                            <td><span class="status-pill status-<?= e($sale['status'] ?? 'pending') ?>"><?= e($sale['status'] ?? '-') ?></span></td>
                            <td><?= e($sale['created_at'] ?? '') ?></td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (empty($sales)): ?><tr><td colspan="10">Henuz satis kaydi yok.</td></tr><?php endif; ?>
                    </tbody>
                </table>
            </div>
        </section>

        <aside class="finance-card">
            <div class="finance-card-head">
                <h2>Kesinti Detaylari</h2>
            </div>
            <dl class="finance-dl">
                <div><dt>Komisyon orani</dt><dd><?= e(((float) ($settings['site_commission_rate'] ?? 0) * 100) . '%') ?></dd></div>
                <div><dt>Vergi orani</dt><dd><?= e(((float) ($settings['tax_rate'] ?? 0) * 100) . '%') ?></dd></div>
                <div><dt>Provider fee orani</dt><dd><?= e(((float) ($settings['provider_fee_rate'] ?? 0) * 100) . '%') ?></dd></div>
                <div><dt>Sabit provider fee</dt><dd><?= e($money($settings['provider_fee_fixed'] ?? 0, $currency)) ?></dd></div>
                <div><dt>Net hesaplama</dt><dd>Brut - provider fee - komisyon - vergi</dd></div>
                <div><dt>Payout modu</dt><dd><?= e($settings['payout_mode'] ?? 'manual') ?></dd></div>
            </dl>
        </aside>
    </div>

    <div class="finance-grid">
        <section class="finance-card">
            <div class="finance-card-head">
                <h2>Banka Bilgilerim</h2>
                <a class="pill" href="<?= url('/studyo/bilgilerim') ?>">Yonet</a>
            </div>
            <div class="bank-mini-list">
                <?php foreach (($bankAccounts ?? []) as $account): ?>
                    <article>
                        <strong><?= e($account['bank_name'] ?? '-') ?></strong>
                        <span><?= e($account['account_holder'] ?? '-') ?> - <?= e($account['iban_masked'] ?? '') ?></span>
                        <?php if (!empty($account['is_default'])): ?><em>Varsayilan</em><?php endif; ?>
                    </article>
                <?php endforeach; ?>
                <?php if (empty($bankAccounts)): ?><p class="muted">Para cekmek icin banka hesabi eklemelisin.</p><?php endif; ?>
            </div>
        </section>

        <section class="finance-card">
            <div class="finance-card-head">
                <h2>Ledger Hareketleri</h2>
            </div>
            <div class="table-wrap">
                <table class="finance-table compact">
                    <thead><tr><th>Tip</th><th>Tutar</th><th>Once</th><th>Sonra</th><th>Aciklama</th><th>Tarih</th></tr></thead>
                    <tbody>
                    <?php foreach (($ledger ?? []) as $entry): ?>
                        <tr>
                            <td><?= e($entry['type'] ?? '-') ?></td>
                            <td><?= e($money($entry['amount'] ?? 0, $entry['currency'] ?? $currency)) ?></td>
                            <td><?= e($money($entry['balance_before'] ?? 0, $entry['currency'] ?? $currency)) ?></td>
                            <td><?= e($money($entry['balance_after'] ?? 0, $entry['currency'] ?? $currency)) ?></td>
                            <td><?= e($entry['description'] ?? '') ?></td>
                            <td><?= e($entry['created_at'] ?? '') ?></td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (empty($ledger)): ?><tr><td colspan="6">Ledger hareketi yok.</td></tr><?php endif; ?>
                    </tbody>
                </table>
            </div>
        </section>
    </div>
</section></div>
