using CloudBuilder.Core.Authorization;
using CloudBuilder.Core.DatabaseAccessor.Entity;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace CloudBuilder.AI.Entity
 {
    [TypeScript]
    [Table("ai_book")]
    public class AiBookEntity : EntityBase<string>
    {

    public AiBookEntity() 
    {
    }

    [Column("description")]
    public string? Description  { get; set; }
    [Column("book_type")]
    public string? BookType  { get; set; }
    [Column("file_id")]
    public int FileId  { get; set; }
    [Column("status")]
    public string Status  { get; set; }
    [Column("created_by")]
    public string CreatedBy  { get; set; }
    [Column("updated_datetime")]
    public DateTime UpdatedDatetime  { get; set; }
    [Column("updated_by")]
    public string UpdatedBy  { get; set; }
    [Key]
    [Column("guid")]
    public string Guid  { get; set; }
    [Column("title")]
    public string Title  { get; set; }
    [Column("author")]
    public string? Author  { get; set; }
    [Column("created_datetime")]
    public DateTime CreatedDatetime  { get; set; }

    public const string DESCRIPTION = "Description";
    public const string BOOK_TYPE = "BookType";
    public const string FILE_ID = "FileId";
    public const string STATUS = "Status";
    public const string CREATED_BY = "CreatedBy";
    public const string UPDATED_DATETIME = "UpdatedDatetime";
    public const string UPDATED_BY = "UpdatedBy";
    public const string GUID = "Guid";
    public const string TITLE = "Title";
    public const string AUTHOR = "Author";
    public const string CREATED_DATETIME = "CreatedDatetime";

    public const string DB_NAME_AI_BOOK = "ai_book";
    public const string DB_NAME_FIELDS="description,book_type,file_id,status,created_by,updated_datetime,updated_by,guid,title,author,created_datetime";

    public const string DB_DESCRIPTION = "description";
    public const string DB_BOOK_TYPE = "book_type";
    public const string DB_FILE_ID = "file_id";
    public const string DB_STATUS = "status";
    public const string DB_CREATED_BY = "created_by";
    public const string DB_UPDATED_DATETIME = "updated_datetime";
    public const string DB_UPDATED_BY = "updated_by";
    public const string DB_GUID = "guid";
    public const string DB_TITLE = "title";
    public const string DB_AUTHOR = "author";
    public const string DB_CREATED_DATETIME = "created_datetime";


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

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

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

        this.Description =  source.Description;
        this.BookType =  source.BookType;
        this.FileId =  source.FileId;
        this.Status =  source.Status;
        this.CreatedBy =  source.CreatedBy;
        this.UpdatedDatetime =  source.UpdatedDatetime;
        this.UpdatedBy =  source.UpdatedBy;
        this.Guid =  source.Guid;
        this.Title =  source.Title;
        this.Author =  source.Author;
        this.CreatedDatetime =  source.CreatedDatetime;

        if (includeSystemFields)
        {
        this.CreatedBy =  source.CreatedBy;
        this.UpdatedDatetime =  source.UpdatedDatetime;
        this.UpdatedBy =  source.UpdatedBy;
        this.CreatedDatetime =  source.CreatedDatetime;
        }
    }

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

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

        return result;
    }

    }
}
