<?php

namespace App\Jobs\Company\WorkerRequest;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\DB;
use App\Models\Company;
use App\Models\CompanySettings;

class ProcessNotifyCompaniesForNewWorkerRequest implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    public $tries = 1;
    protected $zipcodeNotificationData;
    protected $listOfZipcode;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($zipcodeNotificationData, $listOfZipcode)
    {
        $this->zipcodeNotificationData = $zipcodeNotificationData;
        $this->listOfZipcode = $listOfZipcode;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {

        $companyID = $this->zipcodeNotificationData['company_id'];
        $listOfCompaniesSubscribed = $this->getCompaniesSubscribedOnList($this->listOfZipcode, $companyID); //get all the company whose subsribed zipcode is one of the list

        $listOfCompaniesNotSubscribed = $this->getCompanyZipcodeWithNoSettingData($this->listOfZipcode, $companyID); //get all the company who dont have subscribed zipcode (settings data), but company zipcode is one of the list
        

        Log::info('from job: '. json_encode($listOfCompaniesSubscribed));
        Log::info('$listOfCompaniesNotSubscribed: '. json_encode($listOfCompaniesNotSubscribed));
        // $this->processSendEmailNotification()
    }


    public function getCompaniesSubscribedOnList($zipcodeList, $companyID){
        return Company::select(
            'id as company_id',
        )->with('settings')->whereHas('settings', function ($query) use ($zipcodeList){
            return $query
            ->where('company_settings.key', 'subscribed_zipcode')
            ->whereIn('company_settings.value', $zipcodeList);
        })
        ->where('companies.id', '!=', $companyID)
        ->get();
    }

    public function getCompanyZipcodeWithNoSettingData($zipcodeList, $companyID){
        return Company::select(
            'id as company_id',
        )
        ->whereNotExists(function ($query) use ($companyID){
            $query->select(DB::raw(1))
                ->from('company_settings')
                ->where('company_settings.company_id', '!=', $companyID) // the requester should not be included
                ->whereRaw('company_settings.company_id = companies.id')
                ->where('company_settings.key', 'subscribed_zipcode');
        })
        ->where('companies.id', '!=', $companyID) // the requester should not be included
        ->whereIn('zipcode', $zipcodeList)
        ->with('settings')
        ->get();
    }

    public function processSendEmailNotification($companies){
        foreach($companies as $company){

            if(count($company->settings)){
                if($this->isEmailNotificationEnabled($company->settings)){
                    // process send email

                }
            }
        }
    }

    public function isEmailNotificationEnabled($settings){
        foreach($settings as $setting){
            if($setting->key == 'worker_request_email_notification' && $setting->value == 'enabled'){
               return true;
            }
        }
        return false;
    }   
}
