<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;
use App\Installment;

class InstallmentVerificationRule implements Rule
{
    /**
     * Create a new rule instance.
     *
     * @return void
     */

    private $parms;
    private $message; 

    public function __construct($data)
    {
          $this->parms = $data;
          $this->setMessage("The Amount is Invalid");

    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        $amount = $this->parms['amount'];
    

        $sum_of_amount = array_sum($amount);
        $remaining_balance = $this->planVerification();

        
        if($sum_of_amount > $remaining_balance)
        {
              $this->setMessage("The Amount is Invalid");
                     return false;
        }
       return true;
      
    }

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

    // SET ERROR MESSAGE
    public function setMessage($msg)
    {
        $this->message = $msg;
    }

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

     public function planVerification()
    {
       
         try 
        {
            $booking_id = $this->parms['booking_id'];

            //DECRYPTING ENCRYPTED IDS
            $booking_id   = decrypt($booking_id);

            $installment = new Installment;

            // NEW CHUNCH ADED BY AM'S
            $laging = $installment->sumPaidBalance($booking_id,'Paid');
            
            $remaining_plan =  $installment->verificationPlan($booking_id);

            return $remaining_plan + $laging;


           
        } 
        catch (DecryptException $e) 
        {
            
        }


    }

    

}
