<?php

namespace Modules\Admin\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Modules\Admin\Repositories\HolidayRepository;

class HolidayController extends Controller
{
    private $holidayRepository;

    public function __construct(HolidayRepository $holidayRepository)
    {
        $this->holidayRepository = $holidayRepository;
    }
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $data['title'] = 'Holiday Lists';
        $data['holidayLists'] = $this->holidayRepository->findAll();
        return view('admin::holidays.index', $data);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        $data['title'] = 'Create Holiday';
        $data['editable'] = false;
        return view('admin::holidays.create', $data);
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request): RedirectResponse
    {
        try {
            $this->holidayRepository->create($request->all());
            toastr()->success('Holiday Created Successfully');

        } catch (\Throwable $th) {
            toastr()->error($th->getMessage());
        }
        return redirect()->route('holiday.index');
    }

    /**
     * Show the specified resource.
     */
    public function show($id)
    {
        return view('admin::holidays.show');
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit($id)
    {
        try {
            $data['title'] = 'Edit Holiday';
            $data['editable'] = true;
            $data['holiday'] = $this->holidayRepository->getHolidayById($id);

        } catch (\Throwable $th) {
            toastr()->error($th->getMessage());
        }

        return view('admin::holidays.edit', $data);

    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, $id): RedirectResponse
    {
        try {

            $this->holidayRepository->update($id, $request->all());
            toastr()->success('Holiday Updated Successfully');

        } catch (\Throwable $th) {
            toastr()->error($th->getMessage());
        }
        return redirect()->route('holiday.index');
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy($id)
    {
        try {
            $this->holidayRepository->delete($id);
            toastr()->success('Holiday Deleted Successfully');
        } catch (\Throwable $th) {
            toastr()->error($th->getMessage());
        }
        return redirect()->route('holiday.index');
    }
}