<?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\EventRepository;

class EventController extends Controller
{
    private $eventRepository;

    public function __construct(EventRepository $eventRepository)
    {
        $this->eventRepository = $eventRepository;
    }
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $data['title'] = 'Event Lists';
        $data['eventLists'] = $this->eventRepository->findAll();
        return view('admin::events.index', $data);
    }

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

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

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

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

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

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

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

    }

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

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

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

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