<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Jobs\Command\ProcessSendEmailAdminNotificationForNewlyCreatedCompany;
use App\Models\Company;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Everyday, notify admins using emails in regards to the newly created company accounts per day';

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

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $dateToday = date('Y-m-d');
        $companies = Company::select(
            'companies.name as company_name',
            'companies.created_at as date_created',
            'companies.company_tel as phone_number',
            'companies.type',
        )
        ->whereDate('companies.created_at', 'LIKE' , $dateToday .'%')
        ->limit(1)
        ->get();

        if(!$companies->isEmpty()) {
            echo 'Queuing adminnotification : newlycreatedcompanies '.date('l jS \of F Y h:i:s A')."\n";
            ProcessSendEmailAdminNotificationForNewlyCreatedCompany::dispatch()->onConnection('jobs_emails')->onQueue('sendemail');
        }else{
            echo 'No company created today '. $dateToday;
        }
    }
}
