namespace GermanApp.Domain.Entities;
///
/// Represents a user in the DeutschLernen system.
///
public class User
{
public int Id { get; private set; }
public string Username { get; private set; } = string.Empty;
public string Email { get; private set; } = string.Empty;
public string PasswordHash { get; private set; } = string.Empty;
public string CurrentLevel { get; private set; } = "A1";
public string Role { get; private set; } = "User"; // Default role is "User", "Admin" for administrators
public int Streak { get; private set; }
public int TotalPoints { get; private set; }
public DateTime CreatedAt { get; private set; }
///
/// Constructor for EF Core deserialization.
///
private User() { }
///
/// Factory method to create a new user.
///
public static User Create(string username, string email, string passwordHash)
{
return new User
{
Username = username,
Email = email.ToLowerInvariant(),
PasswordHash = passwordHash,
CurrentLevel = "A1",
Role = "User", // Explicitly set default role
Streak = 0,
TotalPoints = 0,
CreatedAt = DateTime.UtcNow
};
}
///
/// Updates user's gamification stats.
///
public void AddPoints(int points)
{
TotalPoints += points;
}
///
/// Updates user's streak.
///
public void UpdateStreak(int streak)
{
Streak = streak;
}
///
/// Updates user's current level.
///
public void UpdateLevel(string level)
{
CurrentLevel = level;
}
///
/// Changes user's email.
///
public void ChangeEmail(string newEmail)
{
Email = newEmail?.ToLowerInvariant() ?? string.Empty;
}
///
/// Changes user's password hash.
///
public void ChangePassword(string newPasswordHash)
{
PasswordHash = newPasswordHash;
}
///
/// Assigns admin role to user (bootstrap only).
///
public void AssignAdminRole()
{
Role = "Admin";
}
///
/// Sets the user's role.
///
/// The role to assign (User or Admin)
public void SetRole(string role)
{
if (role != "Admin" && role != "User")
throw new ArgumentException("Role must be 'Admin' or 'User'.", nameof(role));
Role = role;
}
///
/// Checks if user has admin role.
///
public bool IsAdmin() => Role == "Admin";
// Navigation properties
public virtual ICollection StoryProgress { get; private set; } = new List();
}