<?php
declare(strict_types=1);
require __DIR__ . '/../lib/bootstrap.php';
handle_cors_preflight();
require_method(['POST']);
rate_limit('device-status', 120, 60);

$input = input_json();
$device = authenticate_device($input, true);
$playlists = get_device_playlists((int) $device['id']);
$playlist = get_device_playlist((int) $device['id']);

json_response([
    'ok' => true,
    'mac' => $device['mac'],
    'status' => $device['status'],
    'trial_started_at' => $device['trial_started_at'],
    'trial_ends_at' => $device['trial_ends_at'],
    'active_until' => $device['active_until'],
    'access' => device_access($device),
    'playlist' => public_playlist_payload($playlist),
    'playlists' => public_playlists_payload($playlists),
    'proxy_token' => make_signed_token('proxy', (int) $device['id'], (int) config('app.proxy_token_minutes', 360)),
    'portal_url' => portal_url($device),
    'server_time' => gmdate('c'),
]);
