<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use DB;
use Illuminate\Http\Request;
use App\Manager;
use Illuminate\Support\Facades\Mail;
use App\EmailTemplate;
use App\Organization;
use App\User;

class SycleSendNotification extends Command
{
    protected $signature = 'sycleNotification:send';
    protected $description = 'Sycle Notification Send';

    /**
     * Execute the command.
     *
     * @return void
    */
    public function handle()
    {
        $email = array();
        $current_date = date('Y-m-d');
        $feedback = DB::table('feedback_send_link_log')
                    ->select('*', 'feedback_send_link_log.id as send_link_id', 'feedback_send_link_log.token as logToken')
                    ->where('feedback_send_link_log.status', 'check')
                    ->join('feedback', 'feedback_send_link_log.feedback_id', '=', 'feedback.id')
                    ->where('feedback.imported_from', 'Sycle')
                    ->where('feedback.is_send_link', '1')
                    ->whereNotNull('next3daydate')
                    ->whereIn('feedback.appointment_status', ['Confirmed','Completed'])
                    ->where('feedback_send_link_log.is_clicked', '0')->get();
        
        if (!empty($feedback)) {
            foreach ($feedback as $key => $val) {
                if ($val->next3daydate == $current_date) {
                    $this->readyMail($val);
                    $this->updateStatusToIgnore($val);
                }
            }
        }

        $this->updateCronLog();
         echo "\n  Procedure done successfully. \n";
    }

    public function updateCronLog()
    {
        $date = date('Y-m-d H:i:s');
        DB::table('cron_log')->where('name', $this->signature)->update(['cron_at' => $date]);
    }

    public function updateStatusToIgnore($feedback)
    {
        DB::table('feedback_send_link_log')->where('id', $feedback->send_link_id)->update(['status' => 'ignore']);
    }

    public function readyMail($feedback)
    {
        $EmailTemplateData = Manager::where('organization_id', $feedback->organization_id)->first();
        $isHippa = "";
        if ($EmailTemplateData) {
            if ($EmailTemplateData->isHippa == '1') {
                $body = EmailTemplate::where('slug', 'FEEDBACK_LINK_MEDICAL')->first();
                $isHippa = 1;
            } else {
                $body = EmailTemplate::where('slug', 'FEEDBACK_LINK_NONMEDICAL')->first();
            }
        } else {
            $body = EmailTemplate::where('slug', 'FEEDBACK_LINK')->first();
        }
        if (!empty($body)) {
            $organization = Organization::find($feedback->organization_id);
            $mngrUser = User::find($organization->user_id);
        }
        if ($organization) {
            $helperobj = new \App\CommonHelper();
            $docUrl = $helperobj->publicPath();
            if ($organization->organization_pic != "") {
                $image = $docUrl . '/orgLogo/' . $organization->id . '/' . $organization->organization_pic;
                $mobileImg = $image;
                $image = DOMAIN_URL . 'orgLogo/' . $organization->id . '/' . $organization->organization_pic;
                $mobileImg = $image;
                // if (!file_exists($image)) {
                //     $image = ANGULARURL . PU_LOGO;
                //     $mobileImg = ANGULARURL . 'assets/img/PU-SMS-logo_new.png';
                // } else {
                //     $image = url() . '/orgLogo/' . $organization->id . '/' . $organization->organization_pic;
                //     $mobileImg = $image;
                // }
            } else {
                $image = ANGULARURL . PU_LOGO;
                $mobileImg = ANGULARURL . 'assets/img/PU-SMS-logo_new.png';
            }
        } else {
            $image = ANGULARURL . PU_LOGO;
            $mobileImg = ANGULARURL . 'assets/img/PU-SMS-logo_new.png';
        }
        $orgName = $organization->name ? $organization->name : '-';
        $tmp1 = str_replace("##FEEDBACK_LINK##", $feedback->short_url, $body['description']);
        $tmp2 = str_replace("##LOGO##", $image, $tmp1);
        $tmp3 = str_replace("##ORGANIZATION_NAME##", $orgName, $tmp2);
        $tmp4 = str_replace("##YEAR##", date('Y'), $tmp3);
        $data['html'] = str_replace("##SUPPORT_LINK##", SUPPORT_LINK, $tmp4);
        $subject = str_replace("##USER_NAME##", $organization->name, $body['subject']);
        $email = $feedback->email;
        if ($email != '' && filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $this->sendMail(array('mail_body' => $data['html'], 'mail_subject' => $subject, 'to_email' => $email));
        }
        $smsResult=$this->sendMessagetoPhone($feedback, $orgName, $isHippa, $mobileImg);
    }


    public function sendMail($value)
    {
        $data['html'] = $value['mail_body'];
        $subject = $value['mail_subject'];
        $email = $value['to_email'];
        Mail::send('emails.email', $data, function ($message) use ($subject, $email) {
            $message->from(FROM_IN_MAIL, APP_NAME_IN_MAIL);
            $message->to($email);
            $message->subject($subject);
        });
    }

    public function sendMessagetoPhone($feedback, $orgName, $isHippa, $mobileImg)
    {
        $helperobj = new \App\CommonHelper();
        $link = $feedback->short_url;
        $data['type'] = 'feedback';
        $data['to'] = $feedback->mobile;
        if ($isHippa == 1) {
            $data['msgBody'] = 'Hi,We would like to thank you for choosing ' . $orgName . ' and for being such a wonderful patient. We would greatly appreciate you giving us a positive review online.  Please click the link below to leave a review. It will take about 30 seconds.  Thank you. Click here : ' . $link;
        } else {
            $data['msgBody'] = 'Hi,We would like to thank you for choosing ' . $orgName. ' and for being such a wonderful patron of our business. We would greatly appreciate you giving us a positive review online.  Please click the link below to leave a review. It will take about 30 seconds.  Thank you. Click here : ' . $link;
        }
        $data['media'] = $mobileImg;
        $smsResult = $helperobj->sendSMS($data);
    }
}
