<?php

namespace App\Http\Rules\User;

use Auth;
use App\Models\ContractObligation;

use App\Http\Helpers\Hasher;

use Illuminate\Contracts\Validation\Rule;

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

    private $type;

    public function __construct()
    {
        
    }

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

        $allow = true;
        foreach ($value as $key => $row) {
            if(!empty($row)) {
                $id = Hasher::decode('contract_obligations', $row);
                $contract_obligation = ContractObligation::
                    where('id', $id)
                    ->first();

                $contract_obligation_must_complete = $contract_obligation->must_complete;
                $contract_obligation_complete_status = $contract_obligation->complete_status;
                if ($contract_obligation_must_complete == config('constants.contract_obligation.must_complete.complete')) {
                    if ($contract_obligation_complete_status != config('constants.contract_obligation.complete_status.completed')) {
                        $allow = false;
                    }
                }

                $contract_obligation_must_accept = $contract_obligation->must_accept;
                $contract_obligation_accept_status = $contract_obligation->accept_status;
                if ($contract_obligation_must_accept == config('constants.contract_obligation.must_accept.accept')) {
                    if ($contract_obligation_accept_status != config('constants.contract_obligation.accept_status.accepted')) {
                        $allow = false;
                    }
                }
            }
        }

        return $allow;
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        if ($this->type == 'complete') {
            return config('error_code.contract_obligation_done_error');
        }
    }
}
