<?php


namespace App\Classes\Modules\Transactions\ControllersLogic;

use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Transactions\Services\FetchesTransaction;
use App\Classes\Modules\Transactions\Services\GeneratesTransactionBillNumber;
use App\Classes\Modules\Transactions\Services\CreatesPaymentTransaction;
use App\Classes\Modules\Transactions\Services\CreatesTransactionDetail;
use App\Classes\Modules\Billplzs\Services\CreatesBillplzBill;

use App\Classes\Modules\Transactions\DataTransferObjects\TransactionObject;
use App\Classes\Modules\Transactions\DataTransferObjects\TransactionDetailObject;

use App\Classes\ValueObjects\Constants\OrderRoleTypes;
use App\Classes\ValueObjects\Constants\TransactionType;
use App\Classes\ValueObjects\Constants\PaymentMethodType;
use App\Classes\ValueObjects\Constants\ApprovalStatus;

use App\Http\Resources\TransactionResource;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Meneses\LaravelMpdf\Facades\LaravelMpdf;

class CreatePaymentTransactionLogic extends AbstractControllerLogic
{


    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Create Payment Transactions',
            'message' => 'You have successfully created currency supplier transactions'
        ];
    }

    /** @var FetchesTransaction */
    private $fetchesTransaction;

    /** @var FetchesSegmentConstant */
    private $fetchesSegmentConstant;

    /** @var GeneratesTransactionBillNumber */
    private $generatesTransactionBillNumber;

    /** @var CreatesPaymentTransaction */
    private $createsPaymentTransaction;

    /** @var CreatesBillplzBill */
    private $createsBillplzBill;

    /** @var CreatesTransactionDetail */
    private $createsTransactionDetail;

    public function __construct(
        FetchesTransaction $fetchesTransaction, 
        GeneratesTransactionBillNumber $generatesTransactionBillNumber, 
        CreatesPaymentTransaction $createsPaymentTransaction,
        CreatesTransactionDetail $createsTransactionDetail,
        CreatesBillplzBill $createsBillplzBill
    )
    {
        $this->fetchesTransaction = $fetchesTransaction;
        $this->generatesTransactionBillNumber = $generatesTransactionBillNumber;
        $this->createsPaymentTransaction = $createsPaymentTransaction;
        $this->createsTransactionDetail = $createsTransactionDetail;
        $this->createsBillplzBill = $createsBillplzBill;
    }

    public function logic(Request $request) : JsonResponse
    {
        $invoice_transaction = $this->fetchesTransaction->execute(['id' => $request->input('transaction_id')]);
        $amount = $request->input('amount');
        $company_module = $invoice_transaction->owner()->first()->owner()->first()->companyModule()->first();

        $billNumber = $this->generatesTransactionBillNumber->execute('PYMT-');
        $payment_reference = null;
        if ($request->input('payment_method') == 5) {
            $payment_method = PaymentMethodType::PAYMENT_GATEWAY;
            $billPlzBill = $this->createsBillplzBill->execute(
                $company_module->name, 
                (app()->environment(['production'])) ? $company_module->employees()->first()->email : 'uldvstar@gmail.com',
                'This payment is for the invoice number . ' . $invoice_transaction->bill_no,
                $amount,
                $billNumber, 
                $request->input('bank_code'), 
                true
            );

            $payment_reference = $billPlzBill->id;
        }
        else {
            $payment_method = PaymentMethodType::CASH;
        }

        $object = new TransactionObject(
            $billNumber, 
            TransactionType::PAYMENT, 
            $company_module->id,
            1,
            1, 
            $payment_method,
            $request->input('amount'),
            $request->input('amount'),
            1,
            1, 
            0, 
            0, 
            0,
            null, 
            ApprovalStatus::PENDING_SUBMISSION,
            null,
            $payment_reference
        );

        $payment_transaction = $this->createsPaymentTransaction->execute($invoice_transaction, $object);

        return $this->resourceResponse(new TransactionResource($payment_transaction));
    }
}
