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

namespace Database.Models
{
    public class HelpCenterProvider : BaseEntity
    {
        [Required]
        [MaxLength(200)]
        public string Name { get; set; } = string.Empty;
        
        [Required]
        [MaxLength(50)]
        public string Type { get; set; } = string.Empty; // Zendesk, Salesforce
        
        // JSON column for PostgreSQL
        [Column(TypeName = "jsonb")]
        public string? Configuration { get; set; }
        
        public bool IsActive { get; set; } = true;
        
        public string? Description { get; set; }
        
        public DateTime? LastTested { get; set; }
        
        public bool? LastTestResult { get; set; }
        
        public string? LastTestError { get; set; }
        
        // Helper methods for Configuration JSON
        public HelpCenterProviderConfiguration GetConfiguration()
        {
            if (string.IsNullOrEmpty(Configuration))
                return new HelpCenterProviderConfiguration();
                
            try
            {
                return JsonSerializer.Deserialize<HelpCenterProviderConfiguration>(Configuration) ?? new HelpCenterProviderConfiguration();
            }
            catch
            {
                return new HelpCenterProviderConfiguration();
            }
        }
        
        public void SetConfiguration(HelpCenterProviderConfiguration config)
        {
            Configuration = JsonSerializer.Serialize(config);
        }
        
        // Helper properties
        public bool IsZendeskProvider => Type.Equals("Zendesk", StringComparison.OrdinalIgnoreCase);
        
        public bool IsSalesforceProvider => Type.Equals("Salesforce", StringComparison.OrdinalIgnoreCase);
    }
    
    public class HelpCenterProviderConfiguration
    {
        // Common settings
        public string? BaseUrl { get; set; }
        public string? ApiVersion { get; set; }
        public string? DefaultLocale { get; set; } = "en-us";
        
        // Zendesk specific
        public string? Subdomain { get; set; }
        public string? Username { get; set; }
        public string? ApiKey { get; set; }
        public string? Token { get; set; }
        
        // Salesforce specific
        public string? ClientId { get; set; }
        public string? ClientSecret { get; set; }
        public string? Username { get; set; }
        public string? Password { get; set; }
        public string? SecurityToken { get; set; }
        public string? LoginUrl { get; set; }
        
        // Template variables
        public Dictionary<string, string> DefaultTemplateVariables { get; set; } = new()
        {
            ["VERSION"] = "{{VERSION}}",
            ["SWURL"] = "{{SWURL}}",
            ["PDFURL"] = "{{PDFURL}}",
            ["DATE"] = "{{DATE}}",
            ["PROJECT"] = "{{PROJECT}}"
        };
        
        // Additional settings
        public Dictionary<string, object> AdditionalSettings { get; set; } = new();
        public Dictionary<string, string> Headers { get; set; } = new();
    }
}