<?php

namespace App\Http\Controllers;

use App\Models\Jadwal;
use Illuminate\Http\Request;
use App\Http\Requests\StoreJadwalRequest;
use App\Http\Requests\UpdateJadwalRequest;
use App\Models\Kelas;
use App\Models\Guru;
use App\Models\MataPelajaran;

class JadwalController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $jadwals = Jadwal::with(['kelas', 'mataPelajaran'])->get();
        $view = auth()->user()->isGuru() ? 'guru.jadwals.index' : 'jadwals.index';
        return view($view, compact('jadwals'));
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        $gurus = Guru::all();
        $kelas = Kelas::all();
        $mapels = MataPelajaran::all(); // pakai $mapel, bukan $mapels
        $view = auth()->user()->isGuru() ? 'guru.jadwals.create' : 'jadwals.create';
        return view($view, compact('kelas', 'mapels', 'gurus')); // kirim $mapel
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(StoreJadwalRequest $request)
    {
        Jadwal::create($request->validated());
        return redirect()->route('jadwals.index')->with('success', 'Jadwal berhasil ditambahkan');
    }

    /**
     * Display the specified resource.
     */
    public function show(Jadwal $jadwal)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(Jadwal $jadwal)
    {
        $kelas = Kelas::all();
        $mapels = MataPelajaran::all();
        $view = auth()->user()->isGuru() ? 'guru.jadwals.edit' : 'jadwals.edit';
        return view($view, compact('jadwal', 'kelas', 'mapels'));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(UpdateJadwalRequest $request, Jadwal $jadwal)
    {
        $jadwal->update($request->validated());
        return redirect()->route('jadwals.index')->with('success', 'Jadwal berhasil diupdate');
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(Jadwal $jadwal)
    {
        $jadwal->delete();
        return redirect()->route('jadwals.index')->with('success', 'Jadwal berhasil dihapus');
    }
}
