<?php
declare(strict_types=1);

require __DIR__ . '/../../includes/bootstrap.php';
require_admin_login();

if (SPOTIFY_CLIENT_ID === '' || SPOTIFY_REDIRECT_URI === '') {
  http_response_code(500);
  echo "Missing Spotify client_id or redirect_uri";
  exit;
}

$state = bin2hex(random_bytes(16));
$_SESSION['sp_state'] = $state;

$scope = 'user-read-currently-playing user-read-recently-played';

$url = 'https://accounts.spotify.com/authorize?' . http_build_query([
  'response_type' => 'code',
  'client_id'     => SPOTIFY_CLIENT_ID,
  'scope'         => $scope,
  'redirect_uri'  => SPOTIFY_REDIRECT_URI,
  'state'         => $state,
  'show_dialog'   => 'true',
]);

header('Location: ' . $url);
exit;
