<?php

namespace App\Models;

use App\Http\Helpers\Hasher;

use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;

class ContractTemplateObligation extends Model
{
    protected $table = 'contract_template_obligations';

    protected $appends = [
        'hash_id', 
        'contract_template_list', 
        'contract_template_obligation_dependency_list', 
        'depend_contract_template_obligation_dependency_list'
    ];

    public function contract_template()
    {
        return $this->belongsTo('App\Models\ContractTemplate', 'contract_template_id');
    }

    public function contract_template_obligation_dependency()
    {
        return $this->hasMany('App\Models\ContractTemplateObligationDependency', 'contract_template_obligation_id');
    }

    public function depend_contract_template_obligation_dependency()
    {
        return $this->hasMany('App\Models\ContractTemplateObligationDependency', 'depend_contract_template_obligation_id');
    }

    public function getHashIdAttribute()
    {
        return Hasher::encode('contract_template_obligations', $this->id);
    }

    public function getContractTemplateListAttribute()
    {
        return $this->contract_template()->get()->makeHidden(['contract_template_obligation_list']);
    }

    public function getContractTemplateObligationDependencyListAttribute()
    {
        return $this->contract_template_obligation_dependency()
        ->where('status', config('constants.contract_template_obligation.status.active'))
        ->get()->makeHidden(['contract_template_obligation_list', 'depend_contract_template_obligation_list']);
    }

    public function getDependContractTemplateObligationDependencyListAttribute()
    {
        return $this->depend_contract_template_obligation_dependency()
            ->where('status', config('constants.contract_template_obligation.status.active'))
            ->get()->makeHidden(['contract_template_obligation_list', 'depend_contract_template_obligation_list']);
    }

    public function getUpdatedAtAttribute($date) 
    {
        return $date ? Carbon::parse($date)->timezone('Asia/Kuala_Lumpur')->format('Y-m-d') : '';
    }

    public function getCreatedAtAttribute($date) 
    {
        return $date ? Carbon::parse($date)->timezone('Asia/Kuala_Lumpur')->format('Y-m-d') : '';
    }

    public function getContractTemplateObligationDependencyStructure($contract_template_obligation_id = [], $contract_template_obligation_id_structure = [])
    {
        $contract_template_obligation_id = !empty($contract_template_obligation_id) ? Hasher::decode('contract_template_obligations', $contract_template_obligation_id[0]) : $this->id;
        $contract_template_obligation = ContractTemplateObligation::find($contract_template_obligation_id);

        if (!empty($contract_template_obligation->contract_template_obligation_dependency_list)) {
            foreach ($contract_template_obligation->contract_template_obligation_dependency_list as $key => $row) {
                $contract_template_obligation_id_structure[] = $row->depend_contract_template_obligation_hash_id;
                if (!empty($row->depend_contract_template_obligation_list)) {
                    return $this->getContractTemplateObligationDependencyStructure([$row->depend_contract_template_obligation_hash_id], $contract_template_obligation_id_structure);
                }
            }
        }

        return $contract_template_obligation_id_structure;
    }
}
