<?php

namespace App\Models;

use App\Http\Helpers\Hasher;

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

class ContractEntity extends Model
{
    protected $table = 'contract_entities';

    protected $appends = ['hash_id', 'user_hash_id', 'contract_hash_id', 'entity_hash_id', 'entity_signature_hash_id', 'contract_list', 'entity_list'];

    protected $hidden = [
        'id', 'user_id', 'contract_id', 'entity_id', 'entity_sinature_id',
    ];

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

    public function entity()
    {
        return $this->belongsTo('App\Models\Entity', 'entity_id');
    }

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

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

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

    public function getEntityHashIdAttribute()
    {
        return Hasher::encode('entities', $this->entity_id);
    }

    public function getEntitySignatureHashIdAttribute()
    {
        return Hasher::encode('entity_signatures', $this->entity_signature_id);
    }

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

    public function getEntityListAttribute()
    {
        return $this->entity()->get()->makeHidden(['entity_signature_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') : '';
    }
}
