<?php

namespace App\Classes\Modules\Transactions\ControllersLogic;


use App\Classes\General\Abstracts\AbstractControllerLogic;
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\Transactions\Processors\CreateInvoiceTransactionV2Processor;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\CompanyType;
use App\Classes\ValueObjects\Constants\DocumentType;
use App\Classes\Jobs\SendUserPaymentProofUploadedEmail;
use App\Classes\ValueObjects\Constants\KVPKey;
use App\Models\Company;
use App\Models\Document;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;


class CreatePaymentProofDocumentLogic 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 CreateInvoiceTransactionV2Processor */
    private $createInvoiceTransactionProcessor;

    /** @var SendUserPaymentProofUploadedEmail */
    private $sendUserPaymentProofUploadedEmail;

    /**
     * CreatePaymentProofDocumentLogic constructor.
     * @param FetchesTransaction $fetchesTransaction
     * @param CreatesDocument $createsDocument
     * @param CreatesFiles $createsFile
     * @param UpdatesTransactionStatus $updatesTransactionStatus
     * @param CreateInvoiceTransactionV2Processor $createInvoiceTransactionProcessor
     */
    public function __construct(FetchesTransaction $fetchesTransaction, CreatesDocument $createsDocument, CreatesFiles $createsFile, UpdatesTransactionStatus $updatesTransactionStatus, CreateInvoiceTransactionV2Processor $createInvoiceTransactionProcessor, SendUserPaymentProofUploadedEmail $sendUserPaymentProofUploadedEmail)
    {
        $this->fetchesTransaction = $fetchesTransaction;
        $this->createsDocument = $createsDocument;
        $this->createsFile = $createsFile;
        $this->updatesTransactionStatus = $updatesTransactionStatus;
        $this->createInvoiceTransactionProcessor = $createInvoiceTransactionProcessor;
        $this->sendUserPaymentProofUploadedEmail = $sendUserPaymentProofUploadedEmail;
    }

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

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

        $object = new DocumentObject(DocumentType::CURRENCY_VENDOR_PAYMENT_PROOF, $request->input('files'), '', ApprovalStatus::APPROVED, 'china_bank_slip');

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

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

        $this->updatesTransactionStatus->execute($transaction, ApprovalStatus::APPROVED);

        $this->createInvoiceTransactionProcessor->execute($transaction->owner->booking, "", null, [
                'generateEInvoice' => false,
                'generateEInvoiceWithNormalInvoiceTemplate' => false,
                'generateEInvoiceRefund' => false,
                'bookingOriginalStatus' => ApprovalStatus::COMPLETED //cief todo: 151
            ]
        );

        //cief todo: 110 - Prototype Period Lock - Starts
        // $this->updateOrCreateKeyValuePair($transaction->owner->booking, KVPKey::BOOKING_IS_LOCKED, 1);
        //cief todo: 110 - Prototype Period Lock - Ends

        // send email to customer
        // todo: a function to send a proof to the receipiant, they have to give us a email of the receipiant and also need to submiited purchase order
        $companyEmployee = $transaction->owner->booking->company->employees;
        foreach ($companyEmployee as $employee) {
            if (app()->environment('production') || in_array($employee->email, ['cief.enquirycntr@gmail.com', 'tech.ciefmalaysia@gmail.com'])) {
                $this->sendUserPaymentProofUploadedEmail::dispatch($employee, $transaction->owner->booking, $file[0]);
            }
        }

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

    private function updateOrCreateKeyValuePair($booking, $key, $value)
    {
        $booking->attributesKVP()->updateOrCreate(
            ['key' => $key],
            ['value' => $value]
        );
    }
}
