using FreeSql.DataAnnotations;
using BookmarkApi.Shared.Enums;

namespace BookmarkApi.Data.Entities;

/// <summary>
/// 用户实体
/// </summary>
[Table(Name = "users")]
[Index("idx_user_email", nameof(Email), IsUnique = true)]
public class User
{
    /// <summary>
    /// 用户ID
    /// </summary>
    [Column(IsPrimary = true)]
    public Guid Id { get; set; }

    /// <summary>
    /// 邮箱（登录账号）
    /// </summary>
    [Column(IsNullable = false, StringLength = 200)]
    public string Email { get; set; } = string.Empty;

    /// <summary>
    /// 用户名（显示名称）
    /// </summary>
    [Column(IsNullable = false, StringLength = 100)]
    public string UserName { get; set; } = string.Empty;

    /// <summary>
    /// 密码哈希
    /// </summary>
    [Column(IsNullable = false, StringLength = 500)]
    public string PasswordHash { get; set; } = string.Empty;

    /// <summary>
    /// 用户头像URL
    /// </summary>
    [Column(StringLength = 500)]
    public string? Avatar { get; set; }

    /// <summary>
    /// 管理员角色
    /// </summary>
    [Column(MapType = typeof(int))]
    public AdminRole Role { get; set; } = AdminRole.User;

    /// <summary>
    /// 创建时间
    /// </summary>
    [Column(ServerTime = DateTimeKind.Utc, CanUpdate = false)]
    public DateTime CreatedAt { get; set; }

    /// <summary>
    /// 最后登录时间
    /// </summary>
    public DateTime? LastLoginAt { get; set; }

    /// <summary>
    /// 用户状态
    /// </summary>
    [Column(MapType = typeof(int))]
    public UserStatus Status { get; set; } = UserStatus.Normal;

    /// <summary>
    /// 导航属性 - 用户的设备列表
    /// </summary>
    [Navigate(nameof(Device.UserId))]
    public virtual List<Device>? Devices { get; set; }

    /// <summary>
    /// 导航属性 - 用户的书签列表
    /// </summary>
    [Navigate(nameof(Bookmark.UserId))]
    public virtual List<Bookmark>? Bookmarks { get; set; }

    /// <summary>
    /// 导航属性 - 用户的刷新令牌列表
    /// </summary>
    [Navigate(nameof(RefreshToken.UserId))]
    public virtual List<RefreshToken>? RefreshTokens { get; set; }
}

