<?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 TrialSecondMail 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');

            $propertyService = App::make('App\Core\Service\PropertyService');

            $propertySelectCriteria = [
                'criteria' => [
                    ['field' => 'id', 'condition' => '=', 'value' => $params['propertyId']],

                ],
                'with' => ['userPropertyMapping.user'],
                'firstRow' => true
            ];

            $propertyData = $propertyService->select($propertySelectCriteria);

            if($propertyData['status'] == 'success') {


                $userPropertyMappingCollect = collect($propertyData['data']['user_property_mapping']);
                $userPropertyMapping = $userPropertyMappingCollect->where('status',1)->where('user.status',1)->sortBy('id');
                $userPropertyMapping = $userPropertyMapping ? $userPropertyMapping->toArray() : [];


                $defaultUser = reset($userPropertyMapping);

                $mailData['to'] = [
                    'email' => $defaultUser['user']['email'],
                    'nameSurname' => $defaultUser['user']['nameSurname'],
                ];

                $mailData['bcc'] = [];
                foreach ($userPropertyMapping as $user) {
                    //$mailData['bcc'][] = $user['user']['email'];
                }
                $mailData['bcc'] = 'channel@extranetwork.com';//TODO: Delete

                $mailParams= [
                    'email' => $mailData['to']['email'],
                    'nameSurname' => $mailData['to']['nameSurname'],
                    'propertyName' => $propertyData['data']['name'],
                ];


                $mailTitle = 'Çok görünürlük, daha çok satış.😎';
                //app('translator')->setLocale('en');

                //echo view('emails.reminder.trialSecondMail', compact('mailParams')); die();

                return $this->from($mailSenderAddress, 'Extranetwork')
                    ->view('emails.reminder.trialSecondMail', compact('mailParams'))
                    ->to($mailData['to']['email'], $mailData['to']['nameSurname'])
                    ->bcc($mailData['bcc'])
                    ->subject($mailTitle)
                    ->with(['message' => $this]);

            }


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

    }

}
