<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Http;
use App\Http\Controllers\AdminController;
use App\Http\Controllers\SendEmailsOperator;

class SendEmails extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'mails:send_all {--yes}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Envoyer un mail à tous les opérateurs.';

    /**
     * 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 = SendEmailsOperator::listVehicles();
            for($i = 0; $i < count($array_sending_mail_operator); $i++){
                $destinataire = $array_sending_mail_operator[$i]["email"];
                $operator_id = $array_sending_mail_operator[$i]["operator_id"];
                $immat = '';
                foreach($array_sending_mail_operator[$i]["vehicules"] as $items){
                    $error1 = $items['error1'];
                    if(isset($items['error2'])){
                        $error2 = $items['error2'];
                        $immat .= $items['name'] . " : " . $items['error1'] . " & " . $items['error2'] . "\n";
                    }else{
                        $immat .= $items['name'] . " : " . $items['error1'] . "\n";
                    }
                SendEmailsOperator::edit($items['id']);
            }
	            $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' => 'Bonjour ' . $operator_id . ',' . '<br>' . 'Nous rencontrons le problème suivant : ' . '<br>' . $immat . '<br>',
                'destinataires' => $destinataire,
                'expediteur' => 'carlab@test.com',
                'nom_expediteur' => 'Carlab'
            ])->json();
            }
            if(isset($req['erreurs'])){
                $this->error("Error : " .  $req['erreurs'] . " !\n" . "Échec de l'envoi des mails !");
            }
            else{
                $this->info("Success !\n" . "Les mails ont bien été envoyé aux opérateurs");
            }
        }else{
            $this->info("Vous allez envoyer un email a tous les opérateurs dont le(s) véhicule(s) ne sont pas remontés. \n");
            if($this->confirm('Êtes-vous sur ?')){
                $array_sending_mail_operator = SendEmailsOperator::listVehicles();
                for($i = 0; $i < count($array_sending_mail_operator); $i++){
                    $destinataire = $array_sending_mail_operator[$i]["email"];
                    $operator_id = $array_sending_mail_operator[$i]["operator_id"];
                    $immat = '';
                    foreach($array_sending_mail_operator[$i]["vehicules"] as $items){
                        $error1 = $items['error1'];
                        if(isset($items['error2'])){
                            $error2 = $items['error2'];
                            $immat .= $items['name'] . " : " . $items['error1'] . " & " . $items['error2'] . "\n";
                        }else{
                            $immat .= $items['name'] . " : " . $items['error1'] . "\n";
                        }
                    SendEmailsOperator::edit($items['id']);
                }
                $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' => 'Bonjour ' . $operator_id . ',' . '<br>' . 'Nous rencontrons le problème suivant : ' . '<br>' . $immat . '<br>',
                    'destinataires' => $destinataire,
                    'expediteur' => 'carlab@test.com',
                    'nom_expediteur' => 'Carlab'
                ])->json();
                }
                if(isset($req['erreurs'])){
                    $this->error("Error : " .  $req['erreurs'] . " !\n" . "Échec de l'envoi des mails !");
                }
                else{
                    $this->info("Success !\n" . "Les mails ont bien été envoyé aux opérateurs");
                }
            }
        }
    }
}
