<?php

namespace App\Models;

use App\Enums\Status;
use Illuminate\Database\Eloquent\Builder;
use App\Models\BaseModel;
use Illuminate\Database\Eloquent\Relations\HasMany;

class ContentModel extends BaseModel
{
    protected $table = 'content_models';

    public const CREATED_AT = 'create_time';

    public const UPDATED_AT = 'update_time';

    protected $fillable = [
        'name',
        'alias',
        'description',
        'table_name',
        'sort',
        'status',
        'is_default',
        'field_config',
    ];

    protected $casts = [
        'status' => Status::class,
        'field_config' => 'array',
    ];

    public function fields(): HasMany
    {
        return $this->hasMany(ContentModelField::class, 'model_id');
    }

    public function categories(): HasMany
    {
        return $this->hasMany(ContentCategory::class, 'model_id');
    }

    public function contents(): HasMany
    {
        return $this->hasMany(Content::class, 'model_id');
    }

    public function scopeEnabled(Builder $query): Builder
    {
        return $query->where('status', Status::ENABLED);
    }
}
