<?php

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\GejalaController;
use App\Http\Controllers\PenyakitController;
use App\Http\Controllers\EdukasiController;
use App\Http\Controllers\RuleController;
use App\Http\Controllers\PasienController;
use App\Http\Controllers\DiagnosaController;
use App\Models\Diagnosa;
use App\Http\Controllers\Admin\DashboardController;
use App\Http\Controllers\UserDashboardController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
*/

// =========================
// HALAMAN UTAMA
// =========================
Route::get('/', function () {
    return view('landing');
});


// =========================
// AUTH
// =========================
Auth::routes();


// =========================
// REDIRECT SETELAH LOGIN
// =========================
Route::get('/home', [HomeController::class, 'index'])->name('home');


// =========================
// USER ROUTES
// =========================
Route::prefix('user')->middleware(['auth'])->group(function () {


   // Route::get('/dashboard', [DiagnosaController::class, 'dashboard'])
   // ->name('user.dashboard');

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


    // =========================
    // DATA DIRI (WAJIB DULU)
    // =========================
    Route::get('/data-diri', [PasienController::class, 'create'])
        ->name('user.data_diri');

    Route::post('/data-diri', [PasienController::class, 'store'])
        ->name('user.data_diri.store');


    // =========================
    // DIAGNOSA (FIX TOTAL)
    // =========================
    Route::get('/diagnosa', function () {

        $pasien = \App\Models\Pasien::where('user_id', auth()->id())->first();

        if (!$pasien) {
            return redirect()->route('user.data_diri');
        }

        return app(DiagnosaController::class)->index();
        
    })->name('user.diagnosa');

    Route::post('/diagnosa/proses', [DiagnosaController::class, 'proses'])
        ->name('diagnosa.proses');

    Route::get('/diagnosa/hasil/{id}', [DiagnosaController::class, 'hasil'])
        ->name('diagnosa.hasil');

    Route::get('/diagnosa/pdf/{id}', [DiagnosaController::class, 'pdf'])
        ->name('diagnosa.pdf');


    // =========================
    // RIWAYAT
    // =========================
    Route::get('/riwayat', [DiagnosaController::class, 'riwayat'])
        ->name('user.riwayat');

    Route::get('/diagnosa/hasil/{id}', [DiagnosaController::class, 'hasil'])
    ->name('diagnosa.hasil');


    // =========================
    // EDUKASI
    // =========================
    Route::get('/edukasi', [EdukasiController::class, 'userIndex'])
        ->name('user.edukasi');

    Route::get('/edukasi/{id}', [EdukasiController::class, 'showUser'])
        ->name('user.edukasi.detail');

});


// =========================
// ADMIN ROUTES
// =========================
Route::prefix('admin')->middleware(['auth'])->group(function () {

    // Dashboard
    // Route::get('/dashboard', function () {
       // return view('admin.dashboard');
    // })->name('admin.dashboard');


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


    // =========================
    // DATA PASIEN
    // =========================
Route::get('/pasien', function (Illuminate\Http\Request $request) {

    $perPage = $request->perPage ?? 5;

    $query = \App\Models\Pasien::with('user');

    //  SEARCH
    if ($request->search) {

        $query->where('nama', 'like', '%' . $request->search . '%')
              ->orWhere('alamat', 'like', '%' . $request->search . '%')
              ->orWhere('jenis_kelamin', 'like', '%' . $request->search . '%');
    }

    $pasiens = $query->latest()
        ->paginate($perPage)
        ->withQueryString();

    return view('admin.pasien', compact('pasiens', 'perPage'));

})->name('admin.pasien');


    // =========================
    // DATA GEJALA
    // =========================
    Route::resource('gejala', GejalaController::class);


    // =========================
    // DATA PENYAKIT
    // =========================
    Route::resource('penyakit', PenyakitController::class);


    // =========================
    // DATA EDUKASI
    // =========================
    Route::resource('edukasi', EdukasiController::class);


    // =========================
    // RULE CF
    // =========================
    Route::resource('rule', RuleController::class);


    // =========================
    // RIWAYAT
    // =========================
Route::get('/riwayat', function (Illuminate\Http\Request $request) {

    $perPage = $request->perPage ?? 5;

    $query = Diagnosa::with(['pasien','penyakit']);

    //  SEARCH
    if ($request->search) {

        $query->whereHas('pasien', function ($q) use ($request) {
            $q->where('nama', 'like', '%' . $request->search . '%');
        })

        ->orWhereHas('penyakit', function ($q) use ($request) {
            $q->where('nama', 'like', '%' . $request->search . '%');
        });
    }

    $data = $query->latest()
        ->paginate($perPage)
        ->withQueryString();

    return view('admin.riwayat', compact('data', 'perPage'));

})->name('admin.riwayat');

    Route::get('/admin/riwayat',)->name('admin.riwayat');

});