<?php

namespace App\Http\Rules\User;

use Auth;
use App\Models\Contract;

use App\Http\Helpers\Hasher;

use Illuminate\Contracts\Validation\Rule;

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

    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('contracts', $row);
                $contract = Contract::
                    where('id', $id)
                    ->first();

                foreach ($contract->contract_obligation_list as $key => $row) {
                    if ($row->status != config('constants.contract_obligation.status.active')) {
                        $allow = false;
                    }
                }
            }
        }

        return $allow;
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return config('error_code.contract_done_error');
    }
}
