<?php

namespace App\Classes\Modules\Announcements\ControllersLogic;

use App\Classes\General\Abstracts\AbstractControllerLogic;

use App\Classes\Modules\Announcements\Services\FetchesAnnouncement;
use App\Classes\Modules\Announcements\Processors\AssignAnnouncementToSegmentProcessor;

use App\Http\Resources\AnnouncementResource;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class AssignAnnouncementToSegmentLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification(): array
    {
        return [
            'title' => 'Assign Announcement To Segment',
            'message' => 'You have successfully assigned Announcement to segment'
        ];
    }

    /** @var FetchesAnnouncement */
    private $fetchesAnnouncement;

    /** @var AssignAnnouncementToSegmentProcessor */
    private $assignAnnouncementToSegmentProcessor;

    /**
     * AssignAnnouncementToSegmentLogic constructor.
     * @param FetchesAnnouncement $fetchesAnnouncement
     * @param AssignAnnouncementToSegmentProcessor $assignAnnouncementToSegmentProcessor
     */
    public function __construct(FetchesAnnouncement $fetchesAnnouncement, AssignAnnouncementToSegmentProcessor $assignAnnouncementToSegmentProcessor)
    {
        $this->fetchesAnnouncement = $fetchesAnnouncement;
        $this->assignAnnouncementToSegmentProcessor = $assignAnnouncementToSegmentProcessor;
    }

    /**
     * @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
    {
        $announcement = $this->fetchesAnnouncement->execute(['id' => $request->route('id')]);

        $this->assignAnnouncementToSegmentProcessor->execute($announcement, $request->input('segment_id'));

        return $this->resourceResponse(new AnnouncementResource($announcement));
    }
}
