<?php

namespace App\Models;

use App\Models\Concerns\BelongsToOwner;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Str;

class ContactGroup extends Model
{
    use BelongsToOwner;

    protected $table = 'meet_contact_groups';

    protected $fillable = [
        'uuid', 'owner_ref', 'organization_id', 'name', 'description',
    ];

    protected static function booted(): void
    {
        static::creating(function (ContactGroup $group) {
            if (empty($group->uuid)) {
                $group->uuid = (string) Str::uuid();
            }
        });
    }

    public function getRouteKeyName(): string
    {
        return 'uuid';
    }

    public function organization(): BelongsTo
    {
        return $this->belongsTo(Organization::class, 'organization_id');
    }

    public function members(): HasMany
    {
        return $this->hasMany(ContactGroupMember::class, 'contact_group_id');
    }
}
