<?php

namespace App\Jobs\Company;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Models\Worker;
use App\Models\ProjectWorkersTimesheet;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;
use App\Modules\SMS\SendSMSViaTwilio;
use App\Traits\CreateMobileNotificationTraits;
use Illuminate\Support\Facades\Log;


class ProcessTimesheetReminder implements ShouldQueue
{
    use CreateMobileNotificationTraits;
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    public $type;
    public $details;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($details, $type)
    {
        $this->details = $details;
        $this->type = $type; // remind all or specific
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        switch ($this->type) {
            case 'specific':
                // remind specific workers
                $this->sendToWorker();
                break;
            case 'all':
                // remind all workers
                $this->sendToAll();
                break;
        }
    }

    public function sendToWorker()
    {
        $workerData = Worker::select('phone_num')->where('id', $this->details->worker_id)->first();
        
        $message = 'Please submit a time log for the ' . Carbon::parse($this->details->date)->format('jS \\of F Y') .'.';
        $mobileNotificationData = array(
            'worker_id' => $this->details->worker_id,
            'message' => $message,
            'type' => 'worker',
            'link' => '/worker/tasks',
        );
        $this->createMobileNotification($mobileNotificationData); 

        //SMS notification
        if($workerData->phone_num){
            $this->send($workerData->phone_num, '[ConX] ' .$message);
        }
    }

    public function sendToAll()
    {
        
        $projectID = $this->details->project_id;
        $date = Carbon::parse($this->details->date)->format('Y-m-d');


        $assignedWorkers  =  ProjectWorkersTimesheet::select(
            'project_workers_timesheet.id',
            'project_workers_timesheet.date',
            'project_workers_timesheet.worker_id',
            'workers.phone_num'
            )
            ->leftJoin('workers', 'workers.id', '=', 'project_workers_timesheet.worker_id')
            ->join('assigned_workers', 'assigned_workers.id', '=', 'project_workers_timesheet.project_worker_id')
            ->join('projects', 'projects.id', '=', 'assigned_workers.project_id')
            ->where('project_workers_timesheet.date', $date)
            ->where('projects.id', $projectID)
            ->get();

            foreach($assignedWorkers as $worker){
                $message = 'Please submit a time log for the ' . Carbon::parse($date)->format('jS \\of F Y') .'.';
                $mobileNotificationData = array(
                    'worker_id' => $worker->worker_id,
                    'message' => $message,
                    'type' => 'worker',
                    'link' => '/worker/tasks',
                );


                $this->createMobileNotification($mobileNotificationData); 

                if($worker->phone_num){
                    $this->send($worker->phone_num, '[ConX] ' .$message);
                }
                //SMS notification
            }
    }
    
    public function send($number, $message)
    {
        $twilio = new SendSMSViaTwilio;
        return $twilio->sendSMS($number, $message);
    }
}
