<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Form_question extends Model
{
    use HasFactory;

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'form_question';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'id_section',
        'question',
        'answer_type',
        'id_custom_ui',
        'id_parent_question',
        'sequence',
        'tittle_numbering',
        'id_form',
        'is_active',
        'options', // Assuming options stored as JSON
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'is_active' => 'boolean',
        'options' => 'json', // Cast options to JSON automatically
    ];

    /**
     * Relationship to the parent question (for nested questions).
     */
    public function parent()
    {
        return $this->belongsTo(Form_question::class, 'id_parent_question');
    }

    /**
     * Relationship to child questions (for nested questions).
     */
    public function childQuestions()
    {
        return $this->hasMany(Form_question::class, 'id_parent_question');
    }

    /**
     * Relationship to the section the question belongs to.
     */
    public function form_section()
    {
        return $this->belongsTo(Form_section::class, 'id_section');
    }

    /**
     * Relationship to the form the question belongs to.
     */
    public function form()
    {
        return $this->belongsTo(Form::class, 'id_form');
    }

    /**
     * Relationship to form answers associated with this question.
     */
    public function form_answers()
    {
        return $this->hasMany(Form_answer::class, 'id_form_question');
    }
}
