<?php

namespace App\Services;

use App\Jobs\ScheduleSmsJob;
use App\Models\JobsHistory;
use App\Models\ScheduleSMS;

class ScheduleService
{
    public function checkSchedule(){
        $currentDate = date('Y-m-d');
        $crrTime = date('H:i:s');
        $scheduledSmses = ScheduleSMS::where('date', $currentDate)->where('time','<=', $crrTime)->where('status', 0)->get();

        foreach ($scheduledSmses as $schedule) {
            $sender_id_type = $schedule->sender_id_type;
            $sms_type = $sender_id_type == 1 ? 'T' : 'P';

            dispatch(new ScheduleSmsJob([
                'sms_type' => $sms_type,
                'user_id' => $schedule->user_id,
                'type_id' => $schedule->type_id,
                'type' => $schedule->type,
                'job_type' => 2,
                'date' => date('Y-m-d'),
                'sender_id' => $schedule->sender_id,
            ], new JobsHistory()));

            $schedule->status = 1;
            $schedule->save();
        }
    }
}
