<?php

namespace App\Http\Controllers;

use App\Models\Guru;
use App\Models\Kelas;
use App\Models\Mapel;
use App\Models\Materi;
use App\Models\Mengajar;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

class MengajarController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $mengajar = Mengajar::with(['mata_pelajaran', 'guru', 'kelas'])->get();
        $mapel = Mapel::all();
        $guru = Guru::all();
        $kelas = Kelas::all();

        return view('mengajar.index', ['mengajar' => $mengajar, 'mapel' => $mapel, 'guru' => $guru, 'kelas' => $kelas]);
    }

    public function show($classId)
    {
        $mengajar = Mengajar::where('id_kelas', $classId)->get();
        $kelas = Kelas::find($classId);
        return view('mengajar.show', compact('mengajar', 'kelas'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'kelas' => 'required',
            'guru' => 'required',
            'mata_pelajaran' => 'required',
        ]);

        if ($validator->fails()) {
            return redirect()->back()
                ->withErrors($validator)
                ->withInput();
        }

        try {
            DB::beginTransaction();

            $mengajar = new Mengajar();
            $mengajar->id_kelas = $request->kelas;
            $mengajar->id_guru = $request->guru;
            $mengajar->id_mapel = $request->mata_pelajaran;
            $mengajar->save();

            DB::commit();

            return redirect()->back()->with('success', 'Data mengajar berhasil ditambahkan.');
        } catch (\Exception $e) {
            DB::rollback();
            return back()->withErrors(['error' => $e->getMessage()]);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    // public function show($id)
    // {
    //     //
    // }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $validator = Validator::make($request->all(), [
            'kelas' => 'required',
            'guru' => 'required',
            'mata_pelajaran' => 'required',
        ]);

        if ($validator->fails()) {
            return redirect()->back()
                ->withErrors($validator)
                ->withInput();
        }

        try {
            $mengajar = Mengajar::find($id);

            if (!$mengajar) {
                throw new \Exception('Mengajar tidak ditemukan. Silahkan coba kembali.');
            }

            DB::beginTransaction();

            $mengajar->id_kelas = $request->kelas;
            $mengajar->id_guru = $request->guru;
            $mengajar->id_mapel = $request->mata_pelajaran;
            $mengajar->save();

            DB::commit();

            return redirect()->back()->with('success', 'Data mengajar berhasil ditambahkan.');
        } catch (\Exception $e) {
            DB::rollback();
            return back()->withErrors(['error' => $e->getMessage()]);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $mengajar = Mengajar::findOrFail($id);
        $mengajar->delete();

        return redirect()->back()->with('success', 'Data mengajar berhasil dihapus.');
    }
}
