<?php

namespace App\Models;

use App\Http\Helpers\Hasher;

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

class ContractObligationDependency extends Model
{
    protected $table = 'contract_obligation_dependencies';

    protected $appends = [
        'hash_id', 
        'contract_obligation_hash_id', 
        'depend_contract_obligation_hash_id', 
        'contract_obligation_list', 
        'depend_contract_obligation_list'
    ];

    public function contract_obligation()
    {
        return $this->belongsTo('App\Models\ContractObligation', 'contract_obligation_id');
    }

    public function depend_contract_obligation()
    {
        return $this->belongsTo('App\Models\ContractObligation', 'depend_contract_obligation_id');
    }

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

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

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

    public function getContractObligationListAttribute()
    {
        return $this->contract_obligation()->get()->makeHidden(['contract_obligation_dependency_list']);
    }

    public function getDependContractObligationListAttribute()
    {
        return $this->depend_contract_obligation()->get()->makeHidden(['contract_obligation_dependency_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') : '';
    }
}
