<?php

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Admin\UserController;
use App\Http\Controllers\Admin\ProductController;
use App\Http\Controllers\Admin\CategoryController;
use App\Http\Controllers\Admin\DocumentController;
use App\Http\Controllers\Admin\DashboardController;
use App\Http\Controllers\Admin\ChatLogController;
use App\Http\Controllers\PageController;
use App\Http\Controllers\ChatbotController;
use App\Http\Controllers\ArticleController;
use App\Http\Controllers\PublicProdukController;
use App\Http\Controllers\ContactController;



// --- RUTE PUBLIK ---
Route::get('/export-ragas', [ChatLogController::class, 'exportRagas']);
Route::get('/', [PageController::class, 'home'])->name('beranda');
Route::get('/artikel', [ArticleController::class, 'index'])->name('articles.index');
Route::get('/artikel/{article}', [ArticleController::class, 'show'])->name('articles.show');
Route::get('articles/{category:slug?}', [App\Http\Controllers\ArticleController::class, 'index'])->name('articles.index');
Route::get('/artikel/kategori/{category:slug}', [ArticleController::class, 'index'])->name('articles.category');
Route::get('/artikel/{category:slug?}', [ArticleController::class, 'index'])->name('articles.index');
Route::post('/chatbot/send', [ChatbotController::class, 'handle'])->name('chatbot.send');
Route::get('/produk-binaan', [PublicProdukController::class, 'index'])->name('produk.binaan');
Route::get('/produk-binaan/{id}', [PublicProdukController::class, 'show'])->name('produk.show');
Route::get('/konsultasi', function () {
    return view('konsultasi');
})->name('konsultasi.mandiri');
Route::get('/kontak', [ContactController::class, 'index'])->name('kontak.index');
Route::post('/kontak', [ContactController::class, 'store'])->name('kontak.store');
Auth::routes(['register' => false]);

Route::get('/home', function () {
    if (auth()->user()->is_admin) {
        return redirect()->route('admin.dashboard');
    }
    return redirect()->route('beranda');
})->middleware('auth');

Route::get('/admin', function () {
    return redirect()->route('login');
})->middleware('guest');


// --- RUTE ADMIN 
Route::middleware(['auth', 'admin'])->prefix('admin')->name('admin.')->group(function () {
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');
    // Halaman Admin Riwayat Chat
    Route::get('/chatlogs', [ChatLogController::class, 'index'])
        ->name('chatlogs.index');

    // Validasi jawaban chatbot
    Route::post('/chatlog/{id}/validate', [ChatLogController::class, 'validateAnswer'])
        ->name('chatlog.validate');
    // Kelola User/Admin
    Route::resource('users', UserController::class);
    Route::resource('categories', CategoryController::class);
    // Kelola Artikel Admin
    Route::get('/artikel', [ArticleController::class, 'adminIndex'])->name('articles.index');
    Route::get('/artikel/create', [ArticleController::class, 'create'])->name('articles.create');
    Route::post('/artikel', [ArticleController::class, 'store'])->name('articles.store');
    Route::get('/artikel/{article}/edit', [ArticleController::class, 'edit'])->name('articles.edit');
    Route::put('/artikel/{article}', [ArticleController::class, 'update'])->name('articles.update');
    Route::delete('/artikel/{article}', [ArticleController::class, 'destroy'])->name('articles.destroy');
    // Kelola dokumen chatbot
    Route::get('/documents', [DocumentController::class, 'index'])->name('documents.index');
    Route::post('/documents', [DocumentController::class, 'store'])->name('documents.store');
    // Kelola Produk
    Route::resource('products', ProductController::class);
    // kelola dokumen negara pancasila
    Route::delete('/documents/{filename}', [DocumentController::class, 'destroy'])->name('documents.destroy');
    // Halaman Admin Pesan
    Route::get('/pesan', [ContactController::class, 'adminIndex'])->name('pesan.index');
    Route::delete('/pesan/{id}', [ContactController::class, 'destroy'])->name('pesan.destroy');
    //ragas
    
});
