using CloudBuilder.Core.Authorization;
using CloudBuilder.Core.DatabaseAccessor.Entity;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace CloudBuilder.Security.Entity
 {
    [TypeScript]
    [Table("sys_table_schema_view")]
    public class SysTableSchemaViewEntity : ViewEntityBase
    {

    public SysTableSchemaViewEntity() 
    {
    }

    [Column("table_name")]
    public string? TableName  { get; set; }
    [Column("field_name")]
    public string? FieldName  { get; set; }
    [Column("type_name")]
    public string? TypeName  { get; set; }
    [Column("type_of_name")]
    public string? TypeOfName  { get; set; }
    [Column("max_length")]
    public int MaxLength  { get; set; }
    [Column("scale")]
    public int Scale  { get; set; }
    [Column("is_null_able")]
    public string IsNullAble  { get; set; }
    [Column("field_default_value")]
    public string FieldDefaultValue  { get; set; }
    [Column("xtype")]
    public string? Xtype  { get; set; }
    [Column("find_by")]
    public int FindBy  { get; set; }
    [Column("operation")]
    public int Operation  { get; set; }
    [Column("pk")]
    public int Pk  { get; set; }
    [Column("pk_name")]
    public string PkName  { get; set; }
    [Column("is_identity")]
    public int IsIdentity  { get; set; }
    [Column("colid")]
    public int Colid  { get; set; }

    public const string TABLE_NAME = "TableName";
    public const string FIELD_NAME = "FieldName";
    public const string TYPE_NAME = "TypeName";
    public const string TYPE_OF_NAME = "TypeOfName";
    public const string MAX_LENGTH = "MaxLength";
    public const string SCALE = "Scale";
    public const string IS_NULL_ABLE = "IsNullAble";
    public const string FIELD_DEFAULT_VALUE = "FieldDefaultValue";
    public const string XTYPE = "Xtype";
    public const string FIND_BY = "FindBy";
    public const string OPERATION = "Operation";
    public const string PK = "Pk";
    public const string PK_NAME = "PkName";
    public const string IS_IDENTITY = "IsIdentity";
    public const string COLID = "Colid";

    public const string DB_NAME_SYS_TABLE_SCHEMA_VIEW = "sys_table_schema_view";
    public const string DB_NAME_FIELDS="table_name,field_name,type_name,type_of_name,max_length,scale,is_null_able,field_default_value,xtype,find_by,operation,pk,pk_name,is_identity,colid";

    public const string DB_TABLE_NAME = "table_name";
    public const string DB_FIELD_NAME = "field_name";
    public const string DB_TYPE_NAME = "type_name";
    public const string DB_TYPE_OF_NAME = "type_of_name";
    public const string DB_MAX_LENGTH = "max_length";
    public const string DB_SCALE = "scale";
    public const string DB_IS_NULL_ABLE = "is_null_able";
    public const string DB_FIELD_DEFAULT_VALUE = "field_default_value";
    public const string DB_XTYPE = "xtype";
    public const string DB_FIND_BY = "find_by";
    public const string DB_OPERATION = "operation";
    public const string DB_PK = "pk";
    public const string DB_PK_NAME = "pk_name";
    public const string DB_IS_IDENTITY = "is_identity";
    public const string DB_COLID = "colid";


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

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

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

        this.TableName =  source.TableName;
        this.FieldName =  source.FieldName;
        this.TypeName =  source.TypeName;
        this.TypeOfName =  source.TypeOfName;
        this.MaxLength =  source.MaxLength;
        this.Scale =  source.Scale;
        this.IsNullAble =  source.IsNullAble;
        this.FieldDefaultValue =  source.FieldDefaultValue;
        this.Xtype =  source.Xtype;
        this.FindBy =  source.FindBy;
        this.Operation =  source.Operation;
        this.Pk =  source.Pk;
        this.PkName =  source.PkName;
        this.IsIdentity =  source.IsIdentity;
        this.Colid =  source.Colid;

        if (includeSystemFields)
        {
        }
    }

    public override string GetSelectFrom()
    {
        return $" select {DB_NAME_FIELDS} from { DB_NAME_SYS_TABLE_SCHEMA_VIEW}  with(nolock) ";
    }
	
    public override string GetSelectCountFrom(string sqlWhere)
    {
        return $" select count(*) from { DB_NAME_SYS_TABLE_SCHEMA_VIEW}  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_TABLE_NAME ;  		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_SYS_TABLE_SCHEMA_VIEW}  with(nolock) where 1=1 {sqlWhere} ) a where num> {limit * (start - 1)}";
    }

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

        return result;
    }

    }
}
