<?php

namespace App\Console\Commands;

use App\User;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Mail;
use App\Mail\PasswordExpiryMail;

class PasswordExpiryNotification extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'password_expiry:notification';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Password Expiry Notification';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $user = User::find(169);

        $user_check = $user ?  $user->password_expiration : null;

        if ($user_check){

            $password_updated_at = $user_check->password_updated_at ? $user_check->password_updated_at : $user->updated_at;
            $expiry_days_left = (Carbon::now()->daysInYear - Carbon::now()->diffInDays($password_updated_at));
            $date = Carbon::now()->addDays($expiry_days_left)->format('D d M, Y');

            if (($expiry_days_left >= 0) && ((Carbon::now()->daysInYear - Carbon::now()->diffInDays($password_updated_at)) == 90)){

                $msg = "Hello {$user->username},
Your Deywuro Admin account will expire in 3 months,
kindly have it updated before it finally expires on {$date}.
Thank You
Deywuro";
                $this->sendSms($msg,$user->company->company_phone_number);
                $this->sendEmail($user->company->name,$user->email,$days_left = 90,$date);

            }elseif (($expiry_days_left >= 0) && ((Carbon::now()->daysInYear - Carbon::now()->diffInDays($password_updated_at)) == 60)){

                $msg = "Hello {$user->username},
Your Deywuro Admin account will expire in 2 months,
kindly have it updated before it finally expires on {$date}.
Thank You
Deywuro";
                $this->sendSms($msg,$user->company->company_phone_number);
                $this->sendEmail($user->company->name,$user->email,$days_left = 60, $date);


            }elseif (($expiry_days_left >= 0) && ((Carbon::now()->daysInYear - Carbon::now()->diffInDays($password_updated_at)) == 30)){
                $msg = "Hello {$user->username},
Your Deywuro Admin account will expire in 1 months,
kindly have it updated before it finally expires on {$date}.
Thank You
Deywuro";
                $this->sendSms($msg,$user->company->company_phone_number);
                $this->sendEmail($user->company->name,$user->email,$days_left = 30,$date);


            }elseif (($expiry_days_left >= 0) && ((Carbon::now()->daysInYear - Carbon::now()->diffInDays($password_updated_at)) == 5)){
                $msg = "Hello {$user->username},
Your Deywuro Admin account will expire in 5 days,
kindly have it updated before it finally expires on {$date}.
Thank You
Deywuro";
                $this->sendSms($msg,$user->company->company_phone_number);
                $this->sendEmail($user->company->name,$user->email,$days_left = 5, $date);


            }elseif (($expiry_days_left == 0) && ((Carbon::now()->daysInYear - Carbon::now()->diffInDays($password_updated_at)) == 0)){
                $msg = "Hello {$user->username},
Kindly note that your Password has reached its expiry today. 
Your password, if not updated after today poses security risks to you.
Thank You
Deywuro";
                $this->sendSms($msg,$user->company->company_phone_number);
                $this->sendEmail($user->company->name,$user->email,$days_left = 0, $date);


            }elseif (($expiry_days_left < 0) && ((Carbon::now()->daysInYear - Carbon::now()->diffInDays($password_updated_at)) < 0)){
                $msg = "Hello {$user->username},
Kindly note that your Password has reached its expiry. 
Your password, if not updated poses security risks to you.
Thank You
Deywuro";
                $this->sendSms($msg,$user->company->company_phone_number);
                $this->sendEmail($user->company->name,$user->email,$days_left = $expiry_days_left, $date);

            }

        }
    }


    private function sendEmail($name,$email,$days_left,$date)
    {
        try{
            Mail::to($email)->cc(['support@npontu.com'])->send(new PasswordExpiryMail($name,$email,$days_left,$date));
        }catch (\Exception $ex){
            file_put_contents(public_path('ResetEmail.log'),"{$ex->getMessage()} \n",FILE_APPEND);
        }
    }


    public  function sendSms($text,$phone,$source = 'Deywuro')
    {
        $message = urlencode($text);
        $num = urlencode($phone);

        $password = urlencode('cse@2021_npontu');

        $url = "https://deywuro.com/api/sms?username=sammy&password=$password&source=$source&destination=" . $num . "&message=" . $message;

        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return response()->json(json_decode($output));
    }
}
