<?php

namespace App\Classes\Modules\Announcements\ControllersLogic;

use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Announcements\Services\ListsAnnouncements;
use App\Classes\Modules\Announcements\Standards\Rules\CanListAnnouncements;
use App\Http\Resources\AnnouncementResource;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class ListAnnouncementsLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification(): array
    {
        return [
            'title' => 'Retrieve Announcements',
            'message' => 'You have successfully retrieved a list of Announcements'
        ];
    }

    /** @var CanListAnnouncements */
    private $canListAnnouncements;

    /** @var ListsAnnouncements */
    private $listsAnnouncements;

    /**
     * ListAnnouncementsLogic constructor.
     * @param CanListAnnouncements $canListAnnouncements
     * @param ListsAnnouncements $listsAnnouncements
     */
    public function __construct(CanListAnnouncements $canListAnnouncements, ListsAnnouncements $listsAnnouncements)
    {
        $this->canListAnnouncements = $canListAnnouncements;
        $this->listsAnnouncements = $listsAnnouncements;
    }


    /**
     * @param Request $request
     * @return JsonResponse
     * @throws \App\Classes\Exceptions\AccessForbiddenException
     * @throws \App\Classes\Exceptions\MalformedRequestException
     * @throws \App\Classes\Exceptions\RequestValidationException
     */
    public function logic(Request $request): JsonResponse
    {

        $this->canListAnnouncements->passes();

        $annoucements = $this->listsAnnouncements->execute($this->listsAnnouncements->deserializeFilters($request->input('filters')));

        return $this->collectionResponse(AnnouncementResource::collection($annoucements));
    }
}
