<?php

namespace App\Modules\Settings\Rbac\DTOs;

final readonly class RoleWriteData
{
    /**
     * @param  list<int>  $permissionIds
     */
    public function __construct(
        public string $name,
        public ?string $description,
        public bool $isActive,
        public array $permissionIds,
    ) {}

    /**
     * @param  array<string, mixed>  $validated
     */
    public static function fromValidated(array $validated): self
    {
        return new self(
            name: trim((string) $validated['name']),
            description: self::nullableString($validated['description'] ?? null),
            isActive: (bool) ($validated['is_active'] ?? false),
            permissionIds: array_map('intval', $validated['permission_ids'] ?? []),
        );
    }

    private static function nullableString(mixed $value): ?string
    {
        $text = trim((string) $value);

        return $text !== '' ? $text : null;
    }
}
