<?php

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Artisan;
use App\Http\Controllers\PrintController;

/*
|--------------------------------------------------------------------------
| 1. ROUTE KHUSUS (TETAP DIPERTAHANKAN DI LARAVEL)
|--------------------------------------------------------------------------
| Route di bawah ini tetap bisa diakses karena ditulis sebelum Catch-All.
*/

// Route Cetak Surat Jalan, Invoice & NCR
Route::middleware('auth')->group(function () {
    Route::get('/print/surat-jalan/{id}', [PrintController::class, 'printSuratJalan'])->name('print.surat-jalan');
    Route::get('/print/invoice/{id}', [PrintController::class, 'printInvoice'])->name('print.invoice');
    Route::get('/print/ncr/{id}', [PrintController::class, 'printNcr'])->name('print.ncr'); // Dipindah ke sini agar rapi
});

// ROUTE MAINTENANCE (KHUSUS OWNER/ADMIN)
Route::prefix('system-maintenance')->middleware(['auth'])->group(function () {

    // URL: localhost:8000/system-maintenance/migrate
    Route::get('/migrate', function () {
        if (!in_array(auth()->user()->role, ['owner', 'admin'])) {
            abort(403, 'Hanya Owner yang boleh melakukan update database.');
        }

        try {
            Artisan::call('migrate', ['--force' => true]);
            $nextUrl = env('FRONTEND_URL', 'http://localhost:3000/admin/login');
            return "<h1>Update Database Berhasil!</h1><p>Tabel baru sudah dibuat.</p><a href='{$nextUrl}'>Kembali ke Aplikasi</a>";
        } catch (\Exception $e) {
            return "<h1>Gagal!</h1><p>" . $e->getMessage() . "</p>";
        }
    });

    // URL: localhost:8000/system-maintenance/storage-link
    Route::get('/storage-link', function () {
        if (!in_array(auth()->user()->role, ['owner', 'admin'])) {
            abort(403, 'Unauthorized.');
        }
        
        try {
            Artisan::call('storage:link');
            $nextUrl = env('FRONTEND_URL', 'http://localhost:3000/admin/login');
            return "<h1>Storage Link Berhasil!</h1><p>Gambar produk seharusnya sudah muncul.</p><a href='{$nextUrl}'>Kembali ke Aplikasi</a>";
        } catch (\Exception $e) {
            return "<h1>Gagal!</h1><p>" . $e->getMessage() . "</p>";
        }
    });

    // URL: localhost:8000/system-maintenance/clear-cache
    Route::get('/clear-cache', function () {
        if (!in_array(auth()->user()->role, ['owner', 'admin'])) {
            abort(403, 'Unauthorized.');
        }

        Artisan::call('optimize:clear');
        $nextUrl = env('FRONTEND_URL', 'http://localhost:3000/admin/login');
        return "<h1>Cache Bersih!</h1><p>Aplikasi sudah fresh kembali.</p><a href='{$nextUrl}'>Kembali ke Aplikasi</a>";
    });
});


/*
|--------------------------------------------------------------------------
| 2. CATCH-ALL ROUTE (MEMATIKAN SISA UI LARAVEL)
|--------------------------------------------------------------------------
*/
Route::any('{any}', function (\Illuminate\Http\Request $request) {
    
    // 1. Jika yang request adalah Axios/Next.js, tolak dengan format JSON
    if ($request->wantsJson() || $request->isXmlHttpRequest() || $request->is('api/*')) {
        return response()->json([
            'success' => false,
            'message' => 'API Route tidak ditemukan atau Sesi Anda telah habis.'
        ], 404);
    }

    // 2. Jika yang request adalah manusia lewat Browser, tendang ke UI Next.js
    $nextUrl = env('FRONTEND_URL', 'http://localhost:3000/admin/login'); 
    return redirect($nextUrl);

})->where('any', '.*');