<?php

namespace App\Console\Commands;

use App\Models\Forecast;
use App\Models\Helpers\General;
use App\Models\Payment;
use App\Models\Subscriber;
use Illuminate\Console\Command;

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

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

    /**
     * Execute the console command.
     */
    public function handle()
    {
        $incoming_forecast = Forecast::query()
        ->where('schedule', now()->toDateString())->latest()->first();

        $active_subscribers = Payment::query()->where('status', 'active')
        ->where('status', 'active');
        // ->where('subscription_expiry', '>=', now()->toDateString()); //Get all subscribers from subscriber table

        if ($active_subscribers !== null) {
            try {
                foreach ($active_subscribers as $subscriber) {
                    $data = [
                        'phone' => $subscriber->phone,
                        'title' => $incoming_forecast->title,
                        'banker' => $incoming_forecast->banker,
                        'two_sure' => $incoming_forecast->two_sure,
                        'three_direct' => $incoming_forecast->three_direct,
                        'permutations' => $incoming_forecast->permutations,
                    ];
                    return General::send_lotto_numbers($data);
                }
            } catch (\Throwable $th) {
                General::logError('SENDING LOTTO FORECAST', json_encode($subscriber), 'handle() in ForecastTriggerCommand.php file');
                General::logError('SENDING LOTTO FORECAST', $th->getMessage(), 'handle() in ForecastTriggerCommand.php file');
            }
        }
    }
}
