<?php

namespace App\Models;

use App\Http\Helpers\Hasher;

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

class ContractObligation extends Model
{
    protected $table = 'contract_obligations';

    protected $appends = [
        'hash_id', 
        'user_hash_id',
        'contract_hash_id', 
        'contract_template_obligation_hash_id',
        'contract_list', 
        'contract_obligation_dependency_list', 
        'depend_contract_obligation_dependency_list',
        'contract_obligation_contract_dependency_list',
        'contract_entity_list'
    ];

    protected $hidden = [
        'id',
        'user_id',
        'contract_id',
        'contract_template_obligation_id',
        'contract_entity_id',
        'in_time_contract_obligation_template',
    ];

    public function contract()
    {
        return $this->belongsTo('App\Models\Contract', 'contract_id');
    }

    public function contract_obligation_dependency()
    {
        return $this->hasMany('App\Models\ContractObligationDependency', 'contract_obligation_id');
    }

    public function depend_contract_obligation_dependency()
    {
        return $this->hasMany('App\Models\ContractObligationDependency', 'depend_contract_obligation_id');
    }

    public function contract_obligation_contract_dependency()
    {
        return $this->hasMany('App\Models\ContractObligationContractDependency', 'contract_obligation_id');
    }

    public function contract_entity()
    {
        return $this->belongsTo('App\Models\ContractEntity', 'contract_entity_id');
    }

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

    public function getUserHashIdAttribute()
    {
        return Hasher::encode('users', $this->user_id);
    }

    public function getContractHashIdAttribute()
    {
        return Hasher::encode('contracts', $this->contract_id);
    }

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

    public function getContractEntityHashIdAttribute()
    {
        return Hasher::encode('contract_entities', $this->contract_entity_id);
    }

    public function getContractListAttribute()
    {
        return $this->contract()->get()->makeHidden(['contract_obligation_list']);
    }

    public function getContractObligationDependencyListAttribute()
    {
        return $this->contract_obligation_dependency()
        // ->where('status', config('constants.contract_obligation.status.active'))
        ->get()->makeHidden(['contract_obligation_list', 'depend_contract_obligation_list']);
    }

    public function getDependContractObligationDependencyListAttribute()
    {
        return $this->depend_contract_obligation_dependency()
            // ->where('status', config('constants.contract_obligation.status.active'))
            ->get()->makeHidden(['contract_obligation_list', 'depend_contract_obligation_list']);
    }

    public function getContractObligationContractDependencyListAttribute()
    {
        return $this->contract_obligation_contract_dependency()
        // ->where('status', config('constants.contract_obligation_contract_dependency.status.active'))
        ->get()->makeHidden(['contract_obligation_list']);
    }

    public function getContractEntityListAttribute()
    {
        return $this->contract_entity()->get()->makeHidden(['contract_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') : '';
    }
}
