using IRaCIS.Core.Domain.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IRaCIS.Core.Domain.Models
{
    [Comment("稽查文档管理")]
    [Table("AuditDocument")]
    public class AuditDocument : BaseFullAuditEntity
    {
        // 0=自己, 1=直接子节点, 2=孙节点...
        [JsonIgnore]
        public List<AuditDocumentClosure> AncestorList { get; set; } = new List<AuditDocumentClosure>();

        [JsonIgnore]
        public List<AuditDocumentClosure> DescendantList { get; set; } = new List<AuditDocumentClosure>();

        [JsonIgnore]
        public List<AuditDocument>   AuditDocumentOldVersionList { get; set; }

        [JsonIgnore]
        [ForeignKey(nameof(MainFileId))]
        public AuditDocument MainAuditDocument { get; set; }
        /// <summary>
        /// 文件夹名或者文件名
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 文档类型
        /// </summary>
        public AuditDocumentType AuditDocumentTypeEnum { get; set; }

        /// <summary>
        /// 父文件夹Id
        /// </summary>
        public Guid? ParentId { get; set; }

        /// <summary>
        /// 文件路径
        /// </summary>
        [StringLength(1000)]
        public string FilePath { get; set; } = string.Empty;

        /// <summary>
        /// 文件大小
        /// </summary>
        public decimal? FileSize { get; set; }

        /// <summary>
        /// 文件类型
        /// </summary>
        public string FileFormat { get; set; } = string.Empty;

        /// <summary>
        /// 版本
        /// </summary>
        public int? Version { get; set; }

        /// <summary>
        /// 主文件Id
        /// </summary>
        public Guid? MainFileId { get; set; }

        /// <summary>
        /// 是否授权
        /// </summary>
        public bool IsAuthorization { get; set; } = false;
    }

    /// <summary>
    /// 稽查文档类型
    /// </summary>
    public enum AuditDocumentType
    {
        /// <summary>
        /// 文件夹
        /// </summary>
        Folder = 0,

        /// <summary>
        /// 文件
        /// </summary>
        File = 1,

        /// <summary>
        /// 历史版本
        /// </summary>
        HistoricalVersion = 2,

    }

    /// <summary>
    /// 稽查文档闭包表
    /// </summary>
    public class AuditDocumentClosure : Entity
    {
        [JsonIgnore]

        public AuditDocument Ancestor { get; set; }

        [JsonIgnore]

        public AuditDocument Descendant { get; set; }

        /// <summary>
        /// 父Id
        /// </summary>
        [Comment("祖先")]
        public Guid AncestorId { get; set; }

        /// <summary>
        /// 子Id
        /// </summary>
        [Comment("后代")]
        public Guid DescendantId { get; set; }
        public int Depth { get; set; } // 0=自己, 1=直接子节点, 2=孙节点...
    }


    public class AuditRecord : BaseFullAuditEntity
    {
        [JsonIgnore]
        public List<AuditRecordIdentityUser> AuditRecordIdentityUserList { get; set; }

        public string CompanyName { get; set; }

        public string AuditContent { get; set; }

        [Comment("稽查日期")]
        public DateOnly AuditTime { get; set; }


        public DateTime? BeginTime { get; set; }

        public DateTime? EndTime { get; set; }

        [Comment("稽查状态")]
        public AuditState AuditState { get; set; }

        [Comment("稽查形式")]
        public AuditType AuditType { get; set; }

        public bool IsViewTrainingRecord { get; set; }

    }


    public class AuditRecordIdentityUser : BaseAddAuditEntity
    {
        [JsonIgnore]
        public IdentityUser IdentityUser { get; set; }

        [JsonIgnore]
        public AuditRecord AuditRecord { get; set; }



        public Guid IdentityUserId { get; set; }

        public Guid AuditRecordId { get; set; }
    }


    public class AuditRecordPermission : BaseAddAuditEntity
    {
        [JsonIgnore]
        public AuditRecord AuditRecord { get; set; }

        [JsonIgnore]
        public AuditDocument AuditDocument { get; set; }


        public Guid AuditRecordId { get; set; }


        public Guid AuditDocumentId { get; set; }
    }


    public enum AuditState
    {
        NotStart = 0,

        Ongoing = 1,

        End = 2

    }

    public enum AuditType
    {
        None = 0,

        Online = 1,

        OnSite = 2

    }
}
