<?php

use App\Http\Controllers\Admin\UserController;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\LatihController;
use App\Http\Controllers\UjiController;
use App\Http\Controllers\PrediksiController;
use App\Http\Controllers\AdminController;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\Admin\KasusController;
use App\Http\Controllers\ImportController;
use App\Http\Controllers\KabupatenController;
use Illuminate\Support\Facades\Auth;


/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

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

// Routes with authentication middleware
Route::middleware('auth')->group(function () {
    Route::get('/admin/dashboard', [AdminController::class, 'dashboard'])->name('admin.dashboard');
    Route::get('/data-latih', [LatihController::class, 'index'])->name('data-latih.index');
    Route::post('/data-latih/import', [LatihController::class, 'import'])->name('admin.data-latih.import');
    Route::get('/uji-data', [UjiController::class, 'ujiData'])->name('admin.uji-data');
    Route::get('/users', [UserController::class, 'index'])->name('admin.users');
    Route::post('/import-excel', [ImportController::class, 'import'])->name('import.excel');
});

// Admin routes with prefix
Route::prefix('admin')->middleware('auth')->name('admin.')->group(function () {
    Route::resource('users', UserController::class)->names('admin.users');
    Route::resource('data-latih', LatihController::class);
    Route::resource('users', UserController::class);
    Route::resource('kabupaten', KabupatenController::class);
    Route::resource('kasus', KasusController::class);
    Route::resource('data-latih', LatihController::class);

    Route::get('data-latih/edit/{id}', [LatihController::class, 'edit'])->name('data-latih.edit');
    Route::get('data-latih', [LatihController::class, 'index'])->name('data-latih.index');
    Route::get('/login', [LoginController::class, 'showLoginForm'])->name('login');
    Route::post('/login', [LoginController::class, 'login']);
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');
    Route::get('/data-latih', [LatihController::class, 'index'])->name('data-latih');
    Route::get('/uji-data', [AdminController::class, 'ujiData'])->name('uji-data');
    Route::get('/prediksi', [PrediksiController::class, 'showPrediksi'])->name('prediksi');
    Route::get('/prediksi', [PrediksiController::class, 'index'])->name('prediksi.index');
    Route::get('/prediksi/export', [PrediksiController::class, 'exportExcel'])->name('prediksi.export');
    // Kalau kamu butuh route manual ini bisa dipertimbangkan untuk dihapus karena sudah ada di resource
    // Route::get('/users', [AdminController::class, 'users'])->name('users'); 
    Route::post('/users', [UserController::class, 'store'])->name('users.store');
    Route::post('/logout', [AdminController::class, 'logout'])->name('logout');
    Route::get('/kabupaten', [AdminController::class, 'kabupaten'])->name('kabupaten');
});


// Edit user routes
Route::get('/admin/{id}/edit', [AdminController::class, 'edit'])->name('admin.edit');
Route::put('/admin/{id}', [AdminController::class, 'update'])->name('admin.update');

// Route lainnya
Route::get('/kabupaten', [KabupatenController::class, 'index'])->name('kabupaten.index');
Route::get('/kabupaten/create', [KabupatenController::class, 'create'])->name('kabupaten.create');
Route::post('/kabupaten', [KabupatenController::class, 'store'])->name('kabupaten.store');

// Uji Data routes
Route::get('/admin/uji-data', [UjiController::class, 'index'])->name('admin.uji-data');
Route::post('/uji-data/proses', [UjiController::class, 'proses'])->name('admin.uji-data.proses');

// Logout route
Route::post('/logout', function () {
    Auth::logout();
    return redirect('/login');
})->name('logout');

// Authentication routes
require __DIR__.'/auth.php';
Route::get('/admin/dashboard', [DashboardController::class, 'dashboard'])->name('admin.dashboard');
Route::post('/uji-data/proses', [UjiController::class, 'proses'])->name('uji-data.proses');

Route::prefix('admin')->name('admin.')->group(function () {
    Route::resource('kabupaten', \App\Http\Controllers\KabupatenController::class);
    Route::put('/kabupaten/{id}', [KabupatenController::class, 'update'])->name('kabupaten.update');
    Route::get('/admin/kabupaten/{id}/edit', [KabupatenController::class, 'edit'])->name('kabupaten.edit'); // Rute untuk menampilkan form edit
Route::put('/admin/kabupaten/{id}', [KabupatenController::class, 'update'])->name('kabupaten.update'); // Rute untuk menangani request PUT
});

Route::middleware(['auth', 'admin'])->prefix('admin')->name('admin.')->group(function () {
    Route::post('/users', [UserController::class, 'store'])->name('users.store');
    Route::delete('/users/{id}', [UserController::class, 'destroy'])->name('users.destroy');
});
Route::delete('/data-latih/hapus-semua', [LatihController::class, 'destroyAll'])->name('data-latih.destroyAll');
Route::get('/data-latih/edit/{id}', [LatihController::class, 'edit'])->name('edit');
Route::put('/data-latih/update/{id}', [LatihController::class, 'update'])->name('update');
Route::delete('/data-latih/delete/{id}', [LatihController::class, 'delete'])->name('delete');
Route::post('/uji-data/reset', [UjiController::class, 'reset'])->name('uji-data.reset');
Route::get('/uji', [UjiController::class, 'index'])->name('uji.index');
Route::post('/uji-data/upload', [UjiController::class, 'upload'])->name('uji-data.upload');
// LOGIN ROUTE
Route::get('/login', [LoginController::class, 'showLoginForm'])->name('login');
Route::post('/login', [LoginController::class, 'login']);

// LOGOUT ROUTE
Route::post('/logout', [LoginController::class, 'logout'])->name('logout');
Route::get('/uji-data', [UjiController::class, 'index']);
Route::post('/uji-data/proses', [UjiController::class, 'proses'])->name('uji.proses');
Route::post('/uji/reset', [UjiController::class, 'reset'])->name('uji.reset');
Route::get('/uji/reset', [UjiController::class, 'reset'])->name('uji.reset');
Route::post('/uji/proses', [UjiController::class, 'proses'])->name('uji.proses');
Route::get('/admin/dashboard', [DashboardController::class, 'index'])->name('admin.dashboard');
Route::get('/prediksi', [PrediksiController::class, 'index'])->name('prediksi.index');
Route::get('/uji-data', [UjiController::class, 'showUjiData'])->name('uji-data');
Route::post('/uji-data/upload', [UjiController::class, 'upload'])->name('uji-data.upload');
Route::post('/admin/uji-data', [UjiController::class, 'upload'])->name('uji-data.upload');
Route::get('/uji-data', [PrediksiController::class, 'showUjiData'])->name('uji.data');
Route::get('/admin/prediksi', [PrediksiController::class, 'index'])->name('admin.prediksi.index');
Route::get('/kabupaten/create', [KabupatenController::class, 'create'])->name('admin.kabupaten.create');
Route::get('/data-latih', [LatihController::class, 'index'])->name('data-latih.index');
Route::get('/data-latih/edit/{id}', [LatihController::class, 'edit'])->name('data-latih.edit');
Route::get('/data-latih/{id}/edit', [LatihController::class, 'edit'])->name('edit');
Route::post('/admin/uji-data', [UjiController::class, 'upload'])->name('uji-data.upload');
Route::post('/admin/users', [App\Http\Controllers\AdminController::class, 'store'])->name('admin.users.store');