<?php

use Illuminate\Support\Facades\Route;
use Livewire\Volt\Volt;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\DashboardController;
use App\Models\Province;
use Illuminate\Support\Facades\Storage;

use Illuminate\Support\Facades\Artisan;

use function Ramsey\Uuid\v1;

Route::get('/storage-link', function () {
    Artisan::call('storage:link');
    return 'Storage linked!';
});


/*
|--------------------------------------------------------------------------
| Frontend Routes
|--------------------------------------------------------------------------
*/
Route::get('/', [HomeController::class, 'index'])->name('home');
Route::post('/aksara/print', [HomeController::class,'print'])->name('aksara.print');
//Route::get('/about', [HomeController::class, 'frontendAbout'])->name('about');

Route::get('/csrf-token', function () {
    return response()->json([
        'token' => csrf_token()
    ]);
});

Route::get('/generate-thumbs', function () {

    ini_set('memory_limit', '512M');
    set_time_limit(300);

    $manager = new \Intervention\Image\ImageManager(
        new \Intervention\Image\Drivers\Gd\Driver()
    );

    $files = Storage::disk('public')->files('gallery');

    foreach ($files as $file) {

        // hanya image
        if (!preg_match('/\.(jpg|jpeg|png|webp)$/i', $file)) {
            continue;
        }

        $filename = basename($file);
        $thumbPath = 'gallery/thumbs/' . $filename;

        if (Storage::disk('public')->exists($thumbPath)) {
            continue;
        }

        // AUTO DETECT PATH
        $path = Storage::disk('public')->path($file);

        // fallback untuk local (kalau beda config)
        if (!file_exists($path)) {
            $path = storage_path('app/public/' . $file);
        }

        if (!file_exists($path) || filesize($path) == 0) {
            continue;
        }

        try {

            $image = $manager->read($path);

            $image->cover(300, 300);

            Storage::disk('public')->put(
                $thumbPath,
                (string) $image->encode()
            );

        } catch (\Throwable $e) {
            continue;
        }
    }

    return 'DONE';
});

Route::get('/{slug}', function ($slug) {
    $page = \App\Models\Page::where('slug',$slug)->firstOrFail();
    return view('frontend.page',compact('page'));
});

Route::get('/province/{province:slug}', function (Province $province) {
    $province->load('galleryCategories');
    return view('frontend.province.show', compact('province'));
});
Route::get('/api/province-gallery/{province}', [HomeController::class, 'searchProvinceGallery']);

Route::get('/js/aksara-converter.js', function () {
    return response()
        ->view('frontend.aksara.converter_aksara')
        ->header('Content-Type', 'application/javascript');
});

/*
|--------------------------------------------------------------------------
| Backend Routes
|--------------------------------------------------------------------------
*/

Route::middleware(['auth', 'admin'])
    ->prefix('admin')
    ->group(function () {

        Route::get('/dashboard', [DashboardController::class, 'index'])
            ->name('dashboard');

        Volt::route('/contents/homepage', 'contents.homepage')
            ->name('contents.homepage');

        Volt::route('/contents/pages', 'contents.pages.index')
            ->name('contents.pages.index');

        Volt::route('/contents/pages/create', 'contents.pages.form')
            ->name('contents.pages.create');

        Volt::route('/contents/pages/{page}/edit', 'contents.pages.form')
            ->name('contents.pages.edit');

        Volt::route('/provinces', 'provinces.index')
            ->name('provinces.index');
        Volt::route('/provinces/create', 'provinces.form')
            ->name('provinces.create');
        Volt::route('/provinces/{province}/edit', 'provinces.form')
            ->name('provinces.edit');

        Volt::route('/gallery-categories', 'gallerycategories.index')
            ->name('gallerycategories.index');
        Volt::route('/gallery-categories/create', 'gallerycategories.form')
            ->name('gallerycategories.create');
        Volt::route('/gallery-categories/{id}/edit', 'gallerycategories.form')
            ->name('gallerycategories.edit');
        Volt::route('/gallery-categories/{id}', 'gallerycategories.show')
            ->name('gallerycategories.show');

        Volt::route('/galleries', 'galleries.index')
            ->name('galleries.index');
        Volt::route('/galleries/create', 'galleries.form')
            ->name('galleries.create');
        Volt::route('/galleries/{gallery}/edit', 'galleries.form')
            ->name('galleries.edit');

        Volt::route('/sliders', 'sliders.index')
            ->name('sliders.index');
        Volt::route('/sliders/create', 'sliders.form')
            ->name('sliders.create');
        Volt::route('/sliders/{slider}/edit', 'sliders.form')
            ->name('sliders.edit');

        Volt::route('/users', 'users.index')
            ->name('users.index');
        Volt::route('/users/create', 'users.form')
            ->name('users.create');
        Volt::route('/users/{user}/edit', 'users.form')
            ->name('users.edit');

});

require __DIR__.'/settings.php';
