<?php

namespace App\Classes\Modules\Transactions\ControllersLogic;


use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Companies\Services\FetchesCompany;
use App\Classes\Modules\Companies\Services\UpdatesCompanyStatus;
use App\Classes\Modules\Documents\DataTransferObjects\DocumentObject;
use App\Classes\Modules\Documents\Services\CreatesDocument;
use App\Classes\Modules\Documents\Services\CreatesFiles;
use App\Classes\Modules\Transactions\Services\FetchesTransaction;
use App\Classes\Modules\Transactions\Services\UpdatesTransactionStatus;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\CompanyType;
use App\Classes\ValueObjects\Constants\DocumentType;
use App\Models\Company;
use App\Models\Document;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

use App\Classes\Modules\Transactions\Processors\CreateInvoiceTransactionProcessor;


class CreatePaymentProofDocumentLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Payment Proof Document',
            'message' => 'You have successfully submitted your payment proof document'
        ];
    }

    /** @var FetchesTransaction */
    private $fetchesTransaction;

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

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

    /** @var UpdatesTransactionStatus */
    private $updatesTransactionStatus;

    /** @var CreateInvoiceTransactionProcessor */
    private $createInvoiceTransactionProcessor;

    /**
     * CreatePaymentVerificationDocumentLogic constructor.
     * @param FetchesTransaction $fetchesTransaction
     * @param CreatesDocument $createsDocument
     * @param CreatesFiles $createsFile
     * @param UpdatesTransactionStatus $updatesTransactionStatus
     */
    public function __construct(FetchesTransaction $fetchesTransaction, CreatesDocument $createsDocument, CreatesFiles $createsFile, UpdatesTransactionStatus $updatesTransactionStatus, CreateInvoiceTransactionProcessor $createInvoiceTransactionProcessor)
    {
        $this->fetchesTransaction = $fetchesTransaction;
        $this->createsDocument = $createsDocument;
        $this->createsFile = $createsFile;
        $this->updatesTransactionStatus = $updatesTransactionStatus;
        $this->createInvoiceTransactionProcessor = $createInvoiceTransactionProcessor;
    }

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

        $transaction = $this->fetchesTransaction->execute(['id' => $request->route('id')]);

        $object = new DocumentObject(DocumentType::CURRENCY_VENDOR_PAYMENT_PROOF, $request->input('files'), '', ApprovalStatus::APPROVED, 'china_bank_slip');

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

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

        $this->updatesTransactionStatus->execute($transaction, ApprovalStatus::COMPLETED);

        $this->createInvoiceTransactionProcessor->execute($transaction->booking);

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

}