38428-vm/api/automation.php
2026-02-25 19:30:52 +00:00

37 lines
1.6 KiB
PHP

<?php
header('Content-Type: application/json');
require_once __DIR__ . '/../db/config.php';
try {
$db = db();
// 1. Check if there is an active "played" song within its duration (e.g. 5 minutes)
$stmt = $db->query("SELECT COUNT(*) FROM song_requests WHERE status = 'played' AND created_at > DATE_SUB(NOW(), INTERVAL 5 MINUTE)");
$hasActive = $stmt->fetchColumn();
if ($hasActive == 0) {
// 2. No active song? Let's take the oldest pending request and "execute" it
$stmt = $db->query("SELECT * FROM song_requests WHERE status = 'pending' ORDER BY is_priority DESC, created_at ASC LIMIT 1");
$next = $stmt->fetch();
if ($next) {
$stmt = $db->prepare("UPDATE song_requests SET status = 'played' WHERE id = ?");
$stmt->execute([$next['id']]);
// Log automation
$db->prepare("INSERT INTO automation_logs (message) VALUES (?)")->execute(["Auto-DJ ejecutó: {$next['artist']} - {$next['song']}"]);
// Announce in chat
$chatMsg = "🤖 [AUTO-DJ] Es el turno de: **{$next['artist']} - {$next['song']}** (Pedido por: **{$next['requester']}**) 🎧🔥";
$db->prepare("INSERT INTO messages (username, message, type) VALUES ('Sistema', ?, 'dj_power')")->execute([$chatMsg]);
echo json_encode(['success' => true, 'action' => 'executed', 'song' => $next['song']]);
exit;
}
}
echo json_encode(['success' => true, 'action' => 'none']);
} catch (Exception $e) {
echo json_encode(['success' => false, 'error' => $e->getMessage()]);
}