<?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;

class UploadPaymentVerificationDocumentLogic extends AbstractControllerLogic
{

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

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

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

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

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

    /**
     * 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)
    {
        $this->fetchesTransaction = $fetchesTransaction;
        $this->createsDocument = $createsDocument;
        $this->createsFile = $createsFile;
        $this->updatesTransactionStatus = $updatesTransactionStatus;
    }

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

        $object = new DocumentObject(
            DocumentType::CUSTOMER_PAYMENT_PROOF, 
            $request->input('files'), 
            '', 
            ApprovalStatus::PENDING_VERIFICATION, 
            'payments'
        );

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

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

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

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