using System.ComponentModel.DataAnnotations;
using LittleShop.Models;

namespace LittleShop.DTOs;

public class CustomerMessageDto
{
    public Guid Id { get; set; }
    public Guid CustomerId { get; set; }
    public Guid? OrderId { get; set; }
    public Guid? AdminUserId { get; set; }
    public MessageDirection Direction { get; set; }
    public MessageType Type { get; set; }
    public string Subject { get; set; } = string.Empty;
    public string Content { get; set; } = string.Empty;
    public MessageStatus Status { get; set; }
    public DateTime CreatedAt { get; set; }
    public DateTime? SentAt { get; set; }
    public DateTime? DeliveredAt { get; set; }
    public DateTime? ReadAt { get; set; }
    public DateTime? FailedAt { get; set; }
    public string? FailureReason { get; set; }
    public int RetryCount { get; set; }
    public DateTime? NextRetryAt { get; set; }
    public Guid? ParentMessageId { get; set; }
    public Guid? ThreadId { get; set; }
    public string Platform { get; set; } = string.Empty;
    public string? PlatformMessageId { get; set; }
    public int Priority { get; set; }
    public DateTime? ScheduledFor { get; set; }
    public DateTime? ExpiresAt { get; set; }
    public bool RequiresResponse { get; set; }
    public bool IsUrgent { get; set; }
    public bool IsMarketing { get; set; }
    public bool IsAutoGenerated { get; set; }
    public string? AutoGenerationTrigger { get; set; }
    
    // Navigation properties
    public CustomerSummaryDto? Customer { get; set; }
    public string? AdminUsername { get; set; }
    public string? OrderReference { get; set; }
    
    // For message delivery
    public long TelegramUserId { get; set; }
    
    // Helper properties
    public string DisplayTitle { get; set; } = string.Empty;
    public string StatusDisplay { get; set; } = string.Empty;
    public string DirectionDisplay { get; set; } = string.Empty;
    public string TypeDisplay { get; set; } = string.Empty;
}

public class CreateCustomerMessageDto
{
    [Required]
    public Guid CustomerId { get; set; }
    
    public Guid? OrderId { get; set; }
    
    public Guid? AdminUserId { get; set; } // Set by controller from claims
    
    [Required]
    public MessageType Type { get; set; }
    
    [Required]
    [StringLength(100)]
    public string Subject { get; set; } = string.Empty;
    
    [Required]
    [StringLength(4000)]
    public string Content { get; set; } = string.Empty;
    
    public int Priority { get; set; } = 5;
    
    public DateTime? ScheduledFor { get; set; }
    
    public DateTime? ExpiresAt { get; set; }
    
    public bool RequiresResponse { get; set; } = false;
    
    public bool IsUrgent { get; set; } = false;
    
    public bool IsMarketing { get; set; } = false;
}

public class CustomerMessageSummaryDto
{
    public Guid Id { get; set; }
    public Guid CustomerId { get; set; }
    public string CustomerName { get; set; } = string.Empty;
    public Guid? OrderId { get; set; }
    public string? OrderReference { get; set; }
    public MessageDirection Direction { get; set; }
    public MessageType Type { get; set; }
    public string Subject { get; set; } = string.Empty;
    public MessageStatus Status { get; set; }
    public DateTime CreatedAt { get; set; }
    public DateTime? SentAt { get; set; }
    public bool IsUrgent { get; set; }
    public bool RequiresResponse { get; set; }
    public string DisplayTitle { get; set; } = string.Empty;
}

public class MessageThreadDto
{
    public Guid ThreadId { get; set; }
    public string Subject { get; set; } = string.Empty;
    public Guid CustomerId { get; set; }
    public string CustomerName { get; set; } = string.Empty;
    public Guid? OrderId { get; set; }
    public string? OrderReference { get; set; }
    public DateTime StartedAt { get; set; }
    public DateTime LastMessageAt { get; set; }
    public int MessageCount { get; set; }
    public bool HasUnreadMessages { get; set; }
    public bool RequiresResponse { get; set; }
    public List<CustomerMessageDto> Messages { get; set; } = new();
}