<?php

namespace App\Modules\Company\WorkerRequest;

use App\Events\Company\WorkerRequest\NotificationOnNewAddedWorkerRequestEvent;
use App\Models\WorkerRequest;
use App\Models\State;
use App\Models\ZipcodeRange;
use App\Traits\ValidatorTraits;
use App\Traits\UserTraits;
use Carbon\Carbon;

class Manage{

    use ValidatorTraits, UserTraits;

    public function fetchWorkerRequest()
    {
        return 'test';
    }

    public function addWorkerRequest($payload)
    { 
        $rules = array(
            'date_start'      => 'required',
            'date_end'        => 'required',
            'expertise'       => 'required',
            'experience'      => 'required',
            'work_area_lists' => 'required',
            'state'           => 'required',
            'state_radius'    => 'required',
            'city'            => 'required',
        );

        $validate = $this->validateRequest($payload, $rules);
        
        if ($validate) 
        {
            return $validate;
        }

        $companyID = $this->getCurrentUser()->company_id;

        $areasOfWork = [];

        foreach($payload->work_area_lists as $area){
            array_push($areasOfWork, array('id' => $area));
        }

        $zipcodeRange = $this->getZipcodeDataUsingStateCity($payload->state, $payload->city);

        if(!$zipcodeRange){
            return response()->json(['errors' => ["The selected state and city is not supported by our current database. Please select only the suggested value."]])->setStatusCode(201);
        }

        $workerRequest = array(
            'company_id'       => $companyID,
            'expertise_id'     => $payload->expertise,
            'experience'       => $payload->experience,
            'areas_of_work'    => json_encode($areasOfWork),
            'zipcode_range_id' => $zipcodeRange->id,                                      //should be the zipcode range id: get using state and the city
            'date_end'         => Carbon::parse($payload->date_end)->format('Y-m-d'),
            'date_start'       => Carbon::parse($payload->date_start)->format('Y-m-d'),
        );
        
        $requestTransaction = $this->createWorkerRequest($workerRequest);

        $zipcodeNotificationData = (object) 
        [
            'id'               => $requestTransaction->id,
            'company_id'       => $companyID,
            'expertise_id'     => $payload->expertise,
            'experience'       => $payload->experience,
            'zipcode'          => $zipcodeRange->zipcode,
            'zipcode_range_id' => $zipcodeRange->id,
            'range'            => $payload->state_radius,
        ];

        event(new NotificationOnNewAddedWorkerRequestEvent($zipcodeNotificationData)); 

        // $this->processNotification($payloadForZipcodeNotification);

        if($requestTransaction)
        {
            return response()->json(['success' => ["Worker Request successfuly posted."]])->setStatusCode(201);
        }

        return response()->json(['errors' => ["Something went wrong while posting the worker request. Please try again later."]])->setStatusCode(201);
    }

    public function createWorkerRequest($data)
    {
        return WorkerRequest::create($data);
    }

    public function getZipcodeDataUsingStateCity($stateName, $cityName)
    {
        $state = State::where('name', $stateName)
            ->where('city', $cityName)
            ->first();

        $zipcodeData = ZipcodeRange::select(
            'id',
            'zipcode', 
        )->where('state_id', $state->id)->first();

        return $zipcodeData;
    }  

    public function processNotification($zipcodeNotificationData)
    {
        event(new NotificationOnNewAddedWorkerRequestEvent($zipcodeNotificationData)); 
    }

}
