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

namespace Database.Models
{
    public class PublishingStep : BaseEntity
    {
        public int PublicationId { get; set; }
        
        [Required]
        [MaxLength(200)]
        public string StepName { get; set; } = string.Empty;
        
        [Required]
        [MaxLength(50)]
        public string Status { get; set; } = "Pending"; // Pending, InProgress, Completed, Failed, Skipped
        
        public string? Details { get; set; }
        
        public DateTime? StartedAt { get; set; }
        
        public DateTime? CompletedAt { get; set; }
        
        [Range(0, 100)]
        public int ProgressPercent { get; set; } = 0;
        
        public string? ErrorMessage { get; set; }
        
        public int StepOrder { get; set; }
        
        // Navigation properties
        [ForeignKey(nameof(PublicationId))]
        public Publication Publication { get; set; } = null!;
        
        // Helper properties
        public TimeSpan? Duration => CompletedAt?.Subtract(StartedAt ?? CreatedAt);
        
        public bool IsCompleted => Status == "Completed" && CompletedAt.HasValue;
        
        public bool IsFailed => Status == "Failed";
        
        public bool IsInProgress => Status == "InProgress";
        
        public bool IsPending => Status == "Pending";
        
        public void Start()
        {
            Status = "InProgress";
            StartedAt = DateTime.UtcNow;
            UpdateTimestamp("System");
        }
        
        public void Complete(string? details = null)
        {
            Status = "Completed";
            CompletedAt = DateTime.UtcNow;
            ProgressPercent = 100;
            if (details != null)
                Details = details;
            UpdateTimestamp("System");
        }
        
        public void Fail(string errorMessage, string? details = null)
        {
            Status = "Failed";
            CompletedAt = DateTime.UtcNow;
            ErrorMessage = errorMessage;
            if (details != null)
                Details = details;
            UpdateTimestamp("System");
        }
        
        public void Skip(string reason)
        {
            Status = "Skipped";
            CompletedAt = DateTime.UtcNow;
            Details = reason;
            ProgressPercent = 100;
            UpdateTimestamp("System");
        }
        
        public void UpdateProgress(int percent, string? details = null)
        {
            ProgressPercent = Math.Max(0, Math.Min(100, percent));
            if (details != null)
                Details = details;
            UpdateTimestamp("System");
        }
    }
    
    public static class PublishingStepNames
    {
        public const string ValidateConfiguration = "Validate Configuration";
        public const string CollectBuildData = "Collect Build Data";
        public const string QueryFogBugz = "Query FogBugz Data";
        public const string GenerateReleaseNotes = "Generate Release Notes";
        public const string CompressFiles = "Compress Files";
        public const string UploadToStorage = "Upload to Storage";
        public const string CleanupOldVersions = "Cleanup Old Versions";
        public const string UpdateHelpCenter = "Update Help Center";
        public const string UpdatePackageStatus = "Update Package Status";
        public const string SendNotification = "Send Notification";
    }
}