<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\NilaiSantri;
use App\Models\Santri;
use App\Models\Kelas;
use App\Models\MataPelajaran;
use App\Models\Semester;

class NilaiSantriController extends Controller
{
    public function index(Request $request)
    {
        $kelasList = Kelas::all();
        $mapelList = MataPelajaran::all();

        $kelasId = $request->kelas_id;
        $mapelId = $request->mapel_id;
        $tanggal = $request->tanggal ?? now()->toDateString();

        $santris = collect();
        $nilaiSantris = collect();

        if ($kelasId && $mapelId) {
            $santris = Santri::where('kelas_id', $kelasId)->get();
            $nilaiSantris = NilaiSantri::where('kelas_id', $kelasId)
                ->where('mapel_id', $mapelId)
                ->where('tanggal', $tanggal)
                ->get()
                ->keyBy('santri_id');
        }

        $view = auth()->user()->isGuru() ? 'guru.nilai_santris.index' : 'nilai_santris.index';

        return view($view, compact(
            'kelasList',
            'mapelList',
            'kelasId',
            'mapelId',
            'tanggal',
            'santris',
            'nilaiSantris'
        ));
    }

    public function create(Request $request)
    {
        $kelas = Kelas::all();
        $mapelList = MataPelajaran::all();
        $semesters = Semester::all();

        $kelasId = $request->kelas_id;
        $mapelId = $request->mapel_id;
        $tanggal = $request->tanggal ?? now()->toDateString();
        $santris = $kelasId ? Santri::where('kelas_id', $kelasId)->get() : collect();

        $view = auth()->user()->isGuru() ? 'guru.nilai_santris.create' : 'nilai_santris.create';

        return view($view, compact(
            'kelas',
            'mapelList',
            'semesters',
            'santris',
            'kelasId',
            'mapelId',
            'tanggal'
        ));
    }

    public function store(Request $request)
    {
        $request->validate([
            'kelas_id' => 'required|exists:kelas,id',
            'mapel_id' => 'required|exists:mata_pelajarans,id',
            'semester_id' => 'required|exists:semesters,id',
            'jenis_nilai' => 'required|string',
            'tanggal' => 'required|date',
            'nilai' => 'required|array',
            'nilai.*.santri_id' => 'required|exists:santris,id',
            'nilai.*.nilai' => 'required|numeric|min:0|max:100',
            'nilai.*.keterangan' => 'nullable|string',
        ]);

        foreach ($request->nilai as $n) {
            NilaiSantri::updateOrCreate(
                [
                    'santri_id' => $n['santri_id'],
                    'kelas_id' => $request->kelas_id,
                    'mapel_id' => $request->mapel_id,
                    'tanggal' => $request->tanggal,
                ],
                [
                    'semester_id' => $request->semester_id,
                    'jenis_nilai' => $request->jenis_nilai,
                    'nilai' => $n['nilai'],
                    'keterangan' => $n['keterangan'] ?? null,
                ]
            );
        }

        $route = auth()->user()->isGuru() ? 'guru.nilai_santris.index' : 'nilai_santris.index';

        return redirect()->route($route, [
            'kelas_id' => $request->kelas_id,
            'mapel_id' => $request->mapel_id,
            'tanggal' => $request->tanggal,
        ])->with('success', 'Nilai santri berhasil disimpan.');
    }

    public function edit(NilaiSantri $nilaiSantri)
    {
        $kelas = Kelas::all();
        $mapelList = MataPelajaran::all();
        $semesters = Semester::all();
        $santri = $nilaiSantri->santri;

        $view = auth()->user()->isGuru() ? 'guru.nilai_santris.edit' : 'nilai_santris.edit';

        return view($view, compact(
            'nilaiSantri',
            'kelas',
            'mapelList',
            'semesters',
            'santri'
        ));
    }

    public function update(Request $request, NilaiSantri $nilaiSantri)
    {
        $request->validate([
            'nilai' => 'required|numeric|min:0|max:100',
            'keterangan' => 'nullable|string',
            'semester_id' => 'required|exists:semesters,id',
            'jenis_nilai' => 'required|string',
        ]);

        $nilaiSantri->update([
            'nilai' => $request->nilai,
            'keterangan' => $request->keterangan,
            'semester_id' => $request->semester_id,
            'jenis_nilai' => $request->jenis_nilai,
        ]);

        $route = auth()->user()->isGuru() ? 'guru.nilai_santris.index' : 'nilai_santris.index';

        return redirect()->route($route, [
            'kelas_id' => $nilaiSantri->kelas_id,
            'mapel_id' => $nilaiSantri->mapel_id,
            'tanggal' => $nilaiSantri->tanggal,
        ])->with('success', 'Nilai santri berhasil diperbarui.');
    }

    public function destroy(NilaiSantri $nilaiSantri)
    {
        $kelas_id = $nilaiSantri->kelas_id;
        $mapel_id = $nilaiSantri->mapel_id;
        $tanggal = $nilaiSantri->tanggal;

        $nilaiSantri->delete();

        $route = auth()->user()->isGuru() ? 'guru.nilai_santris.index' : 'nilai_santris.index';

        return redirect()->route($route, [
            'kelas_id' => $kelas_id,
            'mapel_id' => $mapel_id,
            'tanggal' => $tanggal,
        ])->with('success', 'Nilai santri berhasil dihapus.');
    }
}
