<?php

namespace App\Classes\Modules\PackingLists\ControllersLogic;

use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\PackingLists\Services\FetchesPackingList;
use App\Classes\Modules\Schedules\Services\CreatesSchedule;
use App\Classes\Modules\Schedules\Services\UpdatesScheduleStatus;
use App\Classes\Modules\Schedules\DataTransferObjects\ScheduleObject;
use App\Classes\Modules\Transports\DataTransferObjects\TransportObject;
use App\Classes\Modules\Transports\Services\CreatesTransport;
use App\Classes\ValueObjects\Constants\ApprovalStatus;

use App\Classes\ValueObjects\Constants\TransportType;
use App\Http\Resources\PackingListResource;
use App\Models\Transport;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Carbon\Carbon;

class ReschedulePackingListLogic extends AbstractControllerLogic
{


    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Reschedule Packing List',
            'message' => 'You have successfully Reschedule Packing List'
        ];
    }

    /** @var FetchesPackingList */
    private $fetchesPackingList;

    /** @var UpdatesScheduleStatus */
    private $updatesScheduleStatus;

    /** @var CreatesTransport */
    private $createsTransport;

    /** @var CreatesSchedule */
    private $createsSchedule;

    /**
     * ReschedulePackingListLogic constructor.
     * @param FetchesPackingList $fetchesPackingList
     * @param UpdatesScheduleStatus $updatesScheduleStatus
     * @param CreatesTransport $createsTransport
     * @param CreatesSchedule $createsSchedule
     */
    public function __construct(FetchesPackingList $fetchesPackingList, UpdatesScheduleStatus $updatesScheduleStatus, CreatesTransport $createsTransport, CreatesSchedule $createsSchedule)
    {
        $this->fetchesPackingList = $fetchesPackingList;
        $this->updatesScheduleStatus = $updatesScheduleStatus;
        $this->createsTransport = $createsTransport;
        $this->createsSchedule = $createsSchedule;
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws \App\Classes\Exceptions\MalformedRequestException
     */
    public function logic(Request $request) : JsonResponse
    {
        $packing_list = $this->fetchesPackingList->execute(['id' => $request->route('id')]);

        $transport = $packing_list->transports()->first();

        if(!$transport) {
            $object = new TransportObject(TransportType::LAND, null, '', Carbon::parse($request->input('etd')), Carbon::parse($request->input('eta')),ApprovalStatus::APPROVED);
            /** @var Transport $transport */
            $transport = $this->createsTransport->execute($object, $packing_list);
        }

        $transport->schedules()->update([
            'status' => ApprovalStatus::REJECTED
        ]);

        $scheduleObject = new ScheduleObject(
            Carbon::parse($request->input('eta')), 
            Carbon::parse($request->input('etd')), 
            ApprovalStatus::APPROVED
        );
        
        $schedule = $this->createsSchedule->execute($transport, $scheduleObject);

        return $this->resourceResponse(new PackingListResource($packing_list));
    }

}
