<?php

use App\Models\Author;
use App\Models\Category;
use App\Models\CountryLocale;
use App\Models\Post;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| 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::post('login', [App\Http\Controllers\Auth\LoginController::class, 'login'])->name('api.auth.login.post');

Route::middleware('auth:sanctum')->post('logout', [App\Http\Controllers\Auth\LoginController::class, 'logout'])->name('api.auth.logout.post');

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

    Route::get('/post/{id}', function ($id) {
        $post = Post::with('post_category.category')->find($id);

        return response()->json(compact('post'));

    })->name('api.admin.post.get');

    Route::get('/country-locales', function () {
        $country_locales = CountryLocale::where('enabled', true)->get();
        $default_locale_slug = 'my';

        return response()->json(compact('country_locales', 'default_locale_slug'));

    })->name('api.admin.country-locales');

    Route::get('/categories/{country_locale_slug}', function ($country_locale_slug) {
        $categories = Category::where('enabled', true)->where('country_locale_slug', $country_locale_slug)->get();

        return response()->json(compact('categories'));

    })->name('api.admin.categories');

    Route::get('/authors', function () {
        $authors = Author::where('enabled', true)->get();

        return response()->json(compact('authors'));

    })->name('api.admin.authors');

    Route::post('image/upload', [App\Http\Controllers\Services\ImageUploadController::class, 'index'])->name('api.admin.upload.cloud.image');

    Route::post('admin/post/upsert', [App\Http\Controllers\Admin\PostController::class, 'postUpsert'])->name('api.admin.post.upsert');

});
