<?php

namespace App\Http\Controllers;

use App\Models\LeaveRequest;
use Illuminate\Http\Request;

class LeaveRequestController extends Controller
{
    public function store(Request $request)
    {
        LeaveRequest::create($this->validated($request));
        return back()->with('success', 'Pengajuan berhasil ditambahkan.');
    }

    public function update(Request $request, LeaveRequest $leave)
    {
        $leave->update($this->validated($request));
        return back()->with('success', 'Pengajuan berhasil diperbarui.');
    }

    public function destroy(LeaveRequest $leave)
    {
        $leave->delete();
        return back()->with('success', 'Pengajuan berhasil dihapus.');
    }

    private function validated(Request $request): array
    {
        return $request->validate([
            'employee_id' => ['required', 'exists:employees,id'],
            'type' => ['required', 'in:Cuti,Izin,Sakit,Libur Pengganti'],
            'start_date' => ['required', 'date'],
            'end_date' => ['required', 'date', 'after_or_equal:start_date'],
            'status' => ['required', 'in:Menunggu,Disetujui,Ditolak'],
            'notes' => ['nullable', 'string', 'max:1000'],
        ]);
    }
}
