<?php

namespace App\Models\INSURANCE;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class InsuranceType extends Model
{
    use HasFactory;

    protected $fillable = [
        'code',
        'description',
        'parent_id',
        'status',
    ];

    /**
     * Get the parent insurance type.
     */
    public function parent(): BelongsTo
    {
        return $this->belongsTo(InsuranceType::class, 'parent_id');
    }

    /**
     * Get the immediate children.
     */
    public function children(): HasMany
    {
        return $this->hasMany(InsuranceType::class, 'parent_id');
    }

    /**
     * Recursively retrieve all children.
     */
    public function childrenRecursive(): HasMany
    {
        return $this->children()->with('childrenRecursive');
    }
}
