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

namespace Database.Models
{
    public class PackageConfiguration : BaseEntity
    {
        public int PackageId { get; set; }
        
        public string? BuildFolder { get; set; }
        
        public bool ZipContents { get; set; } = false;
        
        public bool DeleteOldPublishedBuilds { get; set; } = false;
        
        public string? ReleaseNoteTemplate { get; set; }
        
        // JSON columns for PostgreSQL
        [Column(TypeName = "jsonb")]
        public string? StorageSettings { get; set; }
        
        [Column(TypeName = "jsonb")]
        public string? HelpCenterSettings { get; set; }
        
        // Navigation properties
        [ForeignKey(nameof(PackageId))]
        public Package Package { get; set; } = null!;
        
        // Helper methods for StorageSettings JSON
        public StorageSettingsModel GetStorageSettings()
        {
            if (string.IsNullOrEmpty(StorageSettings))
                return new StorageSettingsModel();
                
            try
            {
                return JsonSerializer.Deserialize<StorageSettingsModel>(StorageSettings) ?? new StorageSettingsModel();
            }
            catch
            {
                return new StorageSettingsModel();
            }
        }
        
        public void SetStorageSettings(StorageSettingsModel settings)
        {
            StorageSettings = JsonSerializer.Serialize(settings);
        }
        
        // Helper methods for HelpCenterSettings JSON
        public HelpCenterSettingsModel GetHelpCenterSettings()
        {
            if (string.IsNullOrEmpty(HelpCenterSettings))
                return new HelpCenterSettingsModel();
                
            try
            {
                return JsonSerializer.Deserialize<HelpCenterSettingsModel>(HelpCenterSettings) ?? new HelpCenterSettingsModel();
            }
            catch
            {
                return new HelpCenterSettingsModel();
            }
        }
        
        public void SetHelpCenterSettings(HelpCenterSettingsModel settings)
        {
            HelpCenterSettings = JsonSerializer.Serialize(settings);
        }
    }
    
    public class StorageSettingsModel
    {
        public string? ProviderName { get; set; }
        public string? ContainerName { get; set; }
        public string? FolderPath { get; set; }
        public bool EnableCDN { get; set; } = false;
        public int? MaxVersionsToKeep { get; set; }
        public Dictionary<string, object> AdditionalSettings { get; set; } = new();
    }
    
    public class HelpCenterSettingsModel
    {
        public string? ProviderName { get; set; }
        public string? ArticleId { get; set; }
        public string? ArticleTemplate { get; set; }
        public string? Locale { get; set; } = "en-us";
        public Dictionary<string, string> TemplateVariables { get; set; } = new();
        public Dictionary<string, object> AdditionalSettings { get; set; } = new();
    }
}