<?php

namespace App\Classes\Modules\Bookings\ControllersLogic;

use App\Classes\Exceptions\CriteriaNotFulfilledException;
use App\Classes\Exceptions\MalformedRequestException;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Bookings\DataTransferObjects\CreatePaymentVerificationDocumentDTO;
use App\Classes\Modules\Companies\Services\FetchesCompany;
use App\Classes\Modules\Companies\Services\UpdatesCompanyStatus;
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\FetchesTransaction;
use App\Classes\Modules\Transactions\Services\UpdatesTransactionStatus;
use App\Classes\Modules\Rules\Services\RuleEvaluator;
use App\Classes\Modules\Rules\Standards\Rules\CanPassOrderDurationLimitRule;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\CompanyType;
use App\Classes\ValueObjects\Constants\DocumentType;
use App\Models\Company;
use App\Models\Document;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class CreatePaymentVerificationDocumentLogic 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 RuleEvaluator */
    private $ruleEvaluator;

    /** @var CanPassOrderDurationLimitRule */
    private $canPassOrderDurationLimitRule;

    /**
     * CreatePaymentVerificationDocumentLogic constructor.
     * @param FetchesTransaction $fetchesTransaction
     * @param CreatesDocument $createsDocument
     * @param CreatesFiles $createsFile
     * @param UpdatesTransactionStatus $updatesTransactionStatus
     * @param RuleEvaluator $ruleEvaluator
     * @param CanPassOrderDurationLimitRule $canPassOrderDurationLimitRule
     */
    public function __construct(FetchesTransaction $fetchesTransaction, CreatesDocument $createsDocument, CreatesFiles $createsFile, UpdatesTransactionStatus $updatesTransactionStatus, RuleEvaluator $ruleEvaluator, CanPassOrderDurationLimitRule $canPassOrderDurationLimitRule)
    {
        $this->fetchesTransaction = $fetchesTransaction;
        $this->createsDocument = $createsDocument;
        $this->createsFile = $createsFile;
        $this->updatesTransactionStatus = $updatesTransactionStatus;
        $this->ruleEvaluator = $ruleEvaluator;
        $this->canPassOrderDurationLimitRule = $canPassOrderDurationLimitRule;
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws \App\Classes\Exceptions\MalformedRequestException
     */
    public function logic(Request $request) : JsonResponse
    {
        $dto = new CreatePaymentVerificationDocumentDTO($request->all());

        $result = $this->ruleEvaluator->evaluate([
            $this->canPassOrderDurationLimitRule,
        ], $dto);

        if ($result->failed()) {
            throw new CriteriaNotFulfilledException("- " . implode("<br>- ", $result->messages()));
        }

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

        $object = new DocumentObject(DocumentType::CUSTOMER_PAYMENT_PROOF, $request->input('files'), '', ApprovalStatus::PENDING_VERIFICATION, 'payments');

        $hasExcel = false;
        foreach ($object->getFiles() as $file) {
            $decodedFile = json_decode($file);
            if (($decodedFile->extension) === 'excel') {
                $hasExcel = true;
                break;
            }
        }

        if ($hasExcel) {
            throw new MalformedRequestException("Please upload files in PDF, JPEG, or PNG format.");
        }

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

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

        $this->updatesTransactionStatus->execute($transaction, ApprovalStatus::PENDING_VERIFICATION);
        return $this->response([]);
    }

}
