<?php

use App\Http\Controllers\ActivityController;
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\TemplateController;
use App\Models\User;
use Illuminate\Support\Facades\Hash;

use App\Http\Controllers\PrizeController;
use App\Http\Controllers\CheckInController;
use App\Http\Controllers\RecipientsController;
use App\Http\Controllers\DepartmentController;
use App\Http\Controllers\DrawController;
use App\Models\CheckIn;

/*
|--------------------------------------------------------------------------
| 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([
        'account' => 'required',
        'password' => 'required'
    ]);

    $user = User::where('account', $request->account)->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']);


/* Template API */
Route::post('/index', [TemplateController::class, 'index']);

Route::middleware('auth:sanctum')->group(function () {

});

Route::middleware(['auth:sanctum', 'abilities:Admin'])->group(function () {

    //activity
    Route::post('/activity/list', [ActivityController::class, 'index']);
    Route::post('/activity/store', [ActivityController::class, 'store']);
    Route::post('/activity/update', [ActivityController::class, 'update']);
    Route::post('/activity/sql', [ActivityController::class, 'sql']);
    Route::post('/activity/uploadFile', [ActivityController::class, 'uploadFile']);
    Route::post('/activity/show', [ActivityController::class, 'show']);

    // CheckIn
    Route::post('/check_in/update', [CheckInController::class, 'update']);
    Route::post('/check_in/check_out', [CheckInController::class, 'check_out']);
    Route::post('/check_in/draw', [CheckInController::class, 'draw_user_when_check_in']);
    Route::post('/checkin/show', [CheckInController::class, 'show']);
    Route::post('/checkin/export', [CheckInController::class, 'export']);

    // Draw
    Route::post('/draw/draw', [DrawController::class, 'draw']);

    // Prize

    // Recipients
    Route::post('/recipient/claim', [RecipientsController::class, 'claim']);
    Route::post('/recipient/isClaimed', [RecipientsController::class, 'isClaimed']);

    
});
Route::post('/user/index', [UserController::class, 'index']);
Route::post('/department/index', [DepartmentController::class, 'index']);
Route::post('/prize/index_by_activity', [PrizeController::class, 'index_by_activity']);
Route::post('/check_in/index_by_activity', [CheckInController::class, 'index_by_activity']);
Route::post('/recipients/index_by_activity', [RecipientsController::class, 'index_by_activity']);

Route::post('/prize/prizeDrawable', [PrizeController::class, 'prize_is_drawable']);
