<?php
// api/streaming_manual_delete.php
session_start();
require_once '../config/database.php';
require_once '../includes/functions.php';

requireRole('any'); // Allow any authorized user to delete (or restrict to admin if strict)

$type = isset($_GET['type']) ? clean_input($_GET['type']) : '';
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if ($id <= 0) {
    header("Location: ../index.php?page=streaming_input&error=Invalid ID");
    exit;
}

try {
    if ($type === 'youtube') {
        $stmt = $pdo->prepare("DELETE FROM youtube_manual_logs WHERE id = ?");
        $stmt->execute([$id]);
        logActivity($_SESSION['user_id'], 'DELETE', "Deleted YouTube log ID: $id");
        $msg = "YouTube log entry deleted successfully.";
    } elseif ($type === 'radio') {
        $stmt = $pdo->prepare("DELETE FROM radio_manual_logs WHERE id = ?");
        $stmt->execute([$id]);
        logActivity($_SESSION['user_id'], 'DELETE', "Deleted Radio log ID: $id");
        $msg = "Radio log entry deleted successfully.";
    } else {
        throw new Exception("Invalid log type.");
    }
    
    header("Location: ../index.php?page=streaming_input&msg=" . urlencode($msg));
    exit;
} catch (Exception $e) {
    error_log("Delete Error: " . $e->getMessage());
    header("Location: ../index.php?page=streaming_input&error=" . urlencode("Failed to delete entry: " . $e->getMessage()));
    exit;
}
