<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;
use Schema;
use DB;
use Illuminate\Contracts\Encryption\DecryptException;
use EF;
use Crypt;
use App\Contract;

class ContractVerificationRule implements Rule
{
    /**
     * Create a new rule instance.
     *
     * @return void
     */
    private $parms;
    private $contract_id;

    private $message;

    public function __construct($parms,$contract_id)
    {
        $this->parms = $parms;
        $this->contract_id = $contract_id;
        $this->message = 'The validation error message.';
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {


        $sdate         = $this->parms['start_date'];
        $edate         = $this->parms['end_date'];

        $scale_id               = decrypt($this->parms['scale']);

        $designation_id         = decrypt($this->parms['designation']);

         if($scale_id == 0 && $designation_id == 0)
            {
                $this->setMessage("Select Scale or Designation to create contract");
                return false;
            }

        // THIS PART WORK ONLY FOR THE CONTRACT UPDATEATION
        try 
        {


            $contract_id = decrypt($this->contract_id);
            $contract = $this->getContractById($contract_id);

            if(isset($contract->id))
            {
                $this->parms['employee'] = Crypt::encrypt($contract->user_id);
                //CHECK DATE
                if(isset($contract->id) && $this->parms['status'] == "Deactive")
                {
                    if($contract->end_date > date('Y-m-d'))
                    {

                        $this->setMessage('Contract has not yet expired.');
                        return false;
                    }
                } 
            }
        }
        catch (DecryptException $e) 
        {
            $this->setMessage('Contract Invalid');

            return false;
        }
        // END THIS PART WORK ONLY FOR THE CONTRACT UPDATEATION


        

        if((strlen(trim($sdate)) > 0))
        {
            if($this->verification($sdate,$edate))
            {
                $this->setMessage('Contract already found.');

                return false;
            }
        }
        else
        {
            $this->setMessage('Contract is invalid');

            return false;
        }



        return true;

    }

    /**
     * Get the validation error message.
     *
     * @return string
     */

    public function setMessage($msg)
    {
        $this->message = $msg;
    }


    public function message()
    {
        return $this->message;
    }

    public function getParmsList()
    {
        return $this->parms;
    }
    
    public function verification($sdate,$edate)
    {
        // THIS QUERY IS SATISFIED WHEN CONDITION IS 1 RELATED TO DURATION
        
        try 
        {
            $contract_id = decrypt($this->contract_id);

            $user_id = decrypt($this->parms['employee']);
        
            $condition = ['user_id' => $user_id, 'is_recycle' => '0' ];
            
            if(strlen(trim($edate)) <= 0)
            {
                $flag1 =    DB::table('contracts')
                        ->where($condition)
                        ->where('id','!=',$contract_id)
                        ->whereDate('start_date','>=',$sdate)
                        ->count();
            }
            else
            {
                $flag1 =    DB::table('contracts')
                        ->where($condition)
                        ->where('id','!=',$contract_id)
                        ->whereDate('start_date','>=',$sdate)
                        ->whereDate('end_date','<=',$edate)
                        ->count();
            }


            

            

            $condition = ['user_id' => $user_id, 'is_recycle' => '0','status' => 'Active'];
            $flag2 = DB::table('contracts')
                    ->where($condition)
                    ->where('id','!=',$contract_id)
                    ->count();

            if(!$flag1 && !$flag2)
            {
                return 0;
            }

            return 1;

        }
        catch (DecryptException $e) 
        {
            //
        }

    }

    public function getContractById($id)
    {
        $contract =  new Contract;

        return $contract->getContractById($id);
    }
}
