<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\CategoryController;
use App\Http\Controllers\ContactController;
use App\Http\Controllers\UserController;
use App\Http\Controllers\VideosController;
use App\Http\Controllers\WorkshopsController;
use App\Http\Middleware\JwtMiddleware;
use App\Http\Controllers\VideoViewController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\SectorController;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "api" middleware group. Make something great!
|
*/

Route::post('/register', [AuthController::class, 'register']);
Route::post('/login', [AuthController::class, 'login']);
Route::post('/contact', [ContactController::class, 'send']); //proteção da rota no ficheiro index da página de contactos

Route::middleware([JwtMiddleware::class])->group(function () {
    Route::post('/refresh', [AuthController::class, 'refresh']);
    /* Rota protegida por middleware JwtMiddleware - Só os utilizadores autenticados podem aceder a esta rota */
    Route::patch('/profile/{id}', [UserController::class, 'update']);
    Route::get('/profile', [UserController::class, 'profile']);

    Route::get('/videos', [VideosController::class, 'index']);
    Route::get('/video/{id}', [VideosController::class, 'getVideo']);
    Route::post('/video/{video}/watch', [VideoViewController::class, 'store']);
    Route::get('/videos-length', [VideosController::class, 'videosLength']);
    Route::get('/videos-search', [VideosController::class, 'search']);
    Route::get('/next-videos', [VideosController::class, 'nextVideos']);

    Route::get('/categories', [CategoryController::class, 'index']);
    Route::post('/categories', [CategoryController::class, 'create']);
    
    Route::get('/sectors', [SectorController::class, 'index']);
    Route::post('/sectors', [SectorController::class, 'create']);

    Route::get('/workshops', [WorkshopsController::class, 'index']);
    Route::get('/workshop/{id}', [WorkshopsController::class, 'getWorkshop']);
    Route::get('/workshops-length', [WorkshopsController::class, 'workshopsLength']);
    Route::get('/workshops-search', [WorkshopsController::class, 'search']);
    Route::get('/next-workshops', [WorkshopsController::class, 'nextWorkshops']);
    

    Route::post('/inscrever/{id}', [WorkshopsController::class, 'inscrever']);
    Route::delete('/cancelar-inscricao/{id}', [WorkshopsController::class, 'cancelarInscricao']);

    Route::get('/dashboard', [DashboardController::class, 'index']);

    /* Para fazer a verificação do role_id no frontend das páginas do admin */
    Route::get('/me', [AuthController::class, 'me']);

    /* Rota protegida por middleware AdminMiddleware - Só os administradores podem aceder as estas rotas */
    Route::middleware('admin')->group(function () {
        Route::get('/users', [UserController::class, 'index']);

        Route::post('/create-user', [UserController::class, 'create']);
        Route::get('/get-user/{id}', [UserController::class, 'getUser']);
        Route::patch('/edit-user/{id}', [UserController::class, 'update']);
        Route::delete('/edit-user/{id}', [UserController::class, 'destroy']);

        Route::post('/create-video', [VideosController::class, 'create']);
        Route::get('/edit-video/{id}', [VideosController::class, 'getVideo']);
        Route::patch('/edit-video/{id}', [VideosController::class, 'update']);
        Route::delete('/delete-video/{id}', [VideosController::class, 'destroy']); 

        Route::post('/create-workshop', [WorkshopsController::class, 'create']);
        Route::get('/edit-workshop/{id}', [WorkshopsController::class, 'getWorkshop']);
        Route::delete('/edit-workshop/{id}', [WorkshopsController::class, 'destroy']);
        Route::post('/edit-workshop/{id}', [WorkshopsController::class, 'update']);

        /* Depois de protergermos as rotas com middleware, criamos o layout para o admin em pages/privateadmin/_layout.tsx */
    });

});