<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Fakultas;
use App\Models\Prodi;
use Illuminate\Http\Request;
use Inertia\Inertia;

class FakultasProdiController extends Controller
{
    public function index()
    {
        $fakultas = Fakultas::with('prodis')->get();

        return Inertia::render('Admin/FakultasProdi/Index', [
            'fakultas' => $fakultas,
        ]);
    }

    // Fakultas
    public function storeFakultas(Request $request)
    {
        $request->validate(['nama' => 'required|string|max:255'],['nama.required' => 'Nama fakultas harus diisi']);
        Fakultas::create($request->only('nama'));

        return redirect()->back()->with('success', 'Fakultas berhasil dibuat');
    }

    public function updateFakultas(Request $request, Fakultas $fakultas)
    {
        $request->validate(['nama' => 'required|string|max:255'],['nama.required' => 'Nama fakultas harus diisi']);
        $fakultas->update($request->only('nama'));

        return redirect()->back()->with('success', 'Fakultas berhasil diperbarui');
    }

    public function destroyFakultas(Fakultas $fakultas)
    {
        $fakultas->delete();
        return redirect()->back()->with('success', 'Fakultas berhasil dihapus');
    }

    // Prodi
    public function storeProdi(Request $request)
    {
        $request->validate([
            'fakultas_id' => 'required|exists:fakultas,id',
            'nama' => 'required|string|max:255',
        ],[
            'fakultas_id.required' => 'Fakultas harus dipilih',
            'fakultas_id.exists' => 'Fakultas tidak ditemukan',
            'nama.required' => 'Nama prodi harus diisi',
        ]);

        Prodi::create($request->only('fakultas_id', 'nama'));

        return redirect()->back()->with('success', 'Prodi berhasil dibuat');
    }

    public function updateProdi(Request $request, Prodi $prodi)
    {
        $request->validate([
            'fakultas_id' => 'required|exists:fakultas,id',
            'nama' => 'required|string|max:255',
        ],[
            'fakultas_id.required' => 'Fakultas harus dipilih',
            'fakultas_id.exists' => 'Fakultas tidak ditemukan',
            'nama.required' => 'Nama prodi harus diisi',
        ]);

        $prodi->update($request->only('fakultas_id', 'nama'));

        return redirect()->back()->with('success', 'Prodi berhasil diperbarui');
    }

    public function destroyProdi(Prodi $prodi)
    {
        $prodi->delete();
        return redirect()->back()->with('success', 'Prodi berhasil dihapus');
    }
}