<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Send SMS';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {

        $expire_date = date("Y-m-d", strtotime("+1 month"));
        $expire_date_it = date("d/m/Y", strtotime("+1 month"));
        $certificates = \App\Models\MemberCertificate::where('expire_date', $expire_date)->get();
        foreach ($certificates as $certificate) {            
            $new = \App\Models\MemberCertificate::where('expire_date', '>', $expire_date)->where('member_id', $certificate->member_id)->count();
            if ($new == 0)
            {
                $phone = $certificate->member->phone;
                $message = 'Ciao ' . $certificate->member->first_name . ', ci risulta che il tuo certificato medico scade il ' . $expire_date_it . '. Per continuare ad allenarti senza problemi, ricordati di rinnovarlo in tempo. Ti aspettiamo in campo! Centro Sportivo La Madonnella';
                $params = array(
                    'to'            => '+39' . $phone,
                    'from'          => env('SMS_FROM', 'Test'),
                    'message'       => $message,
                    'format'        => 'json',
                );
                sms_send($params);
            }
        }

        $expire_date = date("Y-m-d", strtotime("+15 days"));
        $expire_date_it = date("d/m/Y", strtotime("+15 days"));
        $certificates = \App\Models\MemberCertificate::where('expire_date', $expire_date)->get();
        foreach ($certificates as $certificate) {
            $new = \App\Models\MemberCertificate::where('expire_date', '>', $expire_date)->where('member_id', $certificate->member_id)->count();
            if ($new == 0)
            {
                $phone = $certificate->member->phone;
                $message = 'Ciao ' . $certificate->member->first_name . ', ci risulta che il tuo certificato medico scade il ' . $expire_date_it . '. Per continuare ad allenarti senza problemi, ricordati di rinnovarlo in tempo. Ti aspettiamo in campo! Centro Sportivo La Madonnella';
                $params = array(
                    'to'            => '+39' . $phone,
                    'from'          => env('SMS_FROM', 'Test'),
                    'message'       => $message,
                    'format'        => 'json',
                );
                sms_send($params);
            }
        }

        return Command::SUCCESS;
    }
}
