<?php

use App\Models\Post;
use App\Models\User;
use App\Models\Category;
use App\Models\Kriteria;
use App\Models\Alternatif;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;
use App\Http\Controllers\HistorisController;
use App\Http\Controllers\KriteriaController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\PenilaianController;
use App\Http\Controllers\AlternatifController;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\PerhitunganController;
use App\Http\Controllers\SubKriteriaController;
use App\Http\Controllers\UserSettingController;
use App\Http\Controllers\KelompokTaniController;
use App\Http\Controllers\HistorisPerhitunganController;

// LOGIN route tetap di luar
Route::get('auth/login', [LoginController::class, 'showLoginForm'])->name('login');
Route::post('auth/login', [LoginController::class, 'login']);
Route::post('auth/logout', [LoginController::class, 'logout'])->name('logout');

// Landing page (bebas diakses)
Route::get('/', function () {
    return view('index');
});

// Semua route yang butuh login
Route::middleware('auth')->group(function () {

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

    // KRITERIA
    Route::resource('/admin/kriteria', KriteriaController::class);
    Route::post('/admin/kriteria', [KriteriaController::class, 'store'])->name('kriteria.store');
    Route::delete('/admin/kriteria/{id}', [KriteriaController::class, 'destroy'])->name('kriteria.destroy');

    // SUBKRITERIA
    Route::get('/admin/kriteria/{kriteria_id}/subkriteria', [SubKriteriaController::class, 'index'])->name('admin.subkriteria.index');
    Route::post('/subkriteria', [SubKriteriaController::class, 'store'])->name('subkriteria.store');
    Route::delete('/admin/subkriteria/{id}', [SubKriteriaController::class, 'destroy'])->name('subkriteria.destroy');
    Route::put('/admin/subkriteria/{id}', [SubKriteriaController::class, 'update'])->name('subkriteria.update');

    // ALTERNATIF
    Route::get('/admin/alternatif', [AlternatifController::class, 'index'])->name('alternatif.index');
    Route::post('/admin/alternatif', [AlternatifController::class, 'store'])->name('alternatif.store');
    Route::delete('/admin/alternatif/{id}', [AlternatifController::class, 'destroy'])->name('alternatif.destroy');
    Route::put('/admin/alternatif/{id}', [AlternatifController::class, 'update'])->name('alternatif.update');
    Route::delete('/alternatif/hapus-semua', [AlternatifController::class, 'destroyAll'])->name('alternatif.destroyAll');
    Route::resource('alternatif', AlternatifController::class);

    // PENILAIAN
    Route::get('/admin/penilaian', [PenilaianController::class, 'index'])->name('penilaian.index');
    Route::post('/admin/penilaian', [PenilaianController::class, 'store'])->name('penilaian.store');

    // PERHITUNGAN
    Route::get('/admin/perhitungan', [PerhitunganController::class, 'index'])->name('perhitungan.index');
    Route::post('/perhitungan/simpan-historis', [PerhitunganController::class, 'simpanKeHistoris'])->name('perhitungan.simpanHistoris');
    Route::get('/perhitungan/historis', [PerhitunganController::class, 'historis'])->name('perhitungan.historis');

    // USER SETTING
    Route::get('/admin/usersetting', [UserSettingController::class, 'index'])->name('usersetting.index');
    Route::post('/admin/usersetting/store', [UserSettingController::class, 'store'])->name('usersetting.store');
    Route::put('/admin/usersetting/update/{id}', [UserSettingController::class, 'update'])->name('usersetting.update');
    Route::delete('/admin/usersetting/delete/{id}', [UserSettingController::class, 'destroy'])->name('usersetting.destroy');
    Route::delete('/admin/usersetting/destroyAll', [UserSettingController::class, 'destroyAll'])->name('usersetting.destroyAll');

    // KELOMPOK TANI (khusus superadmin, pengecekan di controller)
    Route::get('/admin/kelompoktani', [KelompokTaniController::class, 'index'])->name('kelompoktani.index');
    Route::post('/admin/kelompoktani', [KelompokTaniController::class, 'store'])->name('kelompoktani.store');
    Route::put('/admin/kelompoktani/{id}', [KelompokTaniController::class, 'update'])->name('kelompoktani.update');
    Route::delete('/admin/kelompoktani/{id}', [KelompokTaniController::class, 'destroy'])->name('kelompoktani.destroy');

});


