<?php

namespace App\Classes\Modules\Announcements\ControllersLogic;

use App\Classes\General\Abstracts\AbstractControllerLogic;

use App\Classes\Modules\Announcements\DataTransferObjects\AnnouncementObject;
use App\Classes\Modules\Announcements\Standards\Rules\CanUpdateAnnouncement;
use App\Classes\Modules\Announcements\Services\FetchesAnnouncement;
use App\Classes\Modules\Announcements\Services\UpdatesAnnouncement;

use App\Http\Resources\AnnouncementResource;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;


class UpdateAnnouncementLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification(): array
    {
        return [
            'title' => 'Update Announcement',
            'message' => 'You have successfully updated the Announcement'
        ];
    }

    /** @var CanUpdateAnnouncement */
    private $canUpdateAnnouncement;

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

    /** @var UpdatesAnnouncement */
    private $updatesAnnouncement;

    /**
     * UpdateAnnouncementLogic constructor.
     * @param CanUpdateAnnouncement $canUpdateAnnouncement
     * @param FetchesAnnouncement $fetchesAnnouncement
     * @param UpdatesAnnouncement $updatesAnnouncement     
     */
    public function __construct(
        CanUpdateAnnouncement $canUpdateAnnouncement,
        FetchesAnnouncement $fetchesAnnouncement,
        UpdatesAnnouncement $updatesAnnouncement        
    ) {
        $this->canUpdateAnnouncement = $canUpdateAnnouncement;
        $this->fetchesAnnouncement = $fetchesAnnouncement;
        $this->updatesAnnouncement = $updatesAnnouncement;        
    }

    /**
     * @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
    {
        $annoucement_object = new AnnouncementObject(
            $request->input('title'),
            $request->input('description'),
            Carbon::createFromFormat('d-m-Y', $request->input('starting_on'))->format('Y-m-d H:i:s'),
            Carbon::createFromFormat('d-m-Y', $request->input('ending_on'))->format('Y-m-d H:i:s')
        );

        $this->canUpdateAnnouncement->passes($annoucement_object);

        $annoucement = $this->fetchesAnnouncement->execute(['id' => $request->route('id')]);

        $announcement_query = $this->updatesAnnouncement->execute($annoucement, $annoucement_object);

        //$announcement_query->segments()->sync($request->input('segment_id'));

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