<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\ValidationException;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;

use App\Models\User;
use App\Models\Message;
use App\Models\Blog;
use App\Models\Comment;
use App\Models\BlogImage;

use App\Http\Controllers\UserController;
use App\Http\Controllers\BlogController;
use App\Http\Controllers\BlogImageController;
use App\Http\Controllers\BlogBannerController;
use App\Http\Controllers\CommentController;

/*
|--------------------------------------------------------------------------
| 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) {
    return $request->user();
});

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

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

    if (!$user || !Hash::check($request->password, $user->password)) {
        throw ValidationException::withMessages([
            'email' => ['The provided credentials are incorrect.'],
        ]);
    }

    return [
        'token' => $user->createToken('login')->plainTextToken,
        'userData' => json_encode($user),
    ];
});

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

Route::middleware('auth:sanctum')->group(function () {
    //users controller
    Route::post('/user/index', [UserController::class, 'index']);
    Route::post('/user/show', [UserController::class, 'show']);
    Route::post('/user/destroy', [UserController::class, 'destroy']);

    //blogs controller
    Route::post('/blog/index', [BlogController::class, 'index']);
    Route::post('/blog/index-page', [BlogController::class, 'index_page']);
    Route::post('/blog/sidebar', [BlogController::class, 'sidebar']);
    Route::post('/blog/total-count', [BlogController::class, 'total_count']);
    Route::post('/blog/show', [BlogController::class, 'show']);
    Route::post('/blog/store', [BlogController::class, 'store']);
    Route::post('/blog/edit', [BlogController::class, 'edit']);
    Route::post('/blog/destroy', [BlogController::class, 'destroy']);

    //blogs image controller
    Route::post('/blog-image/index', [BlogImageController::class, 'index']);
    Route::post('/blog-image/show', [BlogImageController::class, 'show']);
    Route::post('/blog-image/store', [BlogImageController::class, 'store']);

    Route::post('/blog-banner/index', [BlogBannerController::class, 'index']);
    Route::post('/blog-banner/store', [BlogBannerController::class, 'store']);

    //comments controller
    Route::post('/comment/index-by-blog', [CommentController::class, 'index_by_blog']);
    Route::post('/comment/index-count', [CommentController::class, 'index_count']);
    Route::post('/comment/show', [CommentController::class, 'show']);
    Route::post('/comment/store', [CommentController::class, 'store']);
    Route::post('/comment/destroy', [CommentController::class, 'destroy']);
});

Route::post('/blog-image/fake', function (Request $request) {
    $request->validate([
        'blogId' => 'required',
    ]);

    for ( $i=$request->blogId ; $i<11 ; $i++ ) {
        BlogImage::insert([
            'blogId' => $i,
            'saveName' => 'banner-'.$i.'.jpg',
            'fileName' => 'banner-'.$i.'.jpg',
            'created_at' => now(),
            'updated_at' => now(),
        ]);
    }

    return 'success';
});
