using Microsoft.Win32.SafeHandles;
using NPin.Framework.SqlSugarCore.Abstractions.Data;
using NPin.Framework.Upms.Domain.Shared.Enums;
using SqlSugar;
using Volo.Abp.Auditing;
using Volo.Abp.Domain.Entities;

namespace NPin.Framework.Upms.Domain.Entities;

[SugarTable("User")]
public class UserEntity : Entity<Guid>, ISoftDelete, IAuditedObject, IEnabled, IOrderNum
{
    #region User

    [SugarColumn(IsPrimaryKey = true)] public override Guid Id { get; protected set; }

    [SugarColumn(ColumnDescription = "用户名")]
    public string Username { get; set; } = string.Empty;

    [SugarColumn(ColumnDescription = "手机号码")]
    public string PhoneNumber { get; set; }

    [SugarColumn(ColumnDescription = "邮箱")]
    public string? Email { get; set; }

    [SugarColumn(ColumnDescription = "昵称")]
    public string? Nickname { get; set; }

    [SugarColumn(ColumnDescription = "密码")]
    public string Password { get; set; } = string.Empty;

    [SugarColumn(ColumnDescription = "简介")]
    public string? Introduction { get; set; }

    [SugarColumn(ColumnDescription = "性别")]
    public GenderEnum Gender { get; set; } = GenderEnum.Secrecy;

    [SugarColumn(ColumnDescription = "注册IP地址")]
    public string? IpAddr { get; set; }

    [SugarColumn(ColumnDescription = "头像")]
    public string? Avatar { get; set; }

    #endregion

    #region Implements

    /// <summary>
    /// 逻辑删除
    /// </summary>
    public bool IsDeleted { get; }

    public DateTime CreationTime { get; }
    public Guid? CreatorId { get; }
    public DateTime? LastModificationTime { get; }
    public Guid? LastModifierId { get; }

    /// <summary>
    /// 是否启用
    /// </summary>
    public bool IsEnabled { get; set; }

    public int OrderNum { get; set; }

    #endregion

    #region 关联关系（导航）

    #endregion

    public UserEntity()
    {
    }

    public UserEntity(string username, string phoneNumber, string password, string? nickname)
    {
        Username = username;
        PhoneNumber = phoneNumber;
        if (username.IsNullOrEmpty())
        {
            username = $"用户{phoneNumber}";
        }
        Nickname = nickname.IsNullOrWhiteSpace() ? username : nickname;
        
        // Password
        // SafeNCryptHandle
    }
}