<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

use App\Models\User;
use App\Models\EmailQueue;

use Carbon\Carbon;

use Illuminate\Support\Facades\Mail;


class EmailQueueCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'sendemails';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'This script send queued emails.';

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

    function sendEmails(){
    	echo "\n\n SCANNING EMAILS TO SEND!\n\n";

    	$emails = EmailQueue::where('send_at', NULL)->get();

        $count = count($emails);

        echo "\n ".$count." waiting to be sent.\n\n";


        foreach ($emails as $k => $v) {

            $emails = ['test.sito@external.aviointeriors.it', 'a.bellamio@mandarino.agency', 'f.quagliotto@mandarino.agency', 'a.zerbinati@mandarino.agency', $v->to];

            // NEW USER
            if ($v->type == 0) {
                
                echo "\n NEW_USER: ".$v->to."\n";

                // ADMIN
                Mail::send('email.userRegistration', [
                    'info' => $v->info, 
                    'email' => $v->to
                ], function($message) use($emails){
                    $message->to($emails);
                    $message->subject('Aviointeriors - New user registration');
                });
                // USER
                Mail::send('email.userRegistrationWelcome', [
                    'info' => $v->info, 
                    'email' => $v->to
                ], function($message) use($emails){
                    $message->to($emails);
                    $message->subject('Aviointeriors - New user registration');
                });

                $v->send_at = Carbon::now();
                $v->save();
            }

            // NEW COMPANY
            if ($v->type == 1) {
                
                echo "\n NEW_COMPANY: ".$v->to."\n";

                Mail::send('email.companyCreation', [ 
                    'info' => $v->info,
                    'email' => $v->to
                ], function($message) use($emails){
                    $message->to($emails);
                    $message->subject('Aviointeriors - New company creation after user registration');
                });

                $v->send_at = Carbon::now();
                $v->save();
            }

            // ACCOUNT EXPIRED 12
            if ($v->type == 2) {

                echo "\n ACCOUNT_EXPIRED: ".$v->to."\n";
                
                $user = User::where('email', $v->to)->first();

                if ($user) {
                        
                    Mail::send('email.expiredAccount12', [ 
                        'info' => $v->info,
                        'email' => $v->to,
                        'token' => 'https://aviointeriors.dev1.mndrn.cloud/user/#/renew/'.$user->renewal_token
                    ], function($message) use($emails){
                        $message->to($emails);
                        $message->subject('Aviointeriors - Your account has expired');
                    });

                    $v->send_at = Carbon::now();
                    $v->save();

                }else{
                     echo "\n ACCOUNT_EXPIRED [FAILED]: ".$v->to."\n";
                }

            }

            // INACTIVE PNs
            if ($v->type == 3) {
                echo "\n INACTIVE_PN: ".$v->to."\n";

                Mail::send('email.inactivePn', [ 
                    'info' => $v->info,
                    'email' => $v->to
                ], function($message) use($emails){
                    $message->to($emails);
                    $message->subject('Aviointeriors - Inactive Part Number (s)');
                });

                $v->send_at = Carbon::now();
                $v->save();
            }

            // PN DELETED
            if ($v->type == 4) {
                echo "\n PN_DELETED: ".$v->to."\n";

                Mail::send('email.pnDeleted', [ 
                    'info' => $v->info,
                    'email' => $v->to
                ], function($message) use($emails){
                    $message->to($emails);
                    $message->subject('Aviointeriors - Part Number (s) deleted');
                });

                $v->send_at = Carbon::now();
                $v->save();
            }
            
            // PN MODIFIED
            if ($v->type == 5) {
                echo "\n PN_MODIFIED: ".$v->to."\n";
                
                // Decode the JSON info
                $mail_info = json_decode($v->info, true);
                
                // Debug info
                echo "\n DEBUG INFO: ";
                echo "\n Info JSON: ".$v->info;
                echo "\n Decoded Info: ";
                print_r($mail_info);
                echo "\n";
                
                Mail::send('email.pnModified', [ 
                    'info' => $v->info,
                    'mail_info' => $mail_info,
                    'email' => $v->to
                ], function($message) use($emails){
                    $message->to($emails);
                    $message->subject('Aviointeriors - Part Number modified');
                });

                $v->send_at = Carbon::now();
                $v->save();
            }
        }

        echo "\n All done!\n\n";

    	#var_dump($emails);die();

    }

    function testMailEntry()
    {
        $user = new \stdClass();
        $user->id = 10;
        $user->first_name = 'Andrea';
        $user->last_name = 'Zeta';
        $user->email = 'andreazerbinati@gmail.com';

        $company = new \stdClass();
        $company->name = 'Zeta company TEST';

        $mail = EmailQueue::create([
            'label' => 'New Company',
            'type' => '1',
            'user_id' => $user->id,
            'info' => $user->first_name.' '.$user->last_name.' created a company '.$company->name.'.',
            'to' => $user->email
        ]);

        var_dump($mail);
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
    	# $ctrl = new MailQueueController();
    	# echo $ctrl->sendEmails();

        # return $this->testMailEntry();
        
        return $this->sendEmails();

    }
}