<?php

namespace App\Classes\Modules\Accounting\ControllersLogic;

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


class UpdateStatementTransactionStatusLogic extends AbstractControllerLogic
{

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

    /** @var FetchesBankStatementTransaction */
    private $fetchesBankStatementTransaction;

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

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

    /**
     * UpdateAnnouncementLogic constructor.
     * @param FetchesBankStatementTransaction $fetchesBankStatementTransaction
     * @param UpdatesBankStatementTransactionOwnerStatus $updatesBankStatementTransactionOwnerStatus
     * @param UpdatesTransactionStatus $updatesTransactionStatus
     */
    public function __construct(
        FetchesBankStatementTransaction $fetchesBankStatementTransaction,
        UpdatesBankStatementTransactionOwnerStatus $updatesBankStatementTransactionOwnerStatus,
        UpdatesTransactionStatus $updatesTransactionStatus
    ) {
        $this->fetchesBankStatementTransaction = $fetchesBankStatementTransaction;
        $this->updatesBankStatementTransactionOwnerStatus = $updatesBankStatementTransactionOwnerStatus;
        $this->updatesTransactionStatus = $updatesTransactionStatus;
    }

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

        $statementTrasactionOwner = $statementTrasaction->owners->first();

        $this->updatesBankStatementTransactionOwnerStatus->execute($statementTrasactionOwner, $request->route('status') == 'approve' ? ApprovalStatus::APPROVED : ApprovalStatus::REJECTED);

        // todo-new: approve payments status, need to check the owner(if system is shipping, need to api with shipping portal)
        // if ($request->route('status') == 'approve') {
        //     if ($statementTrasactionOwner->transaction->type === StatementTransactionOwnerType::SALES) {
        //         if ($statementTrasactionOwner->owner->status === ApprovalStatus::PENDING_VERIFICATION) {
        //             $this->updatesTransactionStatus->execute($statementTrasactionOwner->owner, ApprovalStatus::APPROVED);
        //         }
        //     }
        // }

        return $this->resourceResponse(new BankStatementTransactionResource($statementTrasaction));
    }
}
