<?php
// pages/streaming_config.php
requireRole('admin');

// Fetch Global Settings
$stmt = $pdo->prepare("SELECT setting_key, setting_value FROM settings WHERE setting_key IN ('youtube_api_key', 'youtube_update_frequency')");
$stmt->execute();
$settings = $stmt->fetchAll(PDO::FETCH_KEY_PAIR);
$api_key = $settings['youtube_api_key'] ?? '';
$frequency = $settings['youtube_update_frequency'] ?? '5';

// Fetch Streams
$streams = $pdo->query("SELECT * FROM youtube_streams ORDER BY created_at DESC")->fetchAll();

$msg = isset($_GET['msg']) ? htmlspecialchars($_GET['msg']) : '';
$error = isset($_GET['error']) ? htmlspecialchars($_GET['error']) : '';
?>

<h1>Streaming Configuration</h1>

<?php if ($msg): ?>
<div class="alert alert-success"><?php echo $msg; ?></div>
<?php endif; ?>
<?php if ($error): ?>
<div class="alert alert-danger"><?php echo $error; ?></div>
<?php endif; ?>

<div class="dashboard-grid">
    <!-- Global Settings -->
    <div class="card">
        <h3>Global Settings</h3>
        <form action="api/streaming_config_save.php" method="POST">
            <input type="hidden" name="action" value="save_global">
            <div class="form-group">
                <label>Google API Key</label>
                <input type="text" name="youtube_api_key" class="form-control" value="<?php echo htmlspecialchars($api_key); ?>" placeholder="Enter API Key">
            </div>
            <div class="form-group">
                <label>Update Frequency (mins)</label>
                <input type="number" name="youtube_update_frequency" class="form-control" value="<?php echo htmlspecialchars($frequency); ?>" min="1" max="60">
            </div>
            <button type="submit" class="btn btn-block">Save Globals</button>
        </form>
    </div>

    <!-- Add Stream -->
    <div class="card">
        <h3>Add New Stream</h3>
        <form action="api/streaming_config_save.php" method="POST">
            <input type="hidden" name="action" value="add_stream">
            <div class="form-group">
                <label>Stream Name</label>
                <input type="text" name="name" class="form-control" placeholder="e.g. Main Service" required>
            </div>
            <div class="form-group">
                <label>Video ID</label>
                <input type="text" name="video_id" class="form-control" placeholder="e.g. dQw4w9WgXcQ" required>
            </div>
            <button type="submit" class="btn btn-block" style="background-color: var(--success-color); color: #000;">Add Stream</button>
        </form>
    </div>
</div>

<!-- Stream List -->
<div class="card" style="margin-top: 30px;">
    <h3>Active Streams</h3>
    <table class="table" style="width: 100%; border-collapse: collapse; margin-top: 15px;">
        <thead>
            <tr style="border-bottom: 2px solid #333; text-align: left;">
                <th style="padding: 10px;">Name</th>
                <th style="padding: 10px;">Video ID</th>
                <th style="padding: 10px;">Status</th>
                <th style="padding: 10px; text-align: right;">Actions</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($streams as $s): ?>
            <tr style="border-bottom: 1px solid #333;">
                <td style="padding: 10px; font-weight: bold;"><?php echo htmlspecialchars($s['name']); ?></td>
                <td style="padding: 10px; color: #aaa;"><?php echo htmlspecialchars($s['video_id']); ?></td>
                <td style="padding: 10px;">
                    <?php if ($s['is_active']): ?>
                        <span class="badge" style="background: var(--success-color); color: #000;">Active</span>
                    <?php else: ?>
                        <span class="badge" style="background: #444; color: #888;">Paused</span>
                    <?php endif; ?>
                </td>
                <td style="padding: 10px; text-align: right;">
                    <form action="api/streaming_config_save.php" method="POST" style="display: inline;" onsubmit="return confirm('Toggle status?')">
                        <input type="hidden" name="action" value="toggle_stream">
                        <input type="hidden" name="id" value="<?php echo $s['id']; ?>">
                        <button type="submit" class="btn" style="padding: 5px 10px; background-color: #555; font-size: 0.8rem;">
                            <?php echo $s['is_active'] ? 'Pause' : 'Resume'; ?>
                        </button>
                    </form>
                    <form action="api/streaming_config_save.php" method="POST" style="display: inline;" onsubmit="return confirm('Delete this stream configuration?')">
                        <input type="hidden" name="action" value="delete_stream">
                        <input type="hidden" name="id" value="<?php echo $s['id']; ?>">
                        <button type="submit" class="btn" style="padding: 5px 10px; background-color: var(--danger-color); font-size: 0.8rem;">Delete</button>
                    </form>
                </td>
            </tr>
            <?php endforeach; ?>
            <?php if (empty($streams)): ?>
            <tr><td colspan="4" style="padding: 20px; text-align: center; color: #666;">No streams configured.</td></tr>
            <?php endif; ?>
        </tbody>
    </table>
</div>

<div class="card" style="margin-top: 30px;">
    <h3>Cron Job Info</h3>
    <code style="display: block; background: #111; padding: 15px; color: #0f0;">
        */<?php echo $frequency; ?> * * * * /usr/local/bin/php <?php echo realpath(__DIR__ . '/../modules/stream_worker.php'); ?>
    </code>
</div>
