<?php

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\CmsController;
use App\Http\Controllers\PimController;
use App\Http\Controllers\ImageController;

/*
|--------------------------------------------------------------------------
| 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::middleware('auth:sanctum')->get('/user', function (Request $request) {
    return $request->user();
});

Route::post('/postman', function(Request $request) {
    $user = User::where('email',  $request['email'])->first();
    $token = $user->createToken('auth_token')->plainTextToken;
    return response($token);
});

Route::group(['middleware' => 'auth:sanctum'], function () {

    Route::get('/token', function(){
        return response(auth()->user()->token);
    });

    Route::group(['prefix' => '/images'], function () {

        Route::post('/{service}/createOn/{model}/{id}', [ImageController::class, 'createOn']);
        Route::post('/delete/{id}', [ImageController::class, 'delete']);

    });

    Route::group(['prefix' => '/categories'], function () {
        Route::get('/index',                    [PimController::class, 'categories_index']);
        Route::get('/show/{category_id}',       [PimController::class, 'categories_show']);
        Route::post('/store',                   [PimController::class, 'categories_store']);
        Route::put('/update/{category_id}',     [PimController::class, 'categories_update']);
        Route::delete('/delete/{category_id}',  [PimController::class, 'categories_delete']);
    });  

    Route::group(['prefix' => '/properties'], function () {
        Route::get('/index',                    [PimController::class, 'properties_index']);
        Route::get('/show/{property_id}',       [PimController::class, 'properties_show']);
        Route::post('/store/{category_id}',     [PimController::class, 'properties_store']);
        Route::put('/update/{property_id}',     [PimController::class, 'properties_update']);
        Route::delete('/delete/{property_id}',  [PimController::class, 'properties_delete']);
        Route::post('/{property_id}/attachCategory/{category_id}', [PimController::class, 'properties_attach_category']);
        Route::post('/{property_id}/detachCategory/{category_id}', [PimController::class, 'properties_detach_category']);
    });    
    
    Route::group(['prefix' => '/products'], function () {
        Route::post('/store/{category_id}',     [PimController::class, 'products_store']);
        Route::put('/update/{product_id}',      [PimController::class, 'products_update']);
        Route::delete('/delete/{product_id}',   [PimController::class, 'products_delete']);
    });  

    Route::group(['prefix' => '/orders'], function () {
        Route::get('/index', [PimController::class, 'orders_index']);
        Route::get('/show/{order_id}', [PimController::class, 'orders_show']);
        Route::post('/setStatus/{order_id}', [PimController::class, 'orders_setStatus']);
    });

    Route::group(['prefix' => '/content'], function () {
        Route::post('/update/{content_id}',  [CmsController::class, 'content_update']);
    }); 
    
    Route::group(['prefix' => '/blog'], function () {
        route::post('/store', [cmscontroller::class, 'blog_create']);
        route::post('/update/{blog_id}', [cmscontroller::class, 'blog_update']);
        route::delete('/delete/{blog_id}', [cmscontroller::class, 'blog_delete']);
    });
    
});

