<?php

namespace App\Models;

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

use Illuminate\Database\Eloquent\Factories\HasFactory;

class PartnerType extends Model
{
    use SoftDeletes, HasFactory;

    protected $fillable = [
        'name',
        'code',
        'description',
        'is_customer_group',
        'is_supplier_group',
        'slug',
    ];

    protected static function booted()
    {
        static::creating(function ($type) {
            if (empty($type->slug)) {
                $type->slug = \Illuminate\Support\Str::slug($type->name);
            }
        });

        static::updating(function ($type) {
            if ($type->isDirty('name') && empty($type->slug)) {
                $type->slug = \Illuminate\Support\Str::slug($type->name);
            }
        });
    }

    protected $casts = [
        'is_customer_group' => 'boolean',
        'is_supplier_group' => 'boolean',
    ];

    public function partners(): HasMany
    {
        return $this->hasMany(Partner::class);
    }
}
