<?php

namespace App\Classes\Modules\Transactions\ControllersLogic;


use App\Classes\General\Abstracts\AbstractControllerLogic;
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\CalculatesBillGroupPaymentAmount;
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\DocumentType;
use App\Models\Document;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class CreateBillGroupPaymentProofDocumentLogic 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 CalculatesBillGroupPaymentAmount */
    private $calculatesBillGroupPaymentAmount;

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

    /**
     * @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::BILL_GROUP_PAYMENT_PROOF, $request->input('files'), '', ApprovalStatus::PENDING_VERIFICATION, 'bill_group_payments');

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

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

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

        $billGroup = $transaction->owner;
        $billGroupPayment = $this->calculatesBillGroupPaymentAmount->execute($billGroup);

        if ($billGroupPayment['outstanding_amount'] <= 0 && $billGroup->transactions()->where('status', ApprovalStatus::PENDING_SUBMISSION)->count() === 0) {
            $billGroup->status = ApprovalStatus::PENDING_VERIFICATION;
            $billGroup->save();
        }

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

}