<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class TextbookCatalog extends Model
{
    protected $table = 'textbook_catalog_nodes';

    protected $fillable = [
        'textbook_id',
        'parent_id',
        'node_type',
        'title',
        'display_no',
        'depth',
        'sort_order',
        'path_key',
        'is_required',
        'is_elective',
        'tags',
        'page_start',
        'page_end',
        'meta',
    ];

    protected $casts = [
        'is_required' => 'boolean',
        'is_elective' => 'boolean',
        // 移除 array cast，直接使用 JSON 字符串
    ];

    public function textbook()
    {
        return $this->belongsTo(Textbook::class);
    }

    public function parent()
    {
        return $this->belongsTo(self::class, 'parent_id');
    }

    public function children()
    {
        return $this->hasMany(self::class, 'parent_id')->orderBy('sort_order');
    }
}
