<?php

namespace App\Models;

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

class ServerGroup extends Model
{
    protected $guarded = [];

    /** Allowed @theme colour token keys for a group's dot/pill (R3 — never a raw hex). */
    public const COLORS = ['accent', 'cyan', 'online', 'warning', 'offline', 'ink'];

    protected static function booted(): void
    {
        static::creating(fn (self $g) => $g->uuid ??= (string) Str::uuid());
        // Fall back to the brand colour if an out-of-allow-list token slips in (defence in depth;
        // the component validates too — this guarantees the token is always a real @theme colour).
        static::saving(function (self $g): void {
            if (! in_array($g->color, self::COLORS, true)) {
                $g->color = 'accent';
            }
        });
    }

    // R11: addressed by uuid in URLs, never the bigint id.
    public function getRouteKeyName(): string
    {
        return 'uuid';
    }

    public function servers(): BelongsToMany
    {
        return $this->belongsToMany(Server::class);
    }
}
