<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\ApplicationController;
use App\Http\Controllers\API\UserController;
use App\Http\Controllers\FooController;
use App\Http\Controllers\MessageController;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
    $user = $request->user();
    $islogin = $user->tokenCan('update');
    $test = $islogin ? 'true' : 'false';
    return $user;
});




Route::post('/login', function (Request $request) {
    $data = $request->validate([
        'email' => 'required|email',
        'password' => 'required'
    ]);

    $user = User::where('email', $request->email)->first();

    if (!$user || !Hash::check($request->password, $user->password)) {
        return response([
            'message' => ['These credentials do not match our records.']
        ], 404);
    }

    $token = $user->createToken('test-token', [$user->role])->plainTextToken;
    $response = [
        'user' => JSON_ENCODE($user, JSON_UNESCAPED_UNICODE),
        'token' => $token,
    ];

    return response($response, 201);
});
Route::post('/logout', function (Request $request) {
    $user = $request->user();
    $user->tokens()->delete();

    return $user;
})->middleware(['auth:sanctum']);

Route::post('/refresh', function (Request $request) {
    $user = $request->user();
    $user->tokens()->delete();

    $token = $user->createToken('test-token', ['uploader', 'tester'])->plainTextToken;

    $response = [
        'user' => $user,
        'token' => $token,
    ];

    return response($response, 201);
})->middleware(['auth:sanctum']);

Route::middleware('auth:sanctum')->group(function () {
    //
    Route::post('/foo/create', [FooController::class, 'create']);
    Route::post('/foo/store', [FooController::class, 'store']);
    Route::post('/foo/update', [FooController::class, 'update']);
});
Route::post('/foo', [FooController::class, 'index'])->middleware(['auth:sanctum']);
Route::get('/foo/show', [FooController::class, 'show'])->middleware(['auth:sanctum', 'abilities:User']);

/*Message Borad */
Route::post('/message/index', [MessageController::class, 'index']);
Route::post('/message/recent', [MessageController::class, 'recent']);
Route::post('/message/detail', [MessageController::class, 'show']);
Route::post('/message/count', [MessageController::class, 'count']);
Route::post('/message/user', [UserController::class, 'index']);

Route::middleware('auth:sanctum')->group(function () {
    //
    Route::post('/message/storeComment', [MessageController::class, 'storeComment']);
    Route::post('/message/storeMessage', [MessageController::class, 'store']);
});


