<?php

namespace App\Jobs;

use App\Http\Controllers\SMS\QuickSmsController;
use App\Models\JobsHistory;
use App\Services\SentSmsService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Http\Request;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class ReteySmsJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public $data;
    public $tries = 1;
    public $day_end_date;
    public $branch_id;
    public $user_id;
    public $job;
    public $history;

    public function __construct($data, JobsHistory $history)
    {
        $this->data = $data;
        $this->history = $history;

        $this->history->date = $data['date'];
        $this->history->job_type = $data['job_type'];
        $this->history->job_type_id = $data['sms_id'];
        $this->history->status = 0;

        $this->history->save();
    }

    public function handle(SentSmsService $sentSmsService)
    {
        $controller = new QuickSmsController();
        $request = new Request();
        $request['inside'] = true;
        $controller->update($request, $this->data['sms_id']);

        $smsData = $this->data['smsData'];

        $smsData->try_count = ($smsData->try_count+1);
        $smsData->job_status = 1;
        $smsData->save();

        $this->history->status = 1;
        $this->history->save();
    }
}
