<?php
$enabled = ($settings['transcode_enabled'] ?? '0') === '1';
$ffmpegDetected = TranscodeManager::ffmpegPath();
$available = $ffmpegDetected !== '';
$hasServers = (bool)Database::fetchOne("SELECT id FROM transcode_servers WHERE is_active = 1 LIMIT 1");
$canProcess = $available || $hasServers;
$ffmpegVersion = '';
if ($available) {
    $vLine = shell_exec(escapeshellcmd($ffmpegDetected) . ' -version 2>/dev/null | head -1') ?? '';
    $ffmpegVersion = explode(' ', $vLine)[2] ?? '';
}
$stats = TranscodeManager::getStats();
$activeCount = $stats['processing'] + $stats['pending'];
?>

<?php if (!$available && !$hasServers): ?>
<div class="flex items-start gap-3 p-4 bg-amber-50 border border-amber-200 rounded-lg mb-6">
    <i class="fa-solid fa-triangle-exclamation text-amber-500 mt-0.5"></i>
    <div>
        <p class="text-sm font-medium text-amber-900">No transcoding capability</p>
        <p class="text-xs text-amber-700 mt-1">FFmpeg is not installed on this server and no remote servers are configured. Either install FFmpeg (<code class="bg-amber-100 px-1.5 py-0.5 rounded">sudo apt install ffmpeg</code>) or <a href="/admin/settings?tab=transcode&sub=servers" class="underline font-medium">add a remote transcoding server</a>.</p>
    </div>
</div>
<?php endif; ?>

<?php if ($canProcess): ?>
<div class="flex items-center justify-between gap-4 mb-6 bg-white rounded-lg border border-gray-200 px-4 py-3">
    <form method="POST" action="/admin/settings" class="flex items-center gap-3">
        <?= Router::csrfField() ?>
        <input type="hidden" name="_tab" value="transcode">
        <label class="relative inline-flex items-center cursor-pointer">
            <input type="checkbox" name="transcode_enabled" <?= $enabled ? 'checked' : '' ?> onchange="this.form.submit()" class="sr-only peer">
            <div class="w-9 h-5 bg-gray-300 rounded-full peer peer-checked:bg-primary-600 transition-colors after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:rounded-full after:h-4 after:w-4 after:transition-transform peer-checked:after:translate-x-4 after:shadow-sm"></div>
        </label>
        <span class="text-sm font-medium text-gray-900">Transcoding</span>
    </form>
    <div class="flex items-center gap-2">
        <?php if ($activeCount > 0): ?>
        <span class="inline-flex items-center gap-1.5 px-2 py-1 rounded-full text-[10px] font-medium bg-blue-100 text-blue-700">
            <span class="w-1.5 h-1.5 rounded-full bg-blue-500 animate-pulse"></span> <?= $activeCount ?> active
        </span>
        <?php endif; ?>
        <?php if ($available): ?>
        <span class="inline-flex items-center gap-1.5 px-2 py-1 rounded-full text-[10px] font-medium bg-green-50 text-green-700 border border-green-200">
            <i class="fa-solid fa-microchip" style="font-size:9px"></i> FFmpeg <?= htmlspecialchars($ffmpegVersion) ?>
        </span>
        <?php endif; ?>
        <?php if ($hasServers): ?>
        <span class="inline-flex items-center gap-1.5 px-2 py-1 rounded-full text-[10px] font-medium bg-purple-50 text-purple-700 border border-purple-200">
            <i class="fa-solid fa-server" style="font-size:9px"></i> Remote
        </span>
        <?php endif; ?>
        <?php if (!$available && $hasServers): ?>
        <span class="inline-flex items-center gap-1.5 px-2 py-1 rounded-full text-[10px] font-medium bg-gray-100 text-gray-500">
            <i class="fa-solid fa-desktop" style="font-size:9px"></i> No local FFmpeg
        </span>
        <?php endif; ?>
    </div>
</div>
<?php endif; ?>

<div class="mb-6 border-b border-gray-200">
    <nav class="flex gap-1 -mb-px admin-tabs">
        <?php foreach ($subTabs as $subKey => $subLabel): ?>
            <a href="/admin/settings?tab=transcode&sub=<?= $subKey ?>"
               class="px-4 py-2.5 text-sm font-medium border-b-2 transition-colors <?= $activeSub === $subKey ? 'border-primary-600 text-primary-600' : 'border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300' ?>">
                <?= $subLabel ?>
                <?php if ($subKey === 'queue' && $activeCount > 0): ?>
                <span class="ml-1 px-1.5 py-0.5 text-[9px] font-bold rounded-full bg-blue-500 text-white"><?= $activeCount ?></span>
                <?php endif; ?>
            </a>
        <?php endforeach; ?>
    </nav>
</div>

<?php
$subFile = __DIR__ . '/_settings_transcode_' . $activeSub . '.php';
if (file_exists($subFile)) {
    require $subFile;
}
?>
