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

namespace Database.Models
{
    public class StorageProvider : BaseEntity
    {
        [Required]
        [MaxLength(200)]
        public string Name { get; set; } = string.Empty;
        
        [Required]
        [MaxLength(50)]
        public string Type { get; set; } = string.Empty; // S3, Box, FTP
        
        // 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 StorageProviderConfiguration GetConfiguration()
        {
            if (string.IsNullOrEmpty(Configuration))
                return new StorageProviderConfiguration();
                
            try
            {
                return JsonSerializer.Deserialize<StorageProviderConfiguration>(Configuration) ?? new StorageProviderConfiguration();
            }
            catch
            {
                return new StorageProviderConfiguration();
            }
        }
        
        public void SetConfiguration(StorageProviderConfiguration config)
        {
            Configuration = JsonSerializer.Serialize(config);
        }
        
        // Helper properties
        public bool IsS3Provider => Type.Equals("S3", StringComparison.OrdinalIgnoreCase);
        
        public bool IsBoxProvider => Type.Equals("Box", StringComparison.OrdinalIgnoreCase);
        
        public bool IsFtpProvider => Type.Equals("FTP", StringComparison.OrdinalIgnoreCase);
    }
    
    public class StorageProviderConfiguration
    {
        // Common settings
        public string? ConnectionString { get; set; }
        public string? Region { get; set; }
        public string? Bucket { get; set; }
        public string? Container { get; set; }
        
        // AWS S3 specific
        public string? AccessKey { get; set; }
        public string? SecretKey { get; set; }
        public string? CloudFrontDomain { get; set; }
        
        // Box specific
        public string? ClientId { get; set; }
        public string? ClientSecret { get; set; }
        public string? AccessToken { get; set; }
        public string? RefreshToken { get; set; }
        
        // FTP specific
        public string? Host { get; set; }
        public int? Port { get; set; }
        public string? Username { get; set; }
        public string? Password { get; set; }
        public bool? UseSSL { get; set; }
        
        // Additional settings
        public Dictionary<string, object> AdditionalSettings { get; set; } = new();
        public Dictionary<string, string> Headers { get; set; } = new();
    }
}