<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Str;

class ServiceItemFeature extends Model
{
    use HasFactory;

    /**
     * Indicates if the IDs are auto-incrementing.
     *
     * @var bool
     */
    public $incrementing = false;

    /**
     * The data type of the auto-incrementing ID.
     *
     * @var string
     */
    protected $keyType = 'string';

    protected $fillable = [
        'service_item_id',
        'feature_type_id',
        'is_required',
        'sort_order',
    ];

    protected $casts = [
        'is_required' => 'boolean',
        'sort_order' => 'integer',
    ];

    protected static function boot()
    {
        parent::boot();

        static::creating(function ($feature) {
            if (empty($feature->id)) {
                $feature->id = Str::uuid();
            }
        });
    }

    public function serviceItem()
    {
        return $this->belongsTo(ServiceItem::class);
    }

    public function featureType()
    {
        return $this->belongsTo(FeatureType::class);
    }

    public function activeServiceItemFeatures()
    {
        return $this->hasMany(ServiceItemFeature::class)->where('is_required', false);
    }
}
