<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

use \App\Http\Controllers\UserController;
use \App\Http\Controllers\DestinationController;
use \App\Http\Controllers\PostController;
use \App\Http\Controllers\AttendController;

// User actions
Route::group(['prefix' => '/account'], function() {
    Route::post('/register', [UserController::class, 'store']);
    Route::post('/login', [UserController::class, 'login']);

    Route::group(['middleware' => ['auth:sanctum']], function() {
        Route::post('/logout', [UserController::class, 'logout']);
        Route::get('/user', [UserController::class, 'getUser']);
        Route::patch('/', [UserController::class, 'update']);
        Route::delete('/', [UserController::class, 'destroy']);
    });
});

// Single action routes
Route::get('/countries', function() {
    return response()->json([
        'success' => true,
        'message' => 'Countries indexed successfully.',
        'countries' => \App\Models\Country::all()
    ]);
});

Route::get('/faq', function() {
    return response()->json([
        'success' => true,
        'message' => 'Questions indexed successfully.',
        'questions' => \App\Models\Question::all()
    ]);
});
Route::get('/questions', \App\Http\Controllers\QuestionController::class);

// Destination and Post actions
Route::get('/destinations/search/{searchTerm}', [DestinationController::class, 'search']);
Route::resource('destinations', DestinationController::class)->only(['index', 'show']);
Route::resource('posts', PostController::class)->only(['index', 'show']);

Route::group(['middleware' => ['auth:sanctum']], function() {
    Route::post('/destinations/{destination}', [DestinationController::class, 'update']);
    Route::get('/my-destinations', [DestinationController::class, 'indexMine']);
    Route::get('/by-me-destinations', [DestinationController::class, 'indexByMe']);
    Route::apiResource('destinations', DestinationController::class)->except(['index', 'show', 'update']);

    Route::post('/posts/{post}', [PostController::class, 'update']);
    Route::apiResource('posts', PostController::class)->except(['index', 'show', 'update']);
});

Route::get('/attends/{id}', [AttendController::class, 'show']);
Route::get('/attends/total/{id}', [AttendController::class, 'indexForDestination']);

Route::group(['middleware' => ['auth:sanctum']], function() {
    Route::post('/attends/check/{id}', [AttendController::class, 'attendByDestination']);
    Route::post('/attends/{id}', [AttendController::class, 'store']);
    Route::delete('/attends/{id}', [AttendController::class, 'destroy']);
});
