<?php

namespace App\Http\Controllers\Manager;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;
use App\Models\Holiday;
use Carbon\Carbon;

class HolidayController extends Controller
{


    public function index()
    {
        $holidays = Holiday::all();
        return view('manager/holiday/index', compact('holidays'));
    }


    public function addpage(){
        return view('manager/holiday/add');
    }

    public function store(Request $request)
    {
        $request->validate([
            'holiday_name' => 'required|string|max:255',
            'holiday_date' => 'required|date',
            'holiday_details' => 'nullable|string',
        ]);

        Holiday::create($request->all());

        return redirect()->route('holiday.add')->with('success', 'Holiday added successfully.');
    }

    public function thisMonth()
    {
        $currentMonth = Carbon::now()->month;
        $holidays = Holiday::whereMonth('holiday_date', $currentMonth)->get();

        return view('manager/holiday/thismonth', compact('holidays'));
    }



    public function edit(Holiday $holiday)
    {
        return view('manager/holiday/edit', compact('holiday'));
    }

    public function update(Request $request, Holiday $holiday)
    {
        $validatedData = $request->validate([
            'holiday_name' => 'required',
            'holiday_date' => 'required|date',
            'holiday_details' => 'nullable',
        ]);

        $holiday->update($validatedData);

        return redirect()->route('holidays.all')->with('success', 'Holiday updated successfully.');
    }

    public function destroy(Holiday $holiday)
    {
        $holiday->delete();

        return redirect()->route('holidays.all')->with('success', 'Holiday deleted successfully.');
    }
}
