<?php

namespace App\Models;

use App\Constants\Status;
use App\Traits\GlobalStatus;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;

class Knowledge extends Model
{

    use GlobalStatus;

     protected $hidden = ['description'];

    public function likes()
    {
        return $this->hasMany(LikeDislike::class)->where('type', 'like');
    }

    public function dislikes()
    {
        return $this->hasMany(LikeDislike::class)->where('type', 'dislike');
    }

    public function comments()
    {
        return $this->hasMany(Comment::class)->where('comment_id', 0);
    }

    public function views()
    {
        return $this->hasMany(KnowledgeView::class);
    }

    public function category()
    {
        return $this->belongsTo(KnowledgeCategory::class, 'knowledge_category_id');
    }

    public function subcategory()
    {
        return $this->belongsTo(KnowledgeCategory::class, 'knowledge_subcategory_id');
    }
    
    public function admin()
    {
        return $this->belongsTo(Admin::class);
    }

    public function statusBadge(): Attribute
    {
        return new Attribute(function () {
            if ($this->status == Status::ENABLE) {
                return '<span class="badge badge--success">' . trans('Published') . '</span>';
            } else {
                return '<span class="badge badge--warning">' . trans('Unpublished') . '</span>';
            }
        });
    }

}
