<?php
$ic = 'w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-primary-500 focus:border-primary-500 outline-none';
$activeTab = $_GET['tab'] ?? 'all';
$tabs = [
    'all'       => 'All Pages',
    'homepage'  => 'Homepage',
    'watch'     => 'Watch Page',
    'player'    => 'Player',
    'listing'   => 'Listings',
    'category'  => 'Categories',
    'performer' => 'Performers',
    'album'     => 'Gallery Page',
    'album_listing' => 'Gallery Listings',
    'reports'   => 'Reports',
    'settings'  => 'Settings',
];
if (!isset($tabs[$activeTab])) $activeTab = 'all';
$filteredZones = in_array($activeTab, ['settings', 'reports'], true) ? [] : array_filter($zones, fn($z) => $z['page'] === $activeTab);
?>


<div class="mb-6 border-b border-gray-200">
    <nav class="flex gap-1 -mb-px admin-tabs admin-tabs--wrap">
        <?php foreach ($tabs as $tabKey => $tabLabel): ?>
        <a href="/admin/ads?tab=<?= $tabKey ?>"
           class="px-4 py-2.5 text-sm font-medium border-b-2 transition-colors <?= $activeTab === $tabKey ? 'border-primary-600 text-primary-600' : 'border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300' ?>">
            <?= $tabLabel ?>
        </a>
        <?php endforeach; ?>
    </nav>
</div>

<?php if ($activeTab === 'settings'): ?>

<div class="space-y-6">
    
    <div class="bg-white rounded-lg shadow-sm border border-gray-200">
        <form method="POST" action="/admin/ads/settings">
            <?= Router::csrfField() ?>
            <div class="px-5 py-4 flex items-center justify-between">
                <label class="flex items-center gap-3 cursor-pointer">
                    <input type="checkbox" name="ads_enabled" value="1" <?= $globalEnabled ? 'checked' : '' ?>
                           class="w-4 h-4 rounded border-gray-300 text-primary-600 focus:ring-primary-500"
                           onchange="this.form.submit()">
                    <div>
                        <span class="text-sm font-semibold text-gray-900">Ads Enabled</span>
                        <p class="text-xs text-gray-400">Master switch — disables all ad zones globally</p>
                    </div>
                </label>
                <?php if ($globalEnabled): ?>
                <span class="inline-flex items-center gap-1.5 px-2.5 py-1 rounded-full text-xs font-medium bg-green-100 text-green-700">
                    <span class="w-1.5 h-1.5 rounded-full bg-green-500"></span> Active
                </span>
                <?php else: ?>
                <span class="inline-flex items-center gap-1.5 px-2.5 py-1 rounded-full text-xs font-medium bg-gray-100 text-gray-500">
                    <span class="w-1.5 h-1.5 rounded-full bg-gray-400"></span> Disabled
                </span>
                <?php endif; ?>
            </div>
            <div class="px-5 py-4 border-t border-gray-100 flex items-center justify-between">
                <label class="flex items-center gap-3 cursor-pointer">
                    <input type="checkbox" name="ads_popunder_enabled" value="1" <?= !empty($popunderEnabled) ? 'checked' : '' ?>
                           class="w-4 h-4 rounded border-gray-300 text-primary-600 focus:ring-primary-500"
                           onchange="this.form.submit()">
                    <div>
                        <span class="text-sm font-semibold text-gray-900">Enable popunders</span>
                        <p class="text-xs text-gray-400">Master switch for popunders. Configure each one (timing &amp; frequency) under the <strong>Popup / Popunder</strong> zone. Off by default.</p>
                    </div>
                </label>
                <?php if (!empty($popunderEnabled)): ?>
                <span class="inline-flex items-center gap-1.5 px-2.5 py-1 rounded-full text-xs font-medium bg-green-100 text-green-700">
                    <span class="w-1.5 h-1.5 rounded-full bg-green-500"></span> On
                </span>
                <?php else: ?>
                <span class="inline-flex items-center gap-1.5 px-2.5 py-1 rounded-full text-xs font-medium bg-gray-100 text-gray-500">
                    <span class="w-1.5 h-1.5 rounded-full bg-gray-400"></span> Off
                </span>
                <?php endif; ?>
            </div>
            <div class="px-5 py-4 border-t border-gray-100 flex items-center justify-between">
                <label class="flex items-center gap-3 cursor-pointer">
                    <input type="checkbox" name="ads_closeable" value="1" <?= !empty($adsCloseable) ? 'checked' : '' ?>
                           class="w-4 h-4 rounded border-gray-300 text-primary-600 focus:ring-primary-500"
                           onchange="this.form.submit()">
                    <div>
                        <span class="text-sm font-semibold text-gray-900">Visitors can close ads</span>
                        <p class="text-[11px] text-gray-400">Shows a small &times; on each ad so visitors can dismiss it (stays hidden for the rest of their visit).</p>
                    </div>
                </label>
                <?php if (!empty($adsCloseable)): ?>
                <span class="inline-flex items-center gap-1.5 px-2.5 py-1 rounded-full text-xs font-medium bg-green-100 text-green-700"><span class="w-1.5 h-1.5 rounded-full bg-green-500"></span> On</span>
                <?php else: ?>
                <span class="inline-flex items-center gap-1.5 px-2.5 py-1 rounded-full text-xs font-medium bg-gray-100 text-gray-500"><span class="w-1.5 h-1.5 rounded-full bg-gray-400"></span> Off</span>
                <?php endif; ?>
            </div>
            <div class="px-5 py-4 border-t border-gray-100 flex items-center justify-between">
                <div>
                    <span class="text-sm font-semibold text-gray-900">Ad label color</span>
                    <p class="text-[11px] text-gray-400">Color of the &ldquo;Sponsored&rdquo; / &ldquo;Ad&rdquo; labels &amp; badges on your ads. Default red.</p>
                </div>
                <span class="flex items-center gap-2">
                    <code class="text-xs text-gray-400"><?= htmlspecialchars($accentColor ?? '#dc2626') ?></code>
                    <input type="color" name="ads_accent_color" value="<?= htmlspecialchars($accentColor ?? '#dc2626') ?>"
                           class="w-10 h-9 rounded border border-gray-300 cursor-pointer bg-white p-0.5"
                           onchange="this.form.submit()" title="Pick the ad label color">
                </span>
            </div>
            <div class="px-5 py-4 border-t border-gray-100">
                <label class="block text-sm font-semibold text-gray-900 mb-1">ads.txt</label>
                <p class="text-[11px] text-gray-400 mb-2">Served at <code>/ads.txt</code>. Paste the authorized-seller lines your ad networks give you (e.g. <code>exoclick.com, 12345, DIRECT</code>) so they will bid on your inventory.</p>
                <textarea name="ads_txt" rows="4" placeholder="exoclick.com, 0000, DIRECT&#10;trafficjunky.com, 0000, DIRECT" class="<?= $ic ?> font-mono text-xs"><?= htmlspecialchars($adsTxt ?? '') ?></textarea>
                <div class="mt-2"><button type="submit" class="px-4 py-1.5 bg-primary-600 text-white text-xs font-medium rounded-lg hover:bg-primary-700">Save ads.txt</button></div>
            </div>
        </form>
    </div>
</div>

<?php elseif ($activeTab === 'reports'): ?>
<?php include __DIR__ . '/_ads_reports.php'; ?>
<?php else: ?>

<?php
$tabDescriptions = [
    'all' => 'These ad zones are displayed on every page of your site.',
    'homepage' => 'These ad zones are specific to your homepage.',
    'watch' => 'Ads on the video page outside the player (sidebar, before comments, between related videos).',
    'player' => 'Ads inside and around the video player: pre-roll / mid-roll / post-roll (VAST), overlay, the pause ad, and the banners directly above and below the player.',
    'listing' => 'These ad zones appear on listing pages: search results, tag pages, etc. (not the homepage).',
    'category' => 'These ad zones appear on category pages.',
    'performer' => 'These ad zones appear on performer/pornstar pages.',
    'album' => 'Ads on the single gallery (album) page: top, bottom, before comments, and between related galleries.',
    'album_listing' => 'These ad zones appear on the album listing pages (/albums and the album category, tag, pornstar and channel archives).',
];
?>
<p class="text-sm text-gray-500 mb-4"><?= $tabDescriptions[$activeTab] ?? '' ?></p>

<?php if (empty($filteredZones)): ?>
<div class="bg-white rounded-lg shadow-sm border border-gray-200 p-8 text-center">
    <p class="text-sm text-gray-500">No ad zones configured for this page.</p>
</div>
<?php else: ?>
<div class="space-y-3">
    <?php foreach ($filteredZones as $zone): ?>
    <div class="bg-white rounded-lg shadow-sm border border-gray-200 hover:border-gray-300 transition-colors">
        <div class="px-4 py-3 flex items-center gap-3">
            <label class="relative inline-flex items-center cursor-pointer shrink-0">
                <input type="checkbox" class="sr-only peer" <?= $zone['is_active'] ? 'checked' : '' ?>
                       onchange="fetch('/admin/ads/zone/toggle',{method:'POST',headers:{'Content-Type':'application/json','X-CSRF-TOKEN':(document.querySelector('meta[name=csrf-token]')||{content:'<?= Router::csrfToken() ?>'}).content},body:JSON.stringify({id:<?= (int)$zone['id'] ?>,active:this.checked}),credentials:'same-origin'})">
                <div class="w-9 h-5 bg-gray-200 peer-focus:ring-2 peer-focus:ring-primary-300 rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-4 after:w-4 after:transition-all peer-checked:bg-primary-600"></div>
            </label>
            <a href="/admin/ads/zone/<?= (int)$zone['id'] ?>" class="flex-1 min-w-0 no-underline group">
                <div class="flex items-center gap-2 flex-wrap">
                    <span class="text-sm font-semibold text-gray-900 group-hover:text-primary-600 transition-colors"><?= htmlspecialchars($zone['name']) ?></span>
                    <?php $zi = class_exists('Ads') ? Ads::zoneInfo($zone['slug']) : null; if ($zi): ?>
                    <span class="inline-flex items-center px-1.5 py-0.5 rounded text-[10px] font-medium bg-gray-100 text-gray-500" title="Recommended size for image ads in this spot"><?= (!empty($zi['image']) && $zi['size'] !== '—') ? htmlspecialchars($zi['size']) . ' px' : htmlspecialchars($zi['label']) ?></span>
                    <?php endif; ?>
                </div>
                <?php if ($zone['description']): ?>
                <p class="text-xs text-gray-400 mt-0.5"><?= htmlspecialchars($zone['description']) ?></p>
                <?php endif; ?>
            </a>
            <div class="flex items-center gap-4 shrink-0">
                <div class="text-center">
                    <span class="block text-sm font-bold text-gray-900"><?= (int)$zone['active_spot_count'] ?></span>
                    <span class="text-[10px] text-gray-400">active</span>
                </div>
                <div class="text-center">
                    <span class="block text-sm font-bold text-gray-300"><?= (int)$zone['spot_count'] ?></span>
                    <span class="text-[10px] text-gray-400">total</span>
                </div>
            </div>
            <a href="/admin/ads/zone/<?= (int)$zone['id'] ?>" class="p-1.5 text-gray-400 hover:text-primary-600 transition-colors rounded-lg hover:bg-gray-50 no-underline shrink-0" title="Manage ads">
                <svg class="w-4 h-4" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M9 18l6-6-6-6"/></svg>
            </a>
        </div>
    </div>
    <?php endforeach; ?>
</div>
<?php endif; ?>
<?php endif; ?>
