<?php

namespace App\Core\Service;


use App\Core\Service\PropertyPaymentService;
use App\Exceptions\ApiErrorException;
use Illuminate\Mail\Mailer ;
use App\Core\Mail\ManualPaymentMail;
use \App\Core\Service\PropertyService;


use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Log;
use Exception;



class ManualPaymentMailService
{
    private $propertyPaymentService;
    private $mailer ;
    private $propertyService;



    public function __construct(
        Mailer $mailer,
        PropertyPaymentService $propertyPaymentService,
        PropertyService $propertyService
    )
    {
        $this->mailer = $mailer ;
        $this->propertyService = $propertyService ;
        $this->propertyPaymentService = $propertyPaymentService;

    }

    public function process ($params = [])
    {
        try
        {


            $paymentDetailParam = [
                'criteria' => [
                    ['field' => 'order_id', 'condition' => '=', 'value' => $params['orderCode']],
                    ['field' => 'transaction_type', 'condition' => '=', 'value' => 'LNK'],
                    ['field' => 'status', 'condition' => '=', 'value' => 5],
                ],
                'with' => ['relatedTransactions', 'paymentUser'],
                'firstRow' => true
            ];

            $paymentDetail = $this->propertyPaymentService->selectPaymentTransaction($paymentDetailParam);


            if ($paymentDetail['status'] != 'success' || empty($paymentDetail['data'])) {
                throw new ApiErrorException(lang('Payment not found'));
            }

            $payment = $paymentDetail['data'] ;
            $transactions = collect($payment['related_transactions']) ;
            $successTransaction =  $transactions->where('status' , '=', 1)->first() ;
            $paymentParams = $payment['paramsArray'];

            if($paymentParams['email']){

                $propertyParam = [
                    'criteria' => [
                        ['field' => 'id', 'condition' => '=', 'value' => $payment['property_id']],
                    ],
                    'with' => ['propertyBrand', 'propertyContact','propertyExecutive'],
                    'firstRow' => true
                ];

                $getProperty = $this->propertyService->select($propertyParam) ;
                if ($getProperty['status'] != "success") {
                    throw new ApiErrorException($getProperty['message']);
                }
                $property = $getProperty['data'];


                $bcc[] = "bd@extranetwork.com";
                if(isset($payment['payment_user']['email'])){
                    $bcc[] = $payment['payment_user']['email'];
                }

                $accountExecutives = collect($property['property_executive'])->where('status',1)->where('executive_type_id',1)->pluck('email')->toArray();
                if(!empty($accountExecutives)) {
                    $bcc = array_merge($bcc,$accountExecutives);
                }

                $mailData = [
                    'to' => [
                        'email' => $paymentParams['email'],
                    ],
                    'bcc' => $bcc

                ];

                $hostAddress = Config::get('app.bookingEngineUrl').'/link/'.$payment['order_id'].'/'.$params['language_code'].'/receipt' ;
                $logo = 'https://www.extranetwork.com/assets/img/logo/logo.png';
                if(isset($property['property_brand']['logo_name'])){
                    $logo = Config::get('app.imageUrl'). '/property-photos/' . $property['id'] . "/logo/" . $property['property_brand']['logo_name'].'_250x250.'. $property['property_brand']['logo_file_ext'];
                }

                $mailViewParams = [
                    'property' => $property,
                    'payment_params' => $paymentParams,
                    'success_transaction' => $successTransaction,
                    'amount' => $successTransaction['amount'],
                    'currency' => $successTransaction['currency'],
                    'date_time' => date('d.m.Y H:i', $successTransaction['created_at'] ),
                    'name_surname' => $successTransaction['paramsArray']['creditCard']['name'],
                    'email' => $paymentParams['email'],
                    'process_title' => $paymentParams['title'],
                    'property_name' => $property['name'],
                    'url' => $hostAddress,
                    'logo' => $logo,
                    'language_code' => $params['language_code'],
                ] ;

                $mailParams = [
                    'mailData' => $mailData,
                    'mailViewParams' => $mailViewParams
                ];

                $this->mailer->onQueue(
                    'manualPaymentMail',
                    new ManualPaymentMail($mailParams)
                );

            }




        }
        catch ( Exception $e )
        {
            $message = $e->getFile()." ".$e->getLine()." ".$e->getMessage();
            Log::error($message);
            return output( ['status' => -1, 'message' => $e->getMessage()] );
        }
    }

}
