<?php

namespace App\Classes\Modules\Accounting\ControllersLogic;

use App\Classes\Exceptions\MalformedRequestException;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Accounting\Services\ListsBankStatementTransactions;
use App\Http\Resources\BankStatementTransactionResource;
use App\Classes\Modules\Accounting\Services\UpdatesBankStatementTransactionOwnerStatus;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Classes\Modules\Transactions\Services\UpdatesTransactionStatus;
use App\Classes\ValueObjects\Constants\StatementTransactionOwnerType;

class GroupApproveStatementTransactionLogic extends AbstractControllerLogic
{


    /**
     * @return array
     */
    protected function notification(): array
    {
        return [
            'title' => 'Group Approve Statement Transaction',
            'message' => 'You have successfully approveed a group of Statement Transation'
        ];
    }

    /** @var ListsBankStatementTransactions */
    private $listsBankStatementTransactions;

    /** @var UpdatesBankStatementTransactionOwnerStatus */
    private $updatesBankStatementTransactionOwnerStatus;

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

    /**
     * @param ListsBankStatementTransactions $listsBankStatementTransactions
     * @param UpdatesBankStatementTransactionOwnerStatus $updatesBankStatementTransactionOwnerStatus
     * @param UpdatesTransactionStatus $updatesTransactionStatus
     */
    public function __construct(ListsBankStatementTransactions $listsBankStatementTransactions, UpdatesBankStatementTransactionOwnerStatus $updatesBankStatementTransactionOwnerStatus, UpdatesTransactionStatus $updatesTransactionStatus)
    {
        $this->listsBankStatementTransactions = $listsBankStatementTransactions;
        $this->updatesBankStatementTransactionOwnerStatus = $updatesBankStatementTransactionOwnerStatus;
        $this->updatesTransactionStatus = $updatesTransactionStatus;
    }

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

        $filters = $request->except(['per_page','order_by']);

        $statementTransactions = $this->listsBankStatementTransactions->execute($filters);

        foreach ($statementTransactions as $statementTransaction) {
            $owners = $statementTransaction->owners;

            if (count($owners)) {
                $this->updatesBankStatementTransactionOwnerStatus->execute($owners->first(), ApprovalStatus::APPROVED);

                // automatically approve payment if pending verification

//                if ($statementTrasactionOwner->owner->type !== StatementTransactionOwnerType::SALES) continue;
//                if ($statementTrasactionOwner->owner->status === ApprovalStatus::PENDING_VERIFICATION ) {
//                    $this->updatesTransactionStatus->execute($statementTrasactionOwner->owner, ApprovalStatus::APPROVED);
//                }
            }
        }

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