<?php

namespace App\Http\Controllers;

use App\Models\Diagnosa;
use App\Models\Gejala;
use App\Models\Informasi;
use App\Models\Kontak;
use App\Models\Penyakit;
use App\Models\Rule;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Validation\Rules;

class ServerController extends Controller
{
    public function Dashboard()
    {
        $totalPenyakit = Penyakit::all()->count();
        $totalGejala = Gejala::all()->count();
        $totalPengguna = User::all()->count();
        $totalDiagnosa = Diagnosa::all()->count();
        $totalRules = Rule::all()->count();
        $totalArtikel = Informasi::all()->count();

        return view('server-side.pages.dashboard.dashboard', compact(
            ['totalPenyakit', 'totalGejala', 'totalPengguna', 'totalDiagnosa', 'totalRules', 'totalArtikel']
        ));
    }

    public function RegistrasiPengguna()
    {
        return view('server-side.pages.manajemen-pengguna.registrasi-pengguna');
    }

    public function RegistrasiStore(Request $request)
    {
        $request->validate([
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:' . User::class],
            'nope' => ['required', 'numeric'],
            'password' => ['required', 'confirmed', Rules\Password::defaults()],
            'role' => ['required'],
        ]);

        User::create([
            'name' => ucfirst($request->name),
            'email' => strtolower($request->email),
            'nope' => $request->email,
            'password' => Hash::make($request->password),
            'role' => $request->role,
        ]);

        return Redirect::route('data.pengguna')->with('message', 'Berhasil melakukan registrasi pengguna baru');
    }

    public function HapusPengguna($penggunaId)
    {
        User::findOrFail($penggunaId)->delete();
        return Redirect::route('data.pengguna')->with('message', 'Berhasil menghapus pengguna');
    }

    public function DataPengguna()
    {
        $numtab = 1;
        $pengguna = User::all();
        return view('server-side.pages.manajemen-pengguna.data-pengguna', compact(
            ['numtab', 'pengguna']
        ));
    }

    public function PesanPengguna()
    {
        $numtab = 1;
        $pesan = Kontak::all();
        return view('server-side.pages.manajemen-laporan.pesan.data', compact(
            ['numtab', 'pesan']
        ));
    }

    public function HapusPesan($id)
    {
        Kontak::find($id)->delete();
        return Redirect::route('pesan.pengguna')->with('message', 'Berhasil menghapus pesan pengguna yang dipilih');
    }
}
