<?php

namespace App\Http\Controllers;

use App\Models\Absensi;
use App\Models\Kelas;
use App\Models\Santri;
use Illuminate\Http\Request;

class AbsensiController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index(Request $request)
    {
        $kelasList = Kelas::all();
        $kelasId = $request->get('kelas_id');
        $tanggal = $request->get('tanggal', date('Y-m-d'));
        $santris = collect();
        $absensis = collect();
        if ($kelasId) {
            $santris = Santri::where('kelas_id', $kelasId)->get();
            $absensis = Absensi::where('kelas_id', $kelasId)->where('tanggal', $tanggal)->get()->keyBy('santri_id');
        }
        $view = auth()->user()->isGuru() ? 'guru.absensis.index' : 'absensis.index';
        return view($view, compact('kelasList', 'kelasId', 'tanggal', 'santris', 'absensis'));
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create(Request $request)
    {
        $kelasId = $request->get('kelas_id');
        $tanggal = $request->get('tanggal', date('Y-m-d'));
        $kelas = Kelas::all();
        $santris = collect();
        if ($kelasId) {
            $santris = Santri::where('kelas_id', $kelasId)->get();
        }
        $view = auth()->user()->isGuru() ? 'guru.absensis.create' : 'absensis.create';
        return view($view, compact('kelas', 'santris', 'tanggal', 'kelasId'));
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $request->validate([
            'kelas_id' => 'required|exists:kelas,id',
            'tanggal' => 'required|date',
            'absensi' => 'required|array',
            'absensi.*.santri_id' => 'required|exists:santris,id',
            'absensi.*.status' => 'required|in:hadir,izin,sakit,alfa',
            'absensi.*.keterangan' => 'nullable|string',
        ]);
        foreach ($request->absensi as $absen) {
            \App\Models\Absensi::updateOrCreate(
                [
                    'santri_id' => $absen['santri_id'],
                    'kelas_id' => $request->kelas_id,
                    'tanggal' => $request->tanggal,
                ],
                [
                    'status' => $absen['status'],
                    'keterangan' => $absen['keterangan'] ?? null,
                ]
            );
        }
        $route = auth()->user()->isGuru() ? 'guru.absensis.index' : 'absensis.index';
        return redirect()->route($route, ['kelas_id' => $request->kelas_id, 'tanggal' => $request->tanggal])
            ->with('success', 'Absensi berhasil disimpan');
    }

    /**
     * Display the specified resource.
     */
    public function show($santri_id)
    {
        $santri = \App\Models\Santri::findOrFail($santri_id);
        $absensis = \App\Models\Absensi::where('santri_id', $santri_id)->orderBy('tanggal', 'desc')->get();
        $view = auth()->user()->isGuru() ? 'guru.absensis.show' : 'absensis.show';
        return view($view, compact('santri', 'absensis'));
    }

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

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, Absensi $absensi)
    {
        $request->validate([
            'status' => 'required|in:hadir,izin,sakit,alfa',
            'keterangan' => 'nullable|string',
        ]);
        $absensi->update([
            'status' => $request->status,
            'keterangan' => $request->keterangan,
        ]);
        $route = auth()->user()->isGuru() ? 'guru.absensis.index' : 'absensis.index';
        return redirect()->route($route, [
            'kelas_id' => $absensi->kelas_id,
            'tanggal' => $absensi->tanggal
        ])->with('success', 'Absensi berhasil diupdate');
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(Absensi $absensi)
    {
        $kelas_id = $absensi->kelas_id;
        $tanggal = $absensi->tanggal;
        $absensi->delete();
        $route = auth()->user()->isGuru() ? 'guru.absensis.index' : 'absensis.index';
        return redirect()->route($route, [
            'kelas_id' => $kelas_id,
            'tanggal' => $tanggal
        ])->with('success', 'Absensi berhasil dihapus');
    }
}

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use App\Models\Absensi;
use Illuminate\Http\Request;

class AbsensiApiController extends Controller
{
    public function index(Request $request)
    {
        $user = $request->user();
        $absensi = Absensi::where('user_id', $user->id)->get();
        return response()->json($absensi);
    }
}
