<?php

namespace App\Core\Mail;

use App\Exceptions\ApiErrorException;
use App\Models\PropertyProductOffer;
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 PropertyProductOfferMail 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;


            $propertyProductOffer = PropertyProductOffer::where('offer_key', $params['offerKey'])->first();
            $propertyProductOffer = $propertyProductOffer ? $propertyProductOffer->toArray() : null;

            if (!$propertyProductOffer) {
                throw new ApiErrorException('The offer was not found');
            }


            $mailData = [
                'to' => [
                    'name' => $propertyProductOffer['executive_name'],
                    'email' => $propertyProductOffer['executive_email'],
                ],
                'bcc' => ['sales@extranetwork.com']

            ];

            $mailData['cc'] = [];
            if (isset($propertyProductOffer['account_manager_email'])) {
                $mailData['cc'][] = $propertyProductOffer['account_manager_email'];
            }

            $mailSenderAddress = Config::get('app.mailSenderAddress');

            $mailParams = [
                'property_name' => $propertyProductOffer['property_name'],
                'executive_name' => $propertyProductOffer['executive_name'],
                'executive_email' => $propertyProductOffer['executive_email'],
                'executive_phone' => $propertyProductOffer['executive_phone'],
                'offer_expire_date' => $propertyProductOffer['offerExpireTimeFormatted'],
                'logo' => 'https://www.extranetwork.com/assets/img/logo/mini-logo.png',
                'btnUrl' => config('app.url') . '/property-product-offer/' . $propertyProductOffer['offer_key']
            ];

            if ($propertyProductOffer['version'] == 'v2') {
                $mailParams['btnUrl'] = config('app.webUrl') . '/property-product-offer/' . $propertyProductOffer['offer_key'];
            }

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

            $subjectMail = 'Extranetwork ' . $mailParams['property_name'] . ' Fiyat Teklifi';
            if (isset($propertyProductOffer['detailArray']['paket'])) {
                $subjectMail = 'Extranetwork ' . $mailParams['property_name'] . ' - ' . $propertyProductOffer['detailArray']['paket'] . ' Fiyat Teklifi';
            }

            return $this->from($mailSenderAddress, 'Extranetwork')
                ->view('emails.propertyProductOfferMail', compact('mailParams'))
                ->to($mailData['to']['email'], $mailData['to']['name'])
                ->cc($mailData['cc'])
                ->bcc($mailData['bcc'])
                ->subject($subjectMail)
                ->with(['message' => $this]);


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

}
