using System.ComponentModel.DataAnnotations;

namespace Database.Models
{
    public class FogBugzCase : BaseEntity
    {
        [Required]
        public int CaseId { get; set; }
        
        [Required]
        [MaxLength(500)]
        public string Title { get; set; } = string.Empty;
        
        [MaxLength(200)]
        public string? Project { get; set; }
        
        [MaxLength(200)]
        public string? Area { get; set; }
        
        [Required]
        [MaxLength(50)]
        public string Status { get; set; } = "Unknown";
        
        public DateTime LastUpdated { get; set; }
        
        public bool IsOpen { get; set; } = true;
        
        // Navigation properties
        public ICollection<FogBugzEvent> Events { get; set; } = new List<FogBugzEvent>();
        
        // Helper methods
        public string GetLatestReleaseNote()
        {
            return Events
                .Where(e => e.EventType == "ReleaseNoted" && !string.IsNullOrEmpty(e.ReleaseNote))
                .OrderByDescending(e => e.EventDate)
                .FirstOrDefault()?.ReleaseNote ?? string.Empty;
        }
        
        public int? GetZendeskTicketNumber()
        {
            return Events
                .Where(e => e.ZendeskNumber > 0)
                .OrderByDescending(e => e.EventDate)
                .FirstOrDefault()?.ZendeskNumber;
        }
        
        public FogBugzStatus GetMappedStatus()
        {
            return Status.ToLower() switch
            {
                var s when s.StartsWith("open") => FogBugzStatus.Opened,
                var s when s.StartsWith("resolved") => FogBugzStatus.Resolved,
                var s when s.StartsWith("closed") => FogBugzStatus.Closed,
                var s when s.StartsWith("reactivated") => FogBugzStatus.Reactivated,
                _ => FogBugzStatus.Unknown
            };
        }
    }
    
    public enum FogBugzStatus
    {
        Unknown,
        Opened,
        Resolved,
        Closed,
        Reactivated
    }
}