<?php

namespace App\Classes\Jobs\Commands\V2;

use App\Classes\Modules\KeyValuePairs\DataTransferObjects\KeyValuePairObject;
use App\Classes\Modules\KeyValuePairs\Services\CreatesKeyValuePair;
use App\Classes\Modules\KeyValuePairs\Services\UpdatesKeyValuePair;
use App\Classes\ValueObjects\Constants\KVPKey;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Models\Booking;
use App\Models\KeyValuePair;
use Illuminate\Support\Facades\Log;


class ProcessPaymentReportV2CommandJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /** @var array */
    private $details;

    /**
     * ProcessPaymentReportV2CommandJob constructor.
     * @param array $details
     */
    public function __construct(array $details)
    {
        $this->details = $details;
    }

    public function handle()
    {
        Log::info(Carbon::now() . ': Start job - Processing single record from 01R - RECEIVE PAYMENT [AR RECEIVE PAYMENT] Import.');
        $start   = new Carbon();

        $docNo         = $this->details['docno'] ?? null;
        $docDate       = $this->details['docdate'] ?? null;
        $debtorCode    = $this->details['debtorcode'] ?? null;
        $description   = $this->details['description'] ?? null;
        $paymentMethod = $this->details['paymentmethod'] ?? null;
        $paymentAmt    = $this->details['paymentamt'] ?? null;
        $knockOffDocNo = $this->details['knockoffdocno'] ?? null;

        Log::info("Processing Payment Report:", [
            'DocNo' => $docNo,
            'DocDate' => $docDate,
            'DebtorCode' => $debtorCode,
            'Description' => $description,
            'PaymentMethod' => $paymentMethod,
            'PaymentAmt' => $paymentAmt,
            'KnockOffDocNo' => $knockOffDocNo,
        ]);

        if($knockOffDocNo){
            $kvp = KeyValuePair::where('key', KVPKey::AUTOCOUNT_DOCNO_INVOICE)->where('value', $knockOffDocNo)->first();
            if($kvp){
                $booking = $kvp->owner;
                if($booking){
                    if($docNo != "" && $docNo != "<<New>>"){
                        $this->updateOrCreateKeyValuePair($booking, KVPKey::AUTOCOUNT_DOCNO_OFFICIAL_RECEIPT, $docNo);
                    }
                    // if($eInvoiceValidationLink){
                    //     $this->updateOrCreateKeyValuePair($booking, KVPKey::AUTOCOUNT_EINVOICE_VALIDATION_LINK, $eInvoiceValidationLink);
                    // }
                }
            }
        }

        $end = new Carbon();
        $elapsedTime = $start->diff($end)->format('%H:%I:%S');
        Log::info(Carbon::now() . ': End job - Processing single record from 01R - RECEIVE PAYMENT [AR RECEIVE PAYMENT] Import. ElapsedTime: ' . $elapsedTime . '.');
    }


    private function updateOrCreateKeyValuePair($booking, $key, $value)
    {
        $keyValuePairObject = new KeyValuePairObject($key, $value);
        $metadata = $booking->attributesKVP()->where('key', $key)->first();

        if ($metadata) {
            (App()->make(UpdatesKeyValuePair::class))->execute($metadata, $keyValuePairObject);
        } else {
            (App()->make(CreatesKeyValuePair::class))->execute($booking, $keyValuePairObject);
        }
    }
}
