<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class QuestionnaireQuestion extends Model
{
    protected $fillable = [
        'questionnaire_set_id', 'parent_id', 'question_text', 'question_type',
        'options_json', 'rating_labels', 'is_required', 'sort_order',
    ];

    protected function casts(): array
    {
        return [
            'options_json'  => 'array',
            'rating_labels' => 'array',
            'is_required'   => 'boolean',
            'sort_order'    => 'integer',
        ];
    }

    public function questionnaireSet()
    {
        return $this->belongsTo(QuestionnaireSet::class);
    }

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

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

    public function answers()
    {
        return $this->hasMany(QuestionnaireAnswer::class);
    }
}
