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

$settings = $settings ?? [];
$whitelist = $whitelist ?? [];
$blacklist = $blacklist ?? [];
$forbiddenWords = $forbiddenWords ?? [];
$logs = $logs ?? [];
$copyrightSummary = $copyrightSummary ?? ['total' => 0, 'review' => 0, 'blocked' => 0, 'duplicates' => 0, 'latest' => []];
$csrfToken = (string) ($csrfToken ?? '');
$zpanelLogo = logo_asset('zpanel');
$testMode = !empty(($GLOBALS['zalorox_config'] ?? [])['FEATURE_TEST_MODE']) && (($GLOBALS['zalorox_config']['APP_ENV'] ?? '') !== 'production');

$enabled = static fn (string $key): bool => (int) ($settings[$key] ?? 0) === 1;
$statusLabel = static fn (string $status): string => match ($status) {
    'blocked' => 'Engellendi',
    'review' => 'Inceleme',
    default => 'Guvenli',
};
?>
<div class="panel zpanel security-bot" data-security-bot data-api-base="<?= e(url('/api')) ?>" data-csrf="<?= e($csrfToken) ?>">
    <?php require BASE_PATH . '/app/Views/zpanel/_sidebar.php'; ?>

    <section class="panel-body security-bot-page">
        <div class="security-head">
            <div>
                <p class="eyebrow">Zalorox Shield</p>
                <h1>Guvenlik Botu</h1>
                <p class="muted">Link, yorum, mesaj, profil ve icerik taramalari icin merkezi moderasyon altyapisi.</p>
            </div>
            <form class="security-scan-inline" data-security-scan>
                <input type="hidden" name="_csrf" value="<?= e($csrfToken) ?>">
                <input name="url" type="url" placeholder="https://drive.google.com/..." autocomplete="off">
                <button class="pill primary" type="submit">Tara</button>
            </form>
        </div>

        <section class="security-card security-card-wide">
            <div class="security-table-head">
                <h2>Muzik Telif Kontrolu</h2>
                <span class="status-pill status-review">Fingerprint provider TODO</span>
            </div>
            <div class="zpanel-stat-grid">
                <article><span>Toplam kontrol</span><strong><?= e((string) (int) ($copyrightSummary['total'] ?? 0)) ?></strong></article>
                <article><span>Inceleme</span><strong><?= e((string) (int) ($copyrightSummary['review'] ?? 0)) ?></strong></article>
                <article><span>Duplicate</span><strong><?= e((string) (int) ($copyrightSummary['duplicates'] ?? 0)) ?></strong></article>
                <article><span>Blocked</span><strong><?= e((string) (int) ($copyrightSummary['blocked'] ?? 0)) ?></strong></article>
            </div>
            <div class="security-list">
                <?php foreach (($copyrightSummary['latest'] ?? []) as $item): ?>
                    <div class="security-list-row">
                        <span><?= e((string) ($item['title'] ?? '-')) ?> / <?= e((string) ($item['status'] ?? 'pending')) ?></span>
                        <small><?= e((string) ($item['risk_score'] ?? 0)) ?> risk</small>
                    </div>
                <?php endforeach; ?>
                <?php if (empty($copyrightSummary['latest'])): ?><p class="muted">Henuz muzik telif kontrol kaydi yok.</p><?php endif; ?>
            </div>
        </section>

        <form class="security-grid security-settings" data-security-settings>
            <input type="hidden" name="_csrf" value="<?= e($csrfToken) ?>">
            <section class="security-card">
                <h2>Bot Durumu</h2>
                <label class="switch-row">
                    <span>Bot aktif</span>
                    <input type="checkbox" name="shield_enabled" value="1" <?= $enabled('shield_enabled') ? 'checked' : '' ?>>
                </label>
                <label class="switch-row">
                    <span>Link kontrol</span>
                    <input type="checkbox" name="shield_link_scan_enabled" value="1" <?= $enabled('shield_link_scan_enabled') ? 'checked' : '' ?>>
                </label>
                <label class="switch-row">
                    <span>Yorum kontrol</span>
                    <input type="checkbox" name="shield_comment_scan_enabled" value="1" <?= $enabled('shield_comment_scan_enabled') ? 'checked' : '' ?>>
                </label>
                <label class="switch-row">
                    <span>Mesaj kontrol</span>
                    <input type="checkbox" name="shield_message_scan_enabled" value="1" <?= $enabled('shield_message_scan_enabled') ? 'checked' : '' ?>>
                </label>
                <label class="switch-row">
                    <span>Icerik kontrol</span>
                    <input type="checkbox" name="shield_content_scan_enabled" value="1" <?= $enabled('shield_content_scan_enabled') ? 'checked' : '' ?>>
                </label>
            </section>

            <section class="security-card">
                <h2>Risk Esikleri</h2>
                <label>
                    Inceleme esigi
                    <input name="shield_review_threshold" type="number" min="0" max="100" value="<?= e((string) ($settings['shield_review_threshold'] ?? 31)) ?>">
                </label>
                <label>
                    Engelleme esigi
                    <input name="shield_block_threshold" type="number" min="0" max="100" value="<?= e((string) ($settings['shield_block_threshold'] ?? 61)) ?>">
                </label>
                <label class="switch-row">
                    <span>Supheli icerik otomatik pending</span>
                    <input type="checkbox" name="shield_auto_pending" value="1" <?= $enabled('shield_auto_pending') ? 'checked' : '' ?>>
                </label>
                <label class="switch-row">
                    <span>Tehlikeli icerik otomatik engelle</span>
                    <input type="checkbox" name="shield_auto_block" value="1" <?= $enabled('shield_auto_block') ? 'checked' : '' ?>>
                </label>
                <button class="pill primary" type="submit">Ayarlari Kaydet</button>
            </section>
        </form>

        <div class="security-grid">
            <section class="security-card">
                <h2>Domain Whitelist</h2>
                <form class="inline-create" data-security-create="/zpanel/domain-whitelist/create">
                    <input type="hidden" name="_csrf" value="<?= e($csrfToken) ?>">
                    <input name="domain" placeholder="drive.google.com" autocomplete="off">
                    <input name="description" placeholder="Aciklama" autocomplete="off">
                    <button class="pill" type="submit">Ekle</button>
                </form>
                <div class="security-list">
                    <?php foreach ($whitelist as $item): ?>
                        <div class="security-list-row">
                            <span><?= e((string) ($item['domain'] ?? '')) ?></span>
                            <button type="button" data-security-delete="/zpanel/domain-whitelist/delete" data-id="<?= e((string) ($item['id'] ?? 0)) ?>">Sil</button>
                        </div>
                    <?php endforeach; ?>
                    <?php if (!$whitelist): ?><p class="muted">Whitelist kaydi yok.</p><?php endif; ?>
                </div>
            </section>

            <section class="security-card">
                <h2>Domain Blacklist</h2>
                <form class="inline-create" data-security-create="/zpanel/domain-blacklist/create">
                    <input type="hidden" name="_csrf" value="<?= e($csrfToken) ?>">
                    <input name="domain" placeholder="phishing-test.local" autocomplete="off">
                    <input name="description" placeholder="Aciklama" autocomplete="off">
                    <button class="pill" type="submit">Ekle</button>
                </form>
                <div class="security-list">
                    <?php foreach ($blacklist as $item): ?>
                        <div class="security-list-row">
                            <span><?= e((string) ($item['domain'] ?? '')) ?></span>
                            <button type="button" data-security-delete="/zpanel/domain-blacklist/delete" data-id="<?= e((string) ($item['id'] ?? 0)) ?>">Sil</button>
                        </div>
                    <?php endforeach; ?>
                    <?php if (!$blacklist): ?><p class="muted">Blacklist kaydi yok.</p><?php endif; ?>
                </div>
            </section>

            <section class="security-card security-card-wide">
                <h2>Yasakli Kelimeler</h2>
                <form class="inline-create" data-security-create="/zpanel/forbidden-words/create">
                    <input type="hidden" name="_csrf" value="<?= e($csrfToken) ?>">
                    <input name="word" placeholder="spamlink" autocomplete="off">
                    <input name="severity" type="number" min="0" max="100" value="60">
                    <button class="pill" type="submit">Ekle</button>
                </form>
                <div class="security-word-grid">
                    <?php foreach ($forbiddenWords as $item): ?>
                        <div class="word-chip">
                            <span><?= e((string) ($item['word'] ?? '')) ?></span>
                            <small><?= e((string) ($item['severity'] ?? 0)) ?></small>
                            <button type="button" data-security-delete="/zpanel/forbidden-words/delete" data-id="<?= e((string) ($item['id'] ?? 0)) ?>">Sil</button>
                        </div>
                    <?php endforeach; ?>
                    <?php if (!$forbiddenWords): ?><p class="muted">Yasakli kelime kaydi yok.</p><?php endif; ?>
                </div>
            </section>
        </div>

        <section class="security-card security-card-wide">
            <div class="security-table-head">
                <h2>Bot Loglari</h2>
                <button class="pill" type="button" data-security-refresh>Yenile</button>
            </div>
            <div class="table-scroll">
                <table class="security-table">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Tip</th>
                            <th>Hedef</th>
                            <th>Risk</th>
                            <th>Durum</th>
                            <th>Aksiyon</th>
                            <th>Sebepler</th>
                            <th>Tarih</th>
                        </tr>
                    </thead>
                    <tbody data-security-logs>
                        <?php foreach ($logs as $log): ?>
                            <tr>
                                <td><?= e((string) ($log['id'] ?? '')) ?></td>
                                <td><?= e((string) ($log['scan_type'] ?? '')) ?></td>
                                <td><?= e((string) ($log['target_type'] ?? '-')) ?></td>
                                <td><?= e((string) ($log['risk_score'] ?? 0)) ?></td>
                                <td><span class="status-pill status-<?= e((string) ($log['status'] ?? 'safe')) ?>"><?= e($statusLabel((string) ($log['status'] ?? 'safe'))) ?></span></td>
                                <td><?= e((string) ($log['action_taken'] ?? 'allow')) ?></td>
                                <td><?= e(implode(', ', (array) ($log['reasons'] ?? []))) ?></td>
                                <td><?= e((string) ($log['created_at'] ?? '')) ?></td>
                            </tr>
                        <?php endforeach; ?>
                        <?php if (!$logs): ?>
                            <tr><td colspan="8">Henuz bot logu yok.</td></tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </section>
    </section>
</div>
