<?php

namespace App\Http\Rules\User;

use Auth;
use App\Models\ContractObligation;
use App\Models\Contract;

use App\Http\Helpers\Hasher;

use Illuminate\Contracts\Validation\Rule;

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

    private $type;

    public function __construct($type = '')
    {
        $this->type = $type;
    }

    /**
     * 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];

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

                if (!empty($contract_obligation->contract_obligation_dependency_list)) {
                    foreach ($contract_obligation->contract_obligation_dependency_list as $key_2 => $row_2) {
                        $depend_contract_obligation_id = Hasher::decode('contract_obligations', $row_2->depend_contract_obligation_hash_id);
                        $depend_contract_obligation = ContractObligation::
                            where('id', $depend_contract_obligation_id)
                            ->first();

                        if ($this->type == 'complete') {
                            $depend_contract_obligation_must_complete = $depend_contract_obligation->must_complete;
                            $depend_contract_obligation_complete_status = $depend_contract_obligation->complete_status;
                            if ($depend_contract_obligation_must_complete == config('constants.contract_obligation.must_complete.complete')) {
                                if ($depend_contract_obligation_complete_status != config('constants.contract_obligation.complete_status.completed')) {
                                    return false;
                                }
                            }
                        }

                        if ($this->type == 'accept') {
                            $depend_contract_obligation_must_accept = $depend_contract_obligation->must_accept;
                            $depend_contract_obligation_accept_status = $depend_contract_obligation->accept_status;
                            if ($depend_contract_obligation_must_accept == config('constants.contract_obligation.must_accept.accept')) {
                                if ($depend_contract_obligation_accept_status != config('constants.contract_obligation.accept_status.accepted')) {
                                    return false;
                                }
                            }
                        }
                    }

                    if (!empty($contract_obligation->contract_obligation_contract_dependency_list)) {
                        foreach ($contract_obligation->contract_obligation_contract_dependency_list as $key_2 => $row_3) {
                            $depend_contract_id = Hasher::decode('contracts', $row_3->depend_contract_hash_id);
                            $depend_contract = Contract::
                                where('id', $depend_contract_id)
                                ->first();
                                if ($depend_contract->status != config('constants.contract.status.done')) {
                                    return false;
                                }
                        }
                    }
                }
            }
        }

        return true;
    }

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