using System;
using System.ComponentModel.DataAnnotations;
using FreeSql.DataAnnotations;
using ZhonTai.Admin.Core.Entities;
using NPP.SmartSchedue.Api.Contracts.Core.Consts;
using NPP.SmartSchedue.Api.Contracts.Core.Enums;

namespace NPP.SmartSchedue.Api.Contracts.Domain.Notification;

/// <summary>
/// 通知设置实体
/// 根据决策点5，通知设置不支持多租户，继承EntityBase
/// </summary>
[Table(Name = DbConsts.TableNamePrefix + "notification_setting")]
public partial class NotificationSettingEntity : EntityBase
{
    #region 基础信息
    
    /// <summary>
    /// 通知名称
    /// </summary>
    [Column(StringLength = 200)]
    [Required(ErrorMessage = "通知名称不能为空")]
    public string NotificationName { get; set; } = "";

    /// <summary>
    /// 通知描述
    /// </summary>
    [Column(StringLength = 500)]
    public string Description { get; set; } = "";

    /// <summary>
    /// 是否启用
    /// </summary>
    public bool IsEnabled { get; set; } = true;

    #endregion

    #region 通知方式配置（决策点1：支持邮件和系统消息）
    
    /// <summary>
    /// 通知方式（支持多选，使用位运算）
    /// </summary>
    public int NotificationTypes { get; set; }

    /// <summary>
    /// 是否启用邮件通知
    /// </summary>
    [Column(MapType = typeof(bool), IsIgnore = true)]
    public bool IsEmailEnabled 
    { 
        get => (NotificationTypes & (int)NotificationTypeEnum.Email) != 0;
        set 
        {
            if (value)
                NotificationTypes |= (int)NotificationTypeEnum.Email;
            else
                NotificationTypes &= ~(int)NotificationTypeEnum.Email;
        }
    }

    /// <summary>
    /// 是否启用系统消息通知
    /// </summary>
    [Column(MapType = typeof(bool), IsIgnore = true)]
    public bool IsSystemMessageEnabled 
    { 
        get => (NotificationTypes & (int)NotificationTypeEnum.SystemMessage) != 0;
        set 
        {
            if (value)
                NotificationTypes |= (int)NotificationTypeEnum.SystemMessage;
            else
                NotificationTypes &= ~(int)NotificationTypeEnum.SystemMessage;
        }
    }

    #endregion

    #region 时间配置（决策点2：简单时间段 开始时间-结束时间）
    
    /// <summary>
    /// 通知开始时间（HH:mm格式，如：09:00）
    /// </summary>
    [Column(StringLength = 10)]
    [Required(ErrorMessage = "通知开始时间不能为空")]
    public string StartTime { get; set; } = "";

    /// <summary>
    /// 通知结束时间（HH:mm格式，如：18:00）
    /// </summary>
    [Column(StringLength = 10)]
    [Required(ErrorMessage = "通知结束时间不能为空")]
    public string EndTime { get; set; } = "";

    #endregion

    #region 频次配置（决策点3：一次性 vs 固定间隔）
    
    /// <summary>
    /// 通知频次类型
    /// </summary>
    public int FrequencyType { get; set; } = (int)NotificationFrequencyEnum.Once;

    /// <summary>
    /// 间隔时间（分钟）
    /// 当FrequencyType为FixedInterval时有效
    /// </summary>
    public int? IntervalMinutes { get; set; }

    #endregion

    #region 人员组配置
    
    /// <summary>
    /// 关联的人员组ID
    /// </summary>
    [Required(ErrorMessage = "人员组不能为空")]
    public long PersonnelGroupId { get; set; }

    #endregion

    #region 模板配置（决策点7：支持模板通知）
    
    /// <summary>
    /// 邮件主题模板
    /// </summary>
    [Column(StringLength = 500)]
    public string EmailSubjectTemplate { get; set; } = "";

    /// <summary>
    /// 邮件内容模板
    /// </summary>
    [Column(DbType = "text")]
    public string EmailContentTemplate { get; set; } = "";

    /// <summary>
    /// 系统消息标题模板
    /// </summary>
    [Column(StringLength = 500)]
    public string SystemMessageTitleTemplate { get; set; } = "";

    /// <summary>
    /// 系统消息内容模板
    /// </summary>
    [Column(DbType = "text")]
    public string SystemMessageContentTemplate { get; set; } = "";

    #endregion

    #region 触发条件
    
    /// <summary>
    /// 触发条件表达式（JSON格式存储）
    /// 用于定义什么条件下触发通知
    /// </summary>
    [Column(DbType = "text")]
    public string TriggerConditions { get; set; } = "";

    #endregion

    #region 创建和修改时间
    
    /// <summary>
    /// 最后修改时间
    /// </summary>
    public DateTime? LastModifiedTime { get; set; }

    #endregion

    #region 导航属性
    
    /// <summary>
    /// 人员组实体
    /// </summary>
    [Navigate("PersonnelGroupId")]
    public PersonnelGroupEntity? PersonnelGroup { get; set; }

    #endregion
}