<?php
declare(strict_types=1);

require_once __DIR__ . '/../includes/bootstrap.php';

header('Content-Type: application/json; charset=utf-8');

if (!isset($_SESSION['user_id'])) {
    http_response_code(401);
    echo json_encode(['success' => false, 'message' => 'Não autenticado']);
    exit;
}

try {
    $dbPath = __DIR__ . '/../ibo_panel.db';
    $db = new PDO('sqlite:' . $dbPath);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $input = json_decode(file_get_contents('php://input'), true);
    $action = $input['action'] ?? 'view';

    if ($action === 'view') {
        $stmt = $db->query("SELECT * FROM playlist");
        $playlistRecords = $stmt->fetchAll(PDO::FETCH_ASSOC);
        echo json_encode(['success' => true, 'data' => $playlistRecords]);
    } elseif ($action === 'get_dns_options') {
        $stmt = $db->query("SELECT id, title FROM dns");
        $dnsOptions = $stmt->fetchAll(PDO::FETCH_ASSOC);
        echo json_encode(['success' => true, 'data' => $dnsOptions]);
    } elseif ($action === 'add') {
        $dns_id = trim($input['dns_id']);
        $mac_address = trim($input['mac_address']);
        $username = trim($input['username']);
        $password = trim($input['password']);
        $pin = trim($input['pin']);

        if (empty($dns_id) || empty($mac_address) || empty($username) || empty($password) || empty($pin)) {
            echo json_encode(['success' => false, 'message' => 'All fields are required']);
            exit();
        }

        $stmt = $db->prepare("INSERT INTO playlist (dns_id, mac_address, username, password, pin) VALUES (:dns_id, :mac_address, :username, :password, :pin)");
        $stmt->execute([
            ':dns_id' => $dns_id,
            ':mac_address' => $mac_address,
            ':username' => $username,
            ':password' => $password,
            ':pin' => $pin,
        ]);

        $newId = $db->lastInsertId();
        echo json_encode(['success' => true, 'message' => 'Playlist entry added successfully', 'new_id' => $newId]);
    } elseif ($action === 'edit') {
        $id = $input['id'];
        $dns_id = trim($input['dns_id']);
        $mac_address = trim($input['mac_address']);
        $username = trim($input['username']);
        $password = trim($input['password']);
        $pin = trim($input['pin']);

        $stmt = $db->prepare("UPDATE playlist SET dns_id = :dns_id, mac_address = :mac_address, username = :username, password = :password, pin = :pin WHERE id = :id");
        $stmt->execute([
            ':dns_id' => $dns_id,
            ':mac_address' => $mac_address,
            ':username' => $username,
            ':password' => $password,
            ':pin' => $pin,
            ':id' => $id,
        ]);

        echo json_encode(['success' => true, 'message' => 'Playlist entry updated successfully']);
    } elseif ($action === 'delete') {
        $id = $input['id'];

        $stmt = $db->prepare("DELETE FROM playlist WHERE id = :id");
        $stmt->execute([':id' => $id]);

        echo json_encode(['success' => true, 'message' => 'Playlist entry deleted successfully']);
    } else {
        echo json_encode(['success' => false, 'message' => 'Invalid action']);
    }
} catch (PDOException $e) {
    echo json_encode(['success' => false, 'message' => 'Database error: ' . $e->getMessage()]);
}
?>
