<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

class Feature extends Model
{
    protected $keyType = 'string';
    public $incrementing = false;

    protected $fillable = [
        'key',
        'label',
        'group',
        'icon',
        'sort',
        'active',
        'feature_group_id',
    ];

    protected $casts = [
        'active' => 'boolean',
    ];

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

        static::creating(function ($model) {
            if (!$model->id) {
                $model->id = (string) Str::uuid();
            }
        });
    }

    public function group()
    {
        return $this->belongsTo(FeatureGroup::class, 'feature_group_id');
    }

    public function plans()
    {
        return $this->belongsToMany(
            \App\Models\Plan::class,
            'feature_plan',
            'feature_id',
            'plan_id'
        )->withTimestamps(false);
    }

}
