<?php
require_once __DIR__ . '/../includes/bootstrap_api.php';
require_once __DIR__ . '/../includes/functions.php';
require_once __DIR__ . '/../includes/api_helpers.php';

$request = api_read_request();
$data = isset($request['data']) ? api_decode_app_data($request['data']) : null;
if (!is_array($data)) api_json_response(['success' => 0, 'status' => false, 'message' => 'Invalid data'], 400);

$mac = api_mac_from_payload($data);
$dnsId = (int)($data['playlist_id'] ?? 0);
if ($mac === '' || $dnsId <= 0) api_json_response(['success' => 0, 'status' => false, 'message' => 'Invalid data for deletion'], 400);

$rows = $db->select('playlist', 'id', 'dns_id = :dns AND mac_address = :mac', '', [':dns' => $dnsId, ':mac' => $mac]);
if (empty($rows)) api_json_response(['success' => 1, 'status' => true, 'message' => 'Playlist already absent']);

$deleted = $db->delete('playlist', 'dns_id = :dns AND mac_address = :mac', [':dns' => $dnsId, ':mac' => $mac]);
api_json_response([
    'success' => $deleted ? 1 : 0,
    'status' => (bool)$deleted,
    'message' => $deleted ? 'Playlist deleted successfully' : 'Error deleting playlist'
], $deleted ? 200 : 500);
