using Hotline.Orders;
using Hotline.Share.Dtos.FlowEngine.Workflow;
using Hotline.Share.Enums.FlowEngine;
using SqlSugar;
using XF.Domain.Entities;
using XF.Domain.Exceptions;
using XF.Domain.Repository;

namespace Hotline.FlowEngine.Workflows
{
    [SugarIndex("index_wfcs_workflowId", nameof(WorkflowStep.WorkflowId), OrderByType.Desc)]
    public class WorkflowCountersign : CreationEntity
    {
        public string WorkflowId { get; set; }


        #region 发起会签

        /// <summary>
        /// 发起会签节点id（从开始节点直接发起的会签，该id和code为start节点的id与code）
        /// </summary>
        public string StartStepId { get; set; }

        //冗余
        public string StartStepCode { get; set; }

        /// <summary>
        /// 业务属性
        /// </summary>
        public EBusinessType StartStepBusiType { get; set; }

        /// <summary>
        /// 发起人
        /// </summary>
        public string? StarterId { get; set; }

        public string? StarterName { get; set; }

        /// <summary>
        /// 发起人部门code
        /// </summary>
        public string? StarterOrgId { get; set; }

        public string? StarterOrgName { get; set; }

        /// <summary>
        /// 发起人部门行政区划代码
        /// </summary>
        public string? StarterOrgAreaCode { get; set; }

        /// <summary>
        /// 发起人部门行政区划名称
        /// </summary>
        public string? StarterOrgAreaName { get; set; }

        #endregion

        #region 结束会签

        /// <summary>
        /// 会签汇总节点id
        /// 人工结束会签为null
        /// </summary>
        public string? EndStepId { get; set; }

        //冗余
        public string? EndStepCode { get; set; }

        /// <summary>
        /// 业务属性
        /// </summary>
        public EBusinessType EndStepBusiType { get; set; }

        /// <summary>
        /// 接办人
        /// </summary>
        public string? FinisherId { get; set; }

        public string? FinisherName { get; set; }

        /// <summary>
        /// 接办人部门code
        /// </summary>
        public string? FinisherOrgId { get; set; }

        public string? FinisherOrgName { get; set; }

        /// <summary>
        /// 接办人部门行政区划代码
        /// </summary>
        public string? FinisherOrgAreaCode { get; set; }

        /// <summary>
        /// 接办人部门行政区划名称
        /// </summary>
        public string? FinisherOrgAreaName { get; set; }

        /// <summary>
        /// 会签结束时间
        /// </summary>
        public DateTime? EndTime { get; set; }

        #endregion

        /// <summary>
        /// 会签嵌套会签场景记录上级会签Id
        /// </summary>
        public string? ParentId { get; set; }

        /// <summary>
        /// 会签类型(非工单办理为null)
        /// </summary>
        public ECounterSignType? CounterSignType { get; set; }

        /// <summary>
        /// 该会签参与成员数量
        /// </summary>
        // public int Members { get; set; }
        [Navigate(NavigateType.OneToMany, nameof(WorkflowCountersignMember.WorkflowCountersignId))]
        public List<WorkflowCountersignMember> Members { get; set; }

        /// <summary>
        /// 流程指派类型(冗余,非工单办理为null)
        /// </summary>
        public EFlowAssignType? FlowAssignType { get; set; }

        /// <summary>
        /// 超期时间（期满时间）
        /// </summary>
        public DateTime? ExpiredTime { get; set; }

        /// <summary>
        /// 是否超期
        /// </summary>
        public bool? IsExpired { get; set; }


        [SugarColumn(IsIgnore = true)]
        public List<WorkflowCountersign> Children { get; set; }

        [SugarColumn(IsIgnore = true)]
		public Order Order { get; set; }

		#region method

		/// <summary>
		/// 会签是否完成(如有嵌套会签，表示下级所有会签都完成了)
		/// </summary>
		public bool IsCompleted() => EndTime.HasValue;

        /// <summary>
        /// 结束会签（需提前确认所有子集都已结束）
        /// </summary>
        public void End(
            string endStepId, string endStepCode, EBusinessType businessType,
            string userId, string? userName,
            string orgId, string? orgName,
            string? orgAreaCode, string? orgAreaName)
        {
            EndStepId = endStepId;
            EndStepCode = endStepCode;
            EndStepBusiType = businessType;
            FinisherId = userId;
            FinisherName = userName;
            FinisherOrgId = orgId;
            FinisherOrgName = orgName;
            FinisherOrgAreaCode = orgAreaCode;
            FinisherOrgAreaName = orgAreaName;
            EndTime = DateTime.Now;
            IsExpired = EndTime.HasValue && ExpiredTime.HasValue && EndTime > ExpiredTime;
        }

        public void MemberHandled(string userId, string orgId)
        {
            var member = FlowAssignType == EFlowAssignType.Org
                ? Members.FirstOrDefault(d => d.Key == orgId)
                : Members.FirstOrDefault(d => d.Key == userId);

            if (member is null)
                throw new UserFriendlyException($"未找到会签办理对象");
            member.IsHandled = true;
        }

        #endregion
    }
}