using CloudBuilder.Core.Authorization;
using CloudBuilder.Core.DatabaseAccessor.Entity;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace CloudBuilder.Security.Entity
 {
    [TypeScript]
    [Table("security_user_web_login")]
    public class SecurityUserWebLoginEntity : EntityBase<SecurityUserWebLoginEntityKey>
    {

    public SecurityUserWebLoginEntity() 
    {
    }

    [Column("session_id")]
    public string SessionId  { get; set; }
    [Column("username")]
    public string Username  { get; set; }
    [Column("host_ip")]
    public string? HostIp  { get; set; }
    [Column("host_user_agent")]
    public string? HostUserAgent  { get; set; }
    [Column("login_time")]
    public DateTime LoginTime  { get; set; }
    [Column("logout_time")]
    public Nullable<DateTime> LogoutTime  { get; set; }
    [Column("refresh_token")]
    public string RefreshToken  { get; set; }
    [Column("refresh_token_refreshed_time")]
    public DateTime RefreshTokenRefreshedTime  { get; set; }

    public const string SESSION_ID = "SessionId";
    public const string USERNAME = "Username";
    public const string HOST_IP = "HostIp";
    public const string HOST_USER_AGENT = "HostUserAgent";
    public const string LOGIN_TIME = "LoginTime";
    public const string LOGOUT_TIME = "LogoutTime";
    public const string REFRESH_TOKEN = "RefreshToken";
    public const string REFRESH_TOKEN_REFRESHED_TIME = "RefreshTokenRefreshedTime";

    public const string DB_NAME_SECURITY_USER_WEB_LOGIN = "security_user_web_login";
    public const string DB_NAME_FIELDS="session_id,username,host_ip,host_user_agent,login_time,logout_time,refresh_token,refresh_token_refreshed_time";

    public const string DB_SESSION_ID = "session_id";
    public const string DB_USERNAME = "username";
    public const string DB_HOST_IP = "host_ip";
    public const string DB_HOST_USER_AGENT = "host_user_agent";
    public const string DB_LOGIN_TIME = "login_time";
    public const string DB_LOGOUT_TIME = "logout_time";
    public const string DB_REFRESH_TOKEN = "refresh_token";
    public const string DB_REFRESH_TOKEN_REFRESHED_TIME = "refresh_token_refreshed_time";


    public static SecurityUserWebLoginEntity GetInstance()
    {
        return new SecurityUserWebLoginEntity();
    }

    public virtual void CopyFrom(SecurityUserWebLoginEntity source)
    {
        CopyFrom(source, true);
    }

    public virtual void CopyFrom(SecurityUserWebLoginEntity source, bool includeSystemFields)
    {
        if (source == null)
        {
            throw new NullReferenceException("Source entity is null.");
        }

        this.SessionId =  source.SessionId;
        this.Username =  source.Username;
        this.HostIp =  source.HostIp;
        this.HostUserAgent =  source.HostUserAgent;
        this.LoginTime =  source.LoginTime;
        this.LogoutTime =  source.LogoutTime;
        this.RefreshToken =  source.RefreshToken;
        this.RefreshTokenRefreshedTime =  source.RefreshTokenRefreshedTime;

        if (includeSystemFields)
        {
        }
    }

    public override string GetSelectFrom()
    {
        return $" select {DB_NAME_FIELDS} from { DB_NAME_SECURITY_USER_WEB_LOGIN}  with(nolock) ";
    }
	
    public override string GetSelectCountFrom(string sqlWhere)
    {
        return $" select count(*) from { DB_NAME_SECURITY_USER_WEB_LOGIN}  with(nolock) where 1=1 {sqlWhere}";
    }
	
    public override string GetSelectTopFrom(int limit, int start,string orderby,string orderbytag,  string sqlWhere)
    {
            if (string.IsNullOrEmpty(orderbytag)  || orderbytag.Trim() != "desc") orderbytag = "asc";
            if (string.IsNullOrEmpty(orderby) || !DB_NAME_FIELDS.Contains(orderby)) orderby =  DB_SESSION_ID ;  		
		string top = limit > 0 ? $"top {limit}" : "";
        return $" select {top} {DB_NAME_FIELDS} from (select {DB_NAME_FIELDS}  ,row_number() over(order by {orderby} {orderbytag}) as num from { DB_NAME_SECURITY_USER_WEB_LOGIN}  with(nolock) where 1=1 {sqlWhere} ) a where num> {limit * (start - 1)}";
    }

    public SecurityUserWebLoginEntity Clone()
    {
        SecurityUserWebLoginEntity result = new SecurityUserWebLoginEntity();
        result.CopyFrom(this);

        return result;
    }

    }
}
