<?php

namespace App\Classes\Modules\Accounting\ControllersLogic;

use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Http\Resources\BankStatementTransactionOwnerResource;
use App\Classes\ValueObjects\Constants\StatementTransactionOwnerType;
use App\Classes\Modules\Accounting\Services\FetchesBankStatementTransactionOwner;
use App\Classes\Modules\Accounting\Services\UpdatesBankStatementTransactionOwnerStatus;

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 FetchesBankStatementTransactionOwner */
    private $fetchesBankStatementTransactionOwner;

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

    /**
     * UpdateAnnouncementLogic constructor.
     * @param FetchesBankStatementTransactionOwner $fetchesBankStatementTransactionOwner
     * @param UpdatesBankStatementTransactionOwnerStatus $updatesBankStatementTransactionOwnerStatus
     */
    public function __construct(
        FetchesBankStatementTransactionOwner $fetchesBankStatementTransactionOwner,
        UpdatesBankStatementTransactionOwnerStatus $updatesBankStatementTransactionOwnerStatus
    ) {
        $this->fetchesBankStatementTransactionOwner = $fetchesBankStatementTransactionOwner;
        $this->updatesBankStatementTransactionOwnerStatus = $updatesBankStatementTransactionOwnerStatus;
    }

    /**
     * @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
    {
        $statementTrasactionOwner = $this->fetchesBankStatementTransactionOwner->execute(['id' => $request->route('id')]);

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

        return $this->resourceResponse(new BankStatementTransactionOwnerResource($statementTrasactionOwner));
    }
}
