<?php

namespace App\Http\Controllers\Api;

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

class JadwalUjianController extends Controller
{
    public function index(Request $request)
    {
        $query = JadwalUjian::with('tahunAjaran')
            ->when($request->tahun_ajaran_id, fn($q) => $q->where('tahun_ajaran_id', $request->tahun_ajaran_id))
            ->when($request->jenis_ujian, fn($q) => $q->where('jenis_ujian', $request->jenis_ujian))
            ->orderByDesc('tanggal_mulai');

        $data = $request->boolean('all') ? $query->get() : $query->paginate(20);

        return response()->json(['success' => true, 'data' => $data]);
    }

    public function store(Request $request)
    {
        $request->validate([
            'tahun_ajaran_id' => 'required|exists:tahun_ajaran,id',
            'nama'            => 'required|string|max:255',
            'jenis_ujian'     => 'required|in:PAS,UM,PTS,PH',
            'tanggal_mulai'   => 'required|date',
            'tanggal_selesai' => 'required|date|after_or_equal:tanggal_mulai',
            'waktu_mulai'     => 'required|date_format:H:i',
            'waktu_selesai'   => 'required|date_format:H:i|after:waktu_mulai',
            'durasi_menit'    => 'required|integer|min:10|max:300',
            'keterangan'      => 'nullable|string',
        ]);

        $jadwal = JadwalUjian::create($request->only([
            'tahun_ajaran_id', 'nama', 'jenis_ujian',
            'tanggal_mulai', 'tanggal_selesai',
            'waktu_mulai', 'waktu_selesai',
            'durasi_menit', 'keterangan',
        ]));

        $jadwal->load('tahunAjaran');

        return response()->json([
            'success' => true,
            'message' => 'Jadwal ujian berhasil dibuat.',
            'data'    => $jadwal,
        ], 201);
    }

    public function show(JadwalUjian $jadwalUjian)
    {
        $jadwalUjian->load('tahunAjaran');
        return response()->json(['success' => true, 'data' => $jadwalUjian]);
    }

    public function update(Request $request, JadwalUjian $jadwalUjian)
    {
        $request->validate([
            'tahun_ajaran_id' => 'sometimes|exists:tahun_ajaran,id',
            'nama'            => 'sometimes|string|max:255',
            'jenis_ujian'     => 'sometimes|in:PAS,UM,PTS,PH',
            'tanggal_mulai'   => 'sometimes|date',
            'tanggal_selesai' => 'sometimes|date|after_or_equal:tanggal_mulai',
            'waktu_mulai'     => 'sometimes|date_format:H:i',
            'waktu_selesai'   => 'sometimes|date_format:H:i|after:waktu_mulai',
            'durasi_menit'    => 'sometimes|integer|min:10|max:300',
            'keterangan'      => 'nullable|string',
        ]);

        $jadwalUjian->update($request->only([
            'tahun_ajaran_id', 'nama', 'jenis_ujian',
            'tanggal_mulai', 'tanggal_selesai',
            'waktu_mulai', 'waktu_selesai',
            'durasi_menit', 'keterangan',
        ]));

        return response()->json([
            'success' => true,
            'message' => 'Jadwal ujian berhasil diperbarui.',
            'data'    => $jadwalUjian->fresh('tahunAjaran'),
        ]);
    }

    public function destroy(JadwalUjian $jadwalUjian)
    {
        $jadwalUjian->delete();
        return response()->json(['success' => true, 'message' => 'Jadwal ujian berhasil dihapus.']);
    }
}
