<?php

namespace Emixion\Calendar\Http\Controllers;

use App\Http\Controllers\PublicController as CMSPublicController;
use Emixion\Calendar\Interfaces\CalendarsRepositoryInterface;

class CalendarsPublicController extends CMSPublicController
{
    /**
     * Constructor.
     *
     * @param type \Emixion\Calendars\Interfaces\CalendarsRepositoryInterface $calendars
     *
     * @return type
     */
    public function __construct(CalendarsRepositoryInterface $calendars)
    {
        $this->repository = $calendars;
        parent::__construct();
    }

    /**
     * Show calendars's list.
     *
     * @param string $slug
     *
     * @return response
     */
    protected function index()
    {
        $calendars = $this->repository->scopeQuery(function($query){
            return $query->orderBy('id','DESC');
        })->paginate();

        return $this->theme->of('calendar::public.calendars.index', compact('calendars'))->render();
    }

    /**
     * Show calendars.
     *
     * @param string $slug
     *
     * @return response
     */
    protected function show($slug)
    {
        $calendars = $this->repository->scopeQuery(function($query) use ($slug) {
            return $query->orderBy('id','DESC')
                         ->where('slug', $slug);
        })->first(['*']);

        return $this->theme->of('calendar::public.calendars.show', compact('calendars'))->render();
    }
}