<?php

namespace App\Core\Mail;

use App\Exceptions\ApiErrorException;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

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

class BookingPaymentDataCodeMail extends Mailable
{

    use Queueable, SerializesModels;

    public $tries = 5;
    public $timeout = 30;

    protected $param;


    public function __construct($param)
    {
        $this->param = $param;
    }

    public function build()
    {
        try {

            $params = $this->param;
            $mailSenderAddress = Config::get('app.mailSenderAddress');
            $bookingService = App::make('App\Core\Service\BookingService');
            $userService = App::make('App\Core\Service\UserService');

            $requestData = [
                'criteria' => [
                    ['field' => 'id', 'condition' => '=', 'value' => $params['booking_id']]
                ],
                //'with' => ['bookingProperty.propertyExecutive', 'bookingProperty.propertyBookingEngineToken', 'bookingProperty.propertyBrand', 'bookingContact', 'bookingChannel', 'bookingPayment', 'bookingPaymentType', 'bookingStatus'],
                'firstRow' => true
            ];

            $booking = $bookingService->select($requestData);

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

            $booking = $booking['data'];

            $requestData = [
                'criteria' => [
                    ['field' => 'id', 'condition' => '=', 'value' => $params['user_id']]
                ],
                'firstRow' => true
            ];

            $user = $userService->select($requestData);

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

            $user = $user['data'];

            $mailParams = [
                'to' => $user['email'],
                'toNameSurname' => $user['nameSurname'],
                'bcc' => [],
                'unlockCode' => $params['unlock_code'],
                'bookingId' => fillOnUndefined($booking, 'id'),
                'propertyId' => fillOnUndefined($booking, 'property_id'),
                'bookingCode' => fillOnUndefined($booking, 'booking_code'),
                'locale' =>  fillOnUndefined($user, 'language', 'en'),
            ];


            /*echo view('emails.bookingPaymentDataCode', compact('mailParams'));
            die();*/

            $mailParams['bcc'][] = 'burhan@extranetwork.com';

            return $this->from($mailSenderAddress, 'Extranetwork')
                ->view('emails.bookingPaymentDataCode', compact('mailParams'))
                ->to($mailParams['to'], $mailParams['toNameSurname'])
                ->bcc($mailParams['bcc'])
                ->subject(__('api-mailing-payment_data_code-subject',['bookingCode' => $mailParams['bookingCode']],$mailParams['locale']));


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

}
