using SCM.Shared.Constants; using SCM.Shared.Exceptions; namespace SCM.Domain.Users; /// Usuário do sistema com um papel (RBAC). Setters públicos para hidratação via Dapper. public sealed class User { public Guid Id { get; set; } public string Username { get; set; } = string.Empty; public string Email { get; set; } = string.Empty; public string PasswordHash { get; set; } = string.Empty; public string Role { get; set; } = Roles.Leitor; public bool IsActive { get; set; } = true; public DateTime CreatedAt { get; set; } public static User Create(string username, string email, string role, string passwordHash, DateTime agora) { return new User { Id = Guid.NewGuid(), Username = NormalizeUsername(username), Email = NormalizeEmail(email), Role = NormalizeRole(role), PasswordHash = passwordHash, IsActive = true, CreatedAt = agora }; } public void Atualizar(string email, string role) { Email = NormalizeEmail(email); Role = NormalizeRole(role); } public void DefinirAtivo(bool ativo) => IsActive = ativo; public void RedefinirSenha(string passwordHash) { if (string.IsNullOrWhiteSpace(passwordHash)) throw new DomainException("O hash da senha é obrigatório."); PasswordHash = passwordHash; } private static string NormalizeUsername(string v) { if (string.IsNullOrWhiteSpace(v)) throw new DomainException("O usuário é obrigatório."); return v.Trim(); } private static string NormalizeEmail(string v) { if (string.IsNullOrWhiteSpace(v) || !v.Contains('@')) throw new DomainException("E-mail inválido."); return v.Trim(); } private static string NormalizeRole(string v) { if (!Roles.All.Contains(v)) throw new DomainException($"Papel inválido. Use um de: {string.Join(", ", Roles.All)}."); return v; } }