<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\CompanySubscriptionPayments;
use App\Jobs\Command\ProcessSendEmailAdminNotificationForCompanySubscriptionPayments;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Everyday, send email notification on super admin admin in regards to daily company subscription payment';

    /**
     * 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');
        $notificationTypeId = 3; //Notification for company subscription payments

        $companypayments = CompanySubscriptionPayments::select(
            'companies.name as company_name',
            'company_subscription_payments.created_at as date_payed',
            'company_subscription_payments.amount_paid',
        )
        ->join('companies', 'companies.id', 'company_subscription_payments.company_id')
        ->whereDate('company_subscription_payments.created_at', 'LIKE' , $dateToday .'%')
        ->limit(1)
        ->get();

        // print_r($companies);

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