using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text.Json.Serialization;

namespace Blahblah.FlowerStory.Server.Data.Model;

/// <summary>
/// 用户对象
/// </summary>
[Table("users")]
public class UserItem
{
    /// <summary>
    /// 自增 id，主键
    /// </summary>
    [Column("uid")]
    [Key]
    [Required]
    public int Id { get; set; }

    /// <summary>
    /// 用户 id
    /// </summary>
    [Column("id")]
    [Required]
    public required string UserId { get; set; }

    /// <summary>
    /// 密码，值为 SHA256(password+id+salt)
    /// </summary>
    [Column("password")]
    [Required]
    [JsonIgnore]
    public string? Password { get; set; }

    /// <summary>
    /// 用户级别
    ///   -1: Disabled
    ///    0: Common
    ///   99: Admin
    /// </summary>
    [Column("level")]
    [Required]
    public required int Level { get; set; }

    /// <summary>
    /// 注册时间
    /// </summary>
    [Column("regdate")]
    [Required]
    [JsonPropertyName("registerDate")]
    public required long RegisterDateUnixTime { get; set; }

    /// <summary>
    /// 最后变动时间
    /// </summary>
    [Column("activedate")]
    [JsonIgnore]
    public long? ActiveDateUnixTime { get; set; }

    /// <summary>
    /// 用户名
    /// </summary>
    [Column("name")]
    [Required]
    public required string Name { get; set; }

    /// <summary>
    /// 邮箱
    /// </summary>
    [Column("email")]
    public string? Email { get; set; }

    /// <summary>
    /// 联系电话
    /// </summary>
    [Column("mobile")]
    public string? Mobile { get; set; }

    /// <summary>
    /// 用户头像
    /// </summary>
    [Column("avatar")]
    [JsonIgnore]
    public byte[]? Avatar { get; set; }

    /// <summary>
    /// 注册时间
    /// </summary>
    [NotMapped]
    [JsonIgnore]
    public DateTimeOffset RegisterDate => DateTimeOffset.FromUnixTimeMilliseconds(RegisterDateUnixTime);

    /// <summary>
    /// 最后变动时间
    /// </summary>
    [NotMapped]
    [JsonIgnore]
    public DateTimeOffset? ActiveDate => ActiveDateUnixTime == null ? null : DateTimeOffset.FromUnixTimeMilliseconds(ActiveDateUnixTime.Value);
}
