<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Api\ArtistController;
use App\Http\Controllers\Api\AuthController;
use App\Http\Controllers\Api\BranchControlController;
use App\Http\Controllers\Api\SyncController;
use App\Http\Controllers\Api\RoomControlController;
use App\Http\Controllers\Api\RoomSongController;
use App\Http\Controllers\Api\SongSearchController;
use Illuminate\Http\Request;
use Laravel\Sanctum\PersonalAccessToken;

Route::get('/artists/search', [ArtistController::class, 'search'])->name('api.artists.search');

Route::post('/login', [AuthController::class, 'login']);
Route::post('/room/receiveRegister', [RoomControlController::class, 'receiveRegister']);

Route::post('/token/validate', function (Request $request) {
    $token = $request->bearerToken();

    if (!$token) {
        return response()->json(['valid' => false, 'message' => 'Token missing'], 401);
    }

    $accessToken = PersonalAccessToken::findToken($token);

    if (!$accessToken) {
        return response()->json(['valid' => false, 'message' => 'Invalid token'], 401);
    }

    return response()->json([
        'valid' => true,
        'user_id' => $accessToken->tokenable_id,
        'abilities' => $accessToken->abilities,
    ]);
});

Route::middleware('auth:sanctum')->group(function () {
    Route::get ('/profile', [AuthController::class, 'profile']);
    Route::get ('/branches',[BranchControlController::class, 'Branches']);

    Route::post('/room/sendSwitch', [RoomControlController::class, 'sendSwitch']);
    Route::post('/room/receiveSwitch', [RoomControlController::class, 'receiveSwitch']);
    Route::post('/room/session',[RoomControlController::class, 'session']);
    Route::post('/room/order-song', [RoomSongController::class, 'orderSong']);
    Route::get ('/room/ordered-songs', [RoomSongController::class, 'listOrderedSongs']);
    Route::post ('/room/sync-order-song', [RoomSongController::class, 'syncOrderSong']);
});