<?php

namespace App\Models;

use App\Http\Helpers\Hasher;

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

class Contract extends Model
{
    protected $table = 'contracts';

    protected $appends = [
        'hash_id', 
        'user_hash_id',
        'contract_template_hash_id', 
        'contract_obligation_list', 
        'contract_entity_list'
    ];

    protected $hidden = [
        'id', 
        'user_id', 
        'contract_template_id', 
        'in_time_contract_template'
    ];

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

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

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

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

    public function getContractTemplateHashIdAttribute()
    {
        return Hasher::encode('contract_templates', $this->contract_template_id);
    }

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