using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;

namespace FutureMailAPI.DTOs
{
    public class SentMailCreateDto
    {
        [Required(ErrorMessage = "标题是必填项")]
        [StringLength(200, ErrorMessage = "标题长度不能超过200个字符")]
        public string Title { get; set; } = string.Empty;
        
        [Required(ErrorMessage = "内容是必填项")]
        public string Content { get; set; } = string.Empty;
        
        // 收件人类型: 0-自己, 1-指定用户, 2-公开时间胶囊
        [Required(ErrorMessage = "收件人类型是必填项")]
        [Range(0, 2, ErrorMessage = "收件人类型必须是0、1或2")]
        public int RecipientType { get; set; }
        
        // 如果是指定用户，提供用户邮箱
        public string? RecipientEmail { get; set; }
        
        [Required(ErrorMessage = "投递时间是必填项")]
        public DateTime DeliveryTime { get; set; }
        
        // 触发条件类型: 0-时间, 1-地点, 2-事件
        [Required(ErrorMessage = "触发条件类型是必填项")]
        [Range(0, 2, ErrorMessage = "触发条件类型必须是0、1或2")]
        public int TriggerType { get; set; } = 0;
        
        // 触发条件详情(JSON格式)
        public string? TriggerDetails { get; set; }
        
        // 附件路径(JSON数组格式)
        public string? Attachments { get; set; }
        
        // 是否加密
        public bool IsEncrypted { get; set; } = false;
        
        // 加密密钥(如果使用端到端加密)
        public string? EncryptionKey { get; set; }
        
        // 邮件主题/胶囊皮肤
        [StringLength(50, ErrorMessage = "主题长度不能超过50个字符")]
        public string? Theme { get; set; }
    }
    
    // 兼容前端请求格式的DTO
    public class SentMailCreateCompatDto
    {
        [Required(ErrorMessage = "标题是必填项")]
        [StringLength(200, ErrorMessage = "标题长度不能超过200个字符")]
        public string title { get; set; } = string.Empty;
        
        [Required(ErrorMessage = "内容是必填项")]
        public string content { get; set; } = string.Empty;
        
        [Required(ErrorMessage = "收件人类型是必填项")]
        [JsonConverter(typeof(JsonStringEnumConverter))]
        public RecipientTypeEnum recipientType { get; set; }
        
        public string? recipientEmail { get; set; }
        
        [Required(ErrorMessage = "投递时间是必填项")]
        public DateTime sendTime { get; set; }
        
        [Required(ErrorMessage = "触发条件类型是必填项")]
        [JsonConverter(typeof(JsonStringEnumConverter))]
        public TriggerTypeEnum triggerType { get; set; }
        
        public object? triggerCondition { get; set; }
        
        public List<object>? attachments { get; set; }
        
        public bool isEncrypted { get; set; } = false;
        
        public string capsuleStyle { get; set; } = "default";
        
        // 转换为内部DTO
        public SentMailCreateDto ToInternalDto()
        {
            var dto = new SentMailCreateDto
            {
                Title = this.title,
                Content = this.content,
                RecipientType = (int)this.recipientType,
                RecipientEmail = this.recipientEmail,
                DeliveryTime = this.sendTime,
                TriggerType = (int)this.triggerType,
                IsEncrypted = this.isEncrypted,
                Theme = this.capsuleStyle
            };
            
            // 处理触发条件
            if (this.triggerCondition != null)
            {
                dto.TriggerDetails = System.Text.Json.JsonSerializer.Serialize(this.triggerCondition);
            }
            
            // 处理附件
            if (this.attachments != null && this.attachments.Count > 0)
            {
                dto.Attachments = System.Text.Json.JsonSerializer.Serialize(this.attachments);
            }
            
            return dto;
        }
    }
    
    public enum RecipientTypeEnum
    {
        SELF = 0,
        SPECIFIC = 1,
        PUBLIC = 2
    }
    
    public enum TriggerTypeEnum
    {
        TIME = 0,
        LOCATION = 1,
        EVENT = 2
    }
    
    public class SentMailUpdateDto
    {
        [StringLength(200, ErrorMessage = "标题长度不能超过200个字符")]
        public string? Title { get; set; }
        
        public string? Content { get; set; }
        
        public DateTime? DeliveryTime { get; set; }
        
        public string? TriggerDetails { get; set; }
        
        public string? Attachments { get; set; }
        
        public string? Theme { get; set; }
    }
    
    public class SentMailResponseDto
    {
        public int Id { get; set; }
        public string Title { get; set; } = string.Empty;
        public string Content { get; set; } = string.Empty;
        public int SenderId { get; set; }
        public string SenderUsername { get; set; } = string.Empty;
        public int RecipientType { get; set; }
        public int? RecipientId { get; set; }
        public string? RecipientUsername { get; set; }
        public DateTime SentAt { get; set; }
        public DateTime DeliveryTime { get; set; }
        public int Status { get; set; }
        public int TriggerType { get; set; }
        public string? TriggerDetails { get; set; }
        public string? Attachments { get; set; }
        public bool IsEncrypted { get; set; }
        public string? Theme { get; set; }
        
        // 计算属性
        public string StatusText { get; set; } = string.Empty;
        public string RecipientTypeText { get; set; } = string.Empty;
        public string TriggerTypeText { get; set; } = string.Empty;
        public int DaysUntilDelivery { get; set; }
    }
    
    public class ReceivedMailResponseDto
    {
        public int Id { get; set; }
        public int SentMailId { get; set; }
        public string Title { get; set; } = string.Empty;
        public string Content { get; set; } = string.Empty;
        public string SenderUsername { get; set; } = string.Empty;
        public DateTime SentAt { get; set; }
        public DateTime ReceivedAt { get; set; }
        public bool IsRead { get; set; }
        public DateTime? ReadAt { get; set; }
        public bool IsReplied { get; set; }
        public int? ReplyMailId { get; set; }
        public string? Theme { get; set; }
    }
    
    public class MailListQueryDto
    {
        public int PageIndex { get; set; } = 1;
        public int PageSize { get; set; } = 10;
        public int? Status { get; set; }
        public int? RecipientType { get; set; }
        public string? Keyword { get; set; }
        public DateTime? StartDate { get; set; }
        public DateTime? EndDate { get; set; }
    }
}