using back.DTO; using MCVIngenieros.Transactional.Abstractions.Interfaces; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace back.DataModels; [Table("Users")] public class User : IEntity { [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] public string Id { get; set; } = null!; [Required, EmailAddress] public string Email { get; set; } = null!; [Required, MinLength(8)] public string Password { get; set; } = null!; [Required] public string Salt { get; set; } = null!; public string CreatedAt { get; set; } = null!; public virtual Person IdNavigation { get; set; } = null!; public virtual ICollection Galleries { get; set; } = []; public virtual ICollection GalleriesNavigation { get; set; } = []; public virtual ICollection Photos { get; set; } = []; public virtual ICollection Roles { get; set; } = []; public User() { } public User(string id, string email, string password, DateTimeOffset createdAt) { Id = id; Email = email; Password = password; CreatedAt = createdAt.ToString("dd-MM-yyyy HH:mm:ss zz"); } public UserDto ToDto() => new() { Id = Id, Roles = [.. Roles.Select(r => r.ToDto())] }; public bool IsAdmin() => Roles.Any(r => r.IsAdmin()); public bool IsContentManager() => Roles.Any(r => r.IsContentManager()); public bool IsUser() => Roles.Any(r => r.IsUser()); public override int GetHashCode() => HashCode.Combine(Id, Email); public override bool Equals(object? obj) => obj is User otherEvent && Equals(otherEvent); public bool Equals(User? other) { if (other is null) return false; if (ReferenceEquals(this, other)) return true; return Id == other.Id && Email == other.Email; } public bool IsNull => this is null; public object Clone() => (User)MemberwiseClone(); public int CompareTo(object? obj) { if (obj is null) return 1; if (obj is not User other) throw new ArgumentException("Object is not a Person"); return CompareTo(other); } public int CompareTo(User? other) { if (other is null) return 1; if (ReferenceEquals(this, other)) return 0; return string.Compare(Id, other.Id, StringComparison.OrdinalIgnoreCase); } public const string SystemUserId = "00000000-0000-0000-0000-000000000001"; public static readonly User SystemUser = new( id: SystemUserId, email: "sys@t.em", password: "", createdAt: DateTime.UtcNow ) { Roles = [Role.AdminRole, Role.ContentManagerRole, Role.UserRole] }; }