<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Http;
use App\Http\Controllers\SendEmailsOperatorController;

class SendEmailImmatInvalide extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'mail:send {operator} {--yes}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Envoyer un mail à un opérateur mis en paramètre.';

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

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        if($this->option('yes')){
            $array_sending_mail_operator = SendEmailsOperatorController::listVehicles();

            $find = false;
            for($i = 0; $i < count($array_sending_mail_operator); $i++){
                if($array_sending_mail_operator[$i]['email'] === $this->argument('operator')){
                    $find = true;
                    $destinataire = $array_sending_mail_operator[$i]["email"];
                    $operator_id = $array_sending_mail_operator[$i]["operator_id"];
                    $probleme = '';
                    $name = $array_sending_mail_operator[$i]["name"];

                    foreach($array_sending_mail_operator[$i]["vehicules"] as $items){
                        if(isset($items['error2'])){
                            $error2 = $items['error2'];
                            $probleme .= $items['name'] . " : " . "<a href=\"https://mycarlab.fr/clients/" . $items['client_id'] . "/vehicule/" . $items['id'] . "\">Modifier</a><br>";
                        }

                        if(env('EMAIL_DEV_TEST') == FALSE)
                        {
                            SendEmailsOperatorController::edit($items['id']);
                        }
                    }

                    $email = file_get_contents(resource_path('views/email_immat_invalide.blade.php'));
                    $lien_immat = str_replace("{liens_vehicules}", $probleme, $email);
                    $message = str_replace("{Operator_name}", $array_sending_mail_operator[$i]["name"], $lien_immat);

                    $req = Http::asForm()->post('https://www.spot-hit.fr/api/envoyer/e-mail', [
                        'key' => '495d6d6f3018d0d42ead704e6f9de82f',
                        'sujet' => 'Problème de remontage de véhicule(s)',
                        'message' => $message,
                        'destinataires' => $destinataire,
                        'expediteur' => 'carlab@test.com',
                        'nom_expediteur' => 'Carlab'
                    ])->json();



                    if(isset($req['erreurs'])){
                        $this->error("Error : " .  $req['erreurs'] . " !\n" . "Échec de l'envoi du mail !");
                    }
                    else{
                        $this->info("Success !\n" . "Le mail a bien été envoyé a l'opérateur " . $this->argument('operator'));
                    }
                }
            }
            if($find === false){
                $this->error("Error !\n" .  "L'opérateur ayant l'adresse mail " . $this->argument('operator') . " n'a pas été trouvé !\n");
            }
        }
        else{
            $this->info("Vous allez envoyer un email a l'opérateur " . $this->argument('operator') . " dont le(s) véhicule(s) ne sont pas remontés. \n");
            if($this->confirm('Êtes-vous sur ?')){

                $array_sending_mail_operator = SendEmailsOperatorController::listVehicles();
                $find = false;
                for($i = 0; $i < count($array_sending_mail_operator); $i++){
                    if($array_sending_mail_operator[$i]['email'] === $this->argument('operator')){
                        $find = true;
                        $destinataire = $array_sending_mail_operator[$i]["email"];
                        $operator_id = $array_sending_mail_operator[$i]["operator_id"];
                        $probleme = '';
                        $name = $array_sending_mail_operator[$i]["name"];

                        foreach($array_sending_mail_operator[$i]["vehicules"] as $items){
                            if(isset($items['error2'])){
                                $error2 = $items['error2'];
                                $probleme .= $items['name'] . " : " . "<a href=\"https://mycarlab.fr/clients/" . $items['client_id'] . "/vehicule/" . $items['id'] . "\">Modifier</a><br>";
                            }
                            if(env('EMAIL_DEV_TEST') == FALSE)
                            {
                                SendEmailsOperatorController::edit($items['id']);
                            }
                        }

                        $email = file_get_contents(resource_path('views/email_immat_invalide.blade.php'));
                        $lien_immat = str_replace("{liens_vehicules}", $probleme, $email);
                        $message = str_replace("{Operator_name}", $array_sending_mail_operator[$i]["name"], $lien_immat);

                        $req = Http::asForm()->post('https://www.spot-hit.fr/api/envoyer/e-mail', [
                            'key' => '495d6d6f3018d0d42ead704e6f9de82f',
                            'sujet' => 'Problème de remontage de véhicule(s)',
                            'message' => $message,
                            'destinataires' => $destinataire,
                            'expediteur' => 'carlab@test.com',
                            'nom_expediteur' => 'Carlab'
                        ])->json();

                        if(isset($req['erreurs'])){
                            $this->error("Error : " .  $req['erreurs'] . " !\n" . "Échec de l'envoi du mail !");
                        }
                        else{
                            $this->info("Success !\n" . "Le mail a bien été envoyé a l'opérateur " . $this->argument('operator'));
                        }
                    }
                }
                if($find === false){
                    $this->error("Error !\n" .  "L'opérateur ayant l'adresse mail " . $this->argument('operator') . " n'a pas été trouvé !\n");
                }
            }
        }
    }
}
