<?php

namespace App\Models;

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

class MasterStand extends Model
{
    use HasFactory;
    use SoftDeletes;

    protected $table = 'master_stand';

    protected $fillable = [
        'nama',
        'tanggal_bergabung',
        'is_active',
    ];

    protected $casts = [
        'tanggal_bergabung' => 'date',
        'is_active' => 'boolean',
    ];

    public function menus(): HasMany
    {
        return $this->hasMany(MasterMenu::class, 'id_stand');
    }

    protected static function booted(): void
    {
        static::deleting(function (MasterStand $stand): void {
            if ($stand->isForceDeleting()) {
                $stand->menus()->withTrashed()->get()->each->forceDelete();
                return;
            }

            $stand->menus()->get()->each->delete();
        });
    }
}
