<?php

namespace App\Classes\Modules\Announcements\ControllersLogic;

use App\Classes\General\Abstracts\AbstractControllerLogic;

use App\Classes\Modules\Announcements\Services\FetchesAnnouncement;
use App\Classes\Modules\Segments\Services\FetchesSegment;
use App\Classes\Modules\Announcements\Services\RemovesAnnouncementFromSegment;

use App\Http\Resources\AnnouncementResource;

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

class RemoveSegmentFromAnnouncementLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification(): array
    {
        return [
            'title' => 'Detach Announcement From Segment',
            'message' => 'You have successfully detached Announcement from segment'
        ];
    }

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

    /** @var FetchesSegment */
    private $fetchesSegment;

    /** @var RemovesAnnouncementFromSegment */
    private $removesAnnouncementFromSegment;

    /**
     * RemoveSegmentFromAnnouncementLogic constructor.
     * @param FetchesAnnouncement $fetchesAnnnouncement
     * @param FetchesSegment $fetchesSegment
     * @param RemovesAnnouncementFromSegment $removesAnnouncementFromSegment
     */
    public function __construct(FetchesAnnouncement $fetchesAnnnouncement, FetchesSegment $fetchesSegment, RemovesAnnouncementFromSegment $removesAnnouncementFromSegment)
    {
        $this->fetchesAnnnouncement = $fetchesAnnnouncement;
        $this->fetchesSegment = $fetchesSegment;
        $this->removesAnnouncementFromSegment = $removesAnnouncementFromSegment;
    }

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

        $announcement = $this->fetchesAnnnouncement->execute(['id' => $request->route('id')]);

        $segment = $this->fetchesSegment->execute(['id' => $request->route('segment_id')]);

        $this->removesAnnouncementFromSegment->execute($announcement, $segment);

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