<?php

namespace App\Classes\Modules\Bookings\ControllersLogic;


use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Bookings\Services\FetchesBooking;
use App\Classes\ValueObjects\Constants\DocumentType;
use App\Classes\Modules\Documents\Standards\Rules\CanDeleteDocument;
use App\Classes\Modules\Documents\Services\DeletesDocument;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class DeletePurchaseOrderPdfLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Purchase Order PDF Deleted',
            'message' => 'You have successfully deleted the Purchase order PDF'
        ];
    }

    /** @var FetchesBooking */
    private $fetchesBooking;

    /** @var DeletesDocument */
    private $deletesDocument;

    /** @var CanDeleteDocument */
    private $canDeleteDocument;


    /**
     * DeletePurchaseOrderPdfLogic constructor.
     * @param FetchesBooking $fetchesBooking
     * @param DeletesDocument $deletesDocument
     * @param CanDeleteDocument $canDeleteDocument
     */
    public function __construct(fetchesBooking $fetchesBooking, DeletesDocument $deletesDocument, CanDeleteDocument $canDeleteDocument)
    {
        $this->fetchesBooking = $fetchesBooking;
        $this->deletesDocument = $deletesDocument;
        $this->canDeleteDocument = $canDeleteDocument;
    }


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

        $document = $booking->documents()->where('document_type', DocumentType::ECOMMERCE_PURCHASE_ORDER)->first();

        $this->canDeleteDocument->passes();

        $this->deletesDocument->execute($document);

        return $this->response([]);
    }

}
