<?php

namespace App\Http\Controllers\Admin;

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

class HariLiburController extends Controller
{
    /**
     * ================================
     * TAMBAH DATA HARI LIBUR
     * ================================
     */
    public function store(Request $request)
    {
        // VALIDASI INPUT
        $request->validate([
            'tanggal_mulai'   => 'required|date',
            'tanggal_selesai' => 'required|date|after_or_equal:tanggal_mulai',
            'keterangan'      => 'required|string|max:255',
            'tipe'            => 'required|in:nasional,semester,agenda_sekolah',
        ]);

        try {
            // SIMPAN DATA
            HariLibur::create([
                'tanggal_mulai'   => $request->tanggal_mulai,
                'tanggal_selesai' => $request->tanggal_selesai,
                'keterangan'      => $request->keterangan,
                'tipe'            => $request->tipe,
            ]);

            return back()->with(
                'success',
                'Hari libur "' . $request->keterangan . '" berhasil ditambahkan!'
            );
        } catch (\Exception $e) {
            return back()->with(
                'error',
                'Gagal menyimpan data: ' . $e->getMessage()
            );
        }
    }

    /**
     * ================================
     * UPDATE DATA HARI LIBUR
     * ================================
     */
    public function update(Request $request, $id)
    {
        // VALIDASI INPUT
        $request->validate([
            'tanggal_mulai'   => 'required|date',
            'tanggal_selesai' => 'required|date|after_or_equal:tanggal_mulai',
            'keterangan'      => 'required|string|max:255',
            'tipe'            => 'required|in:nasional,semester,agenda_sekolah',
        ]);

        try {
            $libur = HariLibur::findOrFail($id);

            // UPDATE DATA
            $libur->update([
                'tanggal_mulai'   => $request->tanggal_mulai,
                'tanggal_selesai' => $request->tanggal_selesai,
                'keterangan'      => $request->keterangan,
                'tipe'            => $request->tipe,
            ]);

            return back()->with('success', 'Data hari libur berhasil diperbarui!');
        } catch (\Exception $e) {
            return back()->with(
                'error',
                'Gagal memperbarui data: ' . $e->getMessage()
            );
        }
    }

    /**
     * ================================
     * HAPUS DATA HARI LIBUR
     * ================================
     */
    public function destroy($id)
    {
        try {
            $libur = HariLibur::findOrFail($id);

            $libur->delete();

            return back()->with('success', 'Hari libur berhasil dihapus');
        } catch (\Exception $e) {
            return back()->with(
                'error',
                'Gagal menghapus data: ' . $e->getMessage()
            );
        }
    }
}
