<?php

namespace App\Classes\Modules\Bookings\ControllersLogic;


use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Bookings\Processors\CreatePurchaseOrderFor1688OrderProcessor;
use App\Classes\Modules\Bookings\Services\FetchesBooking;
use App\Classes\Modules\Documents\DataTransferObjects\DocumentObject;
use App\Classes\Modules\Documents\Services\ApprovesDocument;
use App\Classes\Modules\Documents\Services\CreatesDocument;
use App\Classes\Modules\Documents\Services\CreatesFiles;
use App\Classes\Modules\Documents\Services\FetchesDocument;
use App\Classes\Modules\Documents\Services\RejectsDocument;
use App\Classes\Modules\Transactions\DataTransferObjects\TransactionObject;
use App\Classes\Modules\Transactions\Processors\CreateInvoiceTransactionProcessor;
use App\Classes\Modules\Transactions\Processors\CreatePurchaseOrderTransactionProcessor;
use App\Classes\Modules\Transactions\Services\FetchesTransaction;
use App\Classes\Modules\Transactions\Services\GeneratesTransactionBillNumber;
use App\Classes\Modules\Transactions\Services\UpdatesTransactionStatus;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\DocumentType;
use App\Classes\ValueObjects\Constants\PaymentMethodType;
use App\Classes\ValueObjects\Constants\TransactionType;
use App\Models\Document;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class UploadPurchaseOrderLogic extends AbstractControllerLogic
{

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

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

    /** @var CreatesDocument */
    private $createsDocument;

    /** @var CreatesFiles */
    private $createsFile;

    /** @var CreatePurchaseOrderFor1688OrderProcessor */
    private $createPurchaseOrderFor1688OrderProcessor;


    /**
     * UploadPurchaseOrderLogic constructor.
     * @param FetchesBooking $fetchesBooking
     * @param CreatesDocument $createsDocument
     * @param CreatesFiles $createsFile
     * @param CreatePurchaseOrderFor1688OrderProcessor $createPurchaseOrderFor1688OrderProcessor
     */
    public function __construct(FetchesBooking $fetchesBooking, CreatesDocument $createsDocument, CreatesFiles $createsFile, CreatePurchaseOrderFor1688OrderProcessor $createPurchaseOrderFor1688OrderProcessor)
    {
        $this->fetchesBooking = $fetchesBooking;
        $this->createsDocument = $createsDocument;
        $this->createsFile = $createsFile;
        $this->createPurchaseOrderFor1688OrderProcessor = $createPurchaseOrderFor1688OrderProcessor;
    }

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

        $booking = $this->fetchesBooking->execute(['id' => $request->route('id')]);

        $object = new DocumentObject(DocumentType::ECOMMERCE_PURCHASE_ORDER, $request->input('files'), '', ApprovalStatus::APPROVED, '1688_purchase_orders');

        /** @var Document $document */
        $document = $this->createsDocument->execute($booking, $object);

        $this->createsFile->execute($document, $object);

        if(!in_array($booking->company->id, [199, 510])){
            $this->createPurchaseOrderFor1688OrderProcessor->execute($booking);
        }

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

}
