<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Modules\Workflow\ScheduledEmail;
use App\Console\Commands\AbstractCommand;
use App\Seraph\Services\GlobalMailerService;
use Carbon\Carbon;

class ScheduledEmailsCommand extends AbstractCommand
{
    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'emails:schedule';

    /**
     * A friendly name of the command that will be used when loggin messages.
     *
     * @var string
     */
    protected $command_title = 'Scheduled Emails';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Sends the scheduled emails.';

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

    /**
     * Execute the console command.
     *
     * @return void
     */
    public function fire()
    {
        // Mail::pretend();

        // Get today's date
        $today = Carbon::now()->toDateString();

        $this->info('Fetching a list of emails that need to be sent out today & previously failed emails.');
        $emails = ScheduledEmail::where('send_on', '<=', $today)->where('sent', 0)->where('automatic', 1)->limit(100)->get();

        if (count($emails) > 0) {
            foreach ($emails as $key => $email) {
                // We are now attempting to send the email using GlobalMailerService (no CC/BCC for system notifications)
                // the settings are already stored in the database table
                // all the variables are replaced as well
                $sent = GlobalMailerService::send('emails.dbtemplate', ['template' => $email->settings['template']], function ($message) use ($email) {
                    $message->to($email['to'])->subject($email->settings['subject']);
                    // CC removed for system notifications - use GlobalMailerService global settings only
                });

                // If the message was sent successfully then we will
                // record this
                if ($sent) {
                    $email->sent = 1;
                    $this->info('Email sent successfully to '.$email['to'].' with scheduled_emails.id = '.$email['id']);
                } else {
                    // Something went wrong and the email could not be sent...
                    $email->failed = 1;
                    $this->info('Email failed to send to '.$email['to'].' with scheduled_emails.id = '.$email['id']);
                }

                // Save the state of the email
                $email->save();
            }
        } else {
            $this->info('No emails were found.');
        }
    }

    /**
     * Get the console command arguments.
     *
     * @return array
     */
    protected function getArguments()
    {
        return [

        ];
    }

    /**
     * Get the console command options.
     *
     * @return array
     */
    protected function getOptions()
    {
        return [

        ];
    }
}
