<?php

namespace App\Classes\Modules\Orders\ControllersLogic;


use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Orders\Services\CancelsOrder;
use App\Classes\Modules\Orders\Services\FetchesOrder;
use App\Classes\Modules\Orders\Services\FetchOwnerCompanyConnection;
use App\Classes\Modules\Orders\Services\UpdateOwnerReference;
use App\Http\Resources\OrderResource;
use App\Http\Resources\PackingListResource;
use App\Models\CompanyConnection;
use App\Models\Order;
use App\Models\PackingList;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class ChangeOrderOwnerLogic extends AbstractControllerLogic
{
    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Changed order owner',
            'message' => 'You have successfully changed the owner'
        ];
    }

    /** @var Marking $marking */
    private $marking;

    /** @param UpdateOwnerReference  $updateOwnerReference*/
    private $updateOwnerReference;
    private $fetchOwnerCompanyConnection;

    public function __construct(UpdateOwnerReference $updateOwnerReference, FetchOwnerCompanyConnection $fetchOwnerCompanyConnection)
    {
        $this->updateOwnerReference = $updateOwnerReference;
        $this->fetchOwnerCompanyConnection = $fetchOwnerCompanyConnection;
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws \App\Classes\Exceptions\MalformedRequestException
     */
    protected function logic(Request $request): JsonResponse
    {
        $reference = $request->route('reference');

        $packing = PackingList::where('reference', $reference)->get();

        $companyConnection = $this->fetchOwnerCompanyConnection->execute($request->marking)->invitee->orders;

        $order = $this->updateOwnerReference->execute($reference, $companyConnection);

        return response()->json($packing,200);
     //   return $this->resourceResponse(new PackingListResource($packing));

    }

}
