<?php

namespace App\Classes\Modules\PerfexCRM\Processors;

use App\Classes\Modules\PerfexCRM\DataTransferObjects\UpdatePerfexCRMObject;
use App\Classes\ValueObjects\Constants\PerfexCRMTasks;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\TransactionType;
use App\Classes\ValueObjects\Constants\PerfexCRMProjectStatus;
use App\Classes\ValueObjects\Constants\PerfexCRMTaskStatus;
use App\Classes\Jobs\UpdatePerfexCRMInvoice;
use App\Classes\Jobs\UpdatePerfexCRM;
use App\Classes\Jobs\UpdatePerfexCRMPrelude;
use App\Models\Transaction;
use Illuminate\Support\Facades\Log;

class TransactionToPerfexCRMProcessor
{
    /**
     * @param Transaction $model
     * @param int $status
     * @return \Illuminate\Database\Eloquent\Model
     * @throws \App\Classes\Exceptions\MalformedRequestException
     */
    public function execute(Transaction $model, int $status)
    {
        try {
            if($model->owner instanceof \App\Models\Booking) {
                $companyReference = $model->owner->company->reference;
                $companyName = $model->owner->company->name;
                $employee = $model->owner->company->employees()->first();
                $contactEmail = $employee->email;
                $contactName = $employee->name;
                $bookingMarking = $model->owner->marking;

                $serviceTypeName = $model->owner->company->services()->where('id', $model->owner->service_id)->first()->name;
                $projectName = 'Exchange | '.$serviceTypeName.' | '.$bookingMarking;

                if($status == ApprovalStatus::APPROVED)
                {
                    if($model->type == TransactionType::PAYMENT){

                        $tasks = [
                            PerfexCRMTasks::TASK_1
                        ];
                        if($model->owner->service_id == 1){
                            $tasks =    [
                                PerfexCRMTasks::TASK_1,
                                PerfexCRMTasks::TASK_1_DAY_TRANSFER_1,
                                PerfexCRMTasks::TASK_1_DAY_TRANSFER_2,
                                PerfexCRMTasks::TASK_1_DAY_TRANSFER_3,
                                PerfexCRMTasks::TASK_1_DAY_TRANSFER_3_1,
                                PerfexCRMTasks::TASK_1_DAY_TRANSFER_4,
                                PerfexCRMTasks::TASK_1_DAY_TRANSFER_5,
                                PerfexCRMTasks::TASK_1_DAY_TRANSFER_6,
                            ];
                        }
                        else if($model->owner->service_id == 3){
                            $tasks =    [
                                PerfexCRMTasks::TASK_1,
                                PerfexCRMTasks::TASK_3_DAY_TRANSFER_1,
                                PerfexCRMTasks::TASK_3_DAY_TRANSFER_2,
                                PerfexCRMTasks::TASK_3_DAY_TRANSFER_3,
                                PerfexCRMTasks::TASK_3_DAY_TRANSFER_3_1,
                                PerfexCRMTasks::TASK_3_DAY_TRANSFER_4,
                                PerfexCRMTasks::TASK_3_DAY_TRANSFER_5,
                                PerfexCRMTasks::TASK_3_DAY_TRANSFER_6,
                            ];
                        }
                        else if($model->owner->service_id == 4){
                            $tasks =    [
                                PerfexCRMTasks::TASK_1,
                                PerfexCRMTasks::TASK_1688_PAYMENT_1,
                                PerfexCRMTasks::TASK_1688_PAYMENT_2,
                                PerfexCRMTasks::TASK_1688_PAYMENT_3,
                                PerfexCRMTasks::TASK_1688_PAYMENT_3_1,
                                PerfexCRMTasks::TASK_1688_PAYMENT_4,
                                PerfexCRMTasks::TASK_1688_PAYMENT_5,
                                PerfexCRMTasks::TASK_1688_PAYMENT_6,
                                PerfexCRMTasks::TASK_1688_PAYMENT_7,
                                PerfexCRMTasks::TASK_1688_PAYMENT_8,
                                PerfexCRMTasks::TASK_1688_PAYMENT_9,
                                PerfexCRMTasks::TASK_1688_PAYMENT_10,
                                PerfexCRMTasks::TASK_1688_PAYMENT_11,
                                PerfexCRMTasks::TASK_1688_PAYMENT_12,
                                PerfexCRMTasks::TASK_1688_PAYMENT_13,

                            ];
                        }

                        //Add in tasks if customer has no purchase order yet and using 1 days or 3 days transfer service
                        if($model->owner->service_id == 1 || $model->owner->service_id == 3){
                            $booking = $model->booking;
                            $purchaseOrder = $booking->transactions()
                                ->where('type', TransactionType::PURCHASE_ORDER)
                                ->complete()
                                ->first();
                            if(is_null($purchaseOrder)){
                                $potask1 = PerfexCRMTasks::TASK_PURCHASE_ORDER_1;
                                $potask1['status'] = PerfexCRMTaskStatus::NOT_STARTED;
                                $poTasks = [
                                    $potask1,
                                    PerfexCRMTasks::TASK_PURCHASE_ORDER_2,
                                ];
                                $tasks = array_merge($tasks, $poTasks);
                            }
                        }

                        $updatePerfexCRMObject = new UpdatePerfexCRMObject(
                            $companyName,
                            $companyReference,
                            $contactName,
                            $contactEmail,
                            $bookingMarking,
                            $projectName,
                            PerfexCRMProjectStatus::IN_PROGRESS,
                            0,
                            [],
                            $tasks
                        );

                        UpdatePerfexCRMPrelude::dispatch($model, $updatePerfexCRMObject, UpdatePerfexCRM::class, UpdatePerfexCRMInvoice::class);
                    }
                }
                else if($status == ApprovalStatus::PENDING_VERIFICATION){
                    if($model->type == TransactionType::PURCHASE_ORDER){
                        $tasks = [];
                        if($model->owner->service_id == 1 || $model->owner->service_id == 3){
                            $tasks = [
                                PerfexCRMTasks::TASK_PURCHASE_ORDER_1,
                                PerfexCRMTasks::TASK_PURCHASE_ORDER_2,
                            ];
                        }

                        if(count($tasks) > 0){
                            $updatePerfexCRMObject = new UpdatePerfexCRMObject(
                                $companyName,
                                $companyReference,
                                $contactName,
                                $contactEmail,
                                $bookingMarking,
                                $projectName,
                                PerfexCRMProjectStatus::IN_PROGRESS,
                                0,
                                [],
                                $tasks
                            );
                            UpdatePerfexCRM::dispatch($updatePerfexCRMObject, null, null);
                        }
                    }
                }
            }
        }
        catch (\Exception $exception) {
            Log::error('TransactionToPerfexCRMProcessor debug:');
            Log::error($exception);
        }
        return true;
    }
}
