using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace Database.Models
{
    public class FogBugzEvent : BaseEntity
    {
        public int CaseId { get; set; }
        
        [Required]
        [MaxLength(100)]
        public string EventType { get; set; } = string.Empty;
        
        [MaxLength(100)]
        public string? User { get; set; }
        
        public string? Comment { get; set; }
        
        public string? StatusString { get; set; }
        
        public DateTime EventDate { get; set; }
        
        public string? ReleaseNote { get; set; }
        
        public int? ZendeskNumber { get; set; }
        
        // Navigation properties
        [ForeignKey(nameof(CaseId))]
        public FogBugzCase Case { get; set; } = null!;
        
        // Helper properties
        public bool IsReleaseNoteEvent => EventType == "ReleaseNoted" && !string.IsNullOrEmpty(ReleaseNote);
        
        public bool IsZendeskEvent => EventType == "sZendesk" && ZendeskNumber.HasValue && ZendeskNumber > 0;
        
        public bool IsStatusChangeEvent => !string.IsNullOrEmpty(StatusString) && 
            (StatusString.StartsWith("Opened") || StatusString.StartsWith("Resolved") || 
             StatusString.StartsWith("Closed") || StatusString.StartsWith("Reactivated"));
        
        public FogBugzStatus GetMappedStatus()
        {
            if (string.IsNullOrEmpty(StatusString))
                return FogBugzStatus.Unknown;
                
            return StatusString.ToLower() switch
            {
                var s when s.StartsWith("opened") => 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
            };
        }
    }
}