<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class KnowledgeArticle extends Model
{
    use HasFactory, SoftDeletes;

    protected $fillable = [
        'title','slug','type','content','project_id','module_id','bug_id','created_by','is_published','published_at',
    ];

    protected function casts(): array
    {
        return ['is_published' => 'boolean', 'published_at' => 'datetime'];
    }

    public function tags(): BelongsToMany
    {
        return $this->belongsToMany(KnowledgeTag::class, 'knowledge_article_tag');
    }

    public function project(): BelongsTo { return $this->belongsTo(Project::class); }
    public function module(): BelongsTo { return $this->belongsTo(ProjectModule::class, 'module_id'); }
    public function bug(): BelongsTo { return $this->belongsTo(Bug::class); }
    public function author(): BelongsTo { return $this->belongsTo(User::class, 'created_by'); }
}
