<?php

namespace App\Classes\Modules\Transactions\ControllersLogic;

use App\Classes\General\Abstracts\AbstractControllerLogic;

use App\Classes\Modules\Documents\Services\ApprovesDocument;
use App\Classes\Modules\Documents\Services\RejectsDocument;
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 Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class ApproveBillGroupPaymentVerificationLogic extends AbstractControllerLogic
{
    /**
     * ApproveBillGroupPaymentVerificationLogic constructor.
     * @param FetchesTransaction $fetchesTransaction
     * @param UpdatesTransactionStatus $updatesTransactionStatus
     * @param ApprovesDocument $approvesDocument
     * @param RejectsDocument $rejectsDocument
     * @param CalculatesBillGroupPaymentAmount $calculatesBillGroupPaymentAmount
     */
    public function __construct(FetchesTransaction $fetchesTransaction, UpdatesTransactionStatus $updatesTransactionStatus, ApprovesDocument $approvesDocument, RejectsDocument $rejectsDocument, CalculatesBillGroupPaymentAmount $calculatesBillGroupPaymentAmount)
    {
        $this->fetchesTransaction = $fetchesTransaction;
        $this->updatesTransactionStatus = $updatesTransactionStatus;
        $this->approvesDocument = $approvesDocument;
        $this->rejectsDocument = $rejectsDocument;
        $this->calculatesBillGroupPaymentAmount = $calculatesBillGroupPaymentAmount;
    }

    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Payment Status',
            'message' => 'You have successfully updated the payment status'
        ];
    }

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

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

    /** @var ApprovesDocument */
    private $approvesDocument;

    /** @var RejectsDocument */
    private $rejectsDocument;

    /** @var CalculatesBillGroupPaymentAmount */
    private $calculatesBillGroupPaymentAmount;

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws \App\Classes\Exceptions\MalformedRequestException
     */
    public function logic(Request $request) : JsonResponse
    {
        $status = $request->route('status');

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

        $status === 'approve' ? $this->approvesDocument->execute($transaction->documents()->first()) : $this->rejectsDocument->execute($transaction->documents()->first());

        $this->updatesTransactionStatus->execute($transaction, $status === 'approve' ? ApprovalStatus::APPROVED : ApprovalStatus::REJECTED);

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

        if ($status === 'reject') {
            $billGroup->status = ApprovalStatus::PENDING_SUBMISSION;
            $billGroup->save();
        } else {
            if ($billGroupPayment['outstanding_amount'] <= 0 && $billGroupPayment['floating_amount'] <= 0) {
                $billGroup->status = ApprovalStatus::APPROVED;
                $billGroup->save();
            }
        }

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

}