using OnlyOneAccessTemplate.Models;

namespace OnlyOneAccessTemplate.Services
{
    public interface ISiteConfigurationService
    {
        Task<SiteConfiguration> GetConfigurationAsync(string language);
        SiteConfiguration GetConfiguration(string language); // Versão síncrona para o BaseController
        Task<string> GenerateSitemapAsync();
        Task<string> GenerateRobotsAsync();
        Task UpdateConfigurationAsync(SiteConfiguration config);
        Task<List<string>> GetAvailableLanguagesAsync();
        Task<bool> ConfigurationExistsAsync(string language);
        Task DeleteConfigurationAsync(string language);
    }

    public interface ILanguageService
    {
        string GetCurrentLanguage(HttpContext context);
        string GetLanguageFromPath(string path);
        List<string> GetSupportedLanguages();
        string GetDefaultLanguage();
        string GetLanguageDisplayName(string languageCode);
        Dictionary<string, string> GetHreflangUrls(string currentUrl, string currentLanguage);
        string GetCultureInfo(string languageCode);
        void SetLanguageCookie(HttpContext context, string languageCode);
        string GenerateLanguageSwitcher(HttpContext context, string currentLanguage);
        bool IsRtlLanguage(string languageCode);
        string GetLanguageDirection(string languageCode);
        bool IsValidLanguage(string languageCode);
        string NormalizeLanguageCode(string languageCode);
    }

    public interface IConversionService
    {
        Task<bool> ProcessConversionAsync(ConversionData data);
        Task<ConversionStats> GetConversionStatsAsync(string language, DateTime from, DateTime to);
        Task LogConversionEventAsync(string eventName, Dictionary<string, object> properties);
        Task<ConversionRecord?> GetConversionByIdAsync(string conversionId);
        Task<List<ConversionRecord>> GetConversionsAsync(string language, int skip = 0, int take = 50);
        Task<bool> ValidateFormDataAsync(Dictionary<string, string> formData, List<FormField> formFields);
        Task<ConversionStats> GetRealTimeStatsAsync(string language);
        Task<Dictionary<string, object>> GetConversionMetricsAsync(string language, string period);
        Task<bool> SendConversionNotificationAsync(ConversionRecord conversion);
        Task<List<ConversionTrend>> GetConversionTrendsAsync(string language, DateTime from, DateTime to, string groupBy = "day");
    }
    public interface ISeoService
    {
        Task<SeoMetadata> GenerateSeoMetadataAsync(string language, string pageName, PageContent? content = null);
        Task<string> GenerateStructuredDataAsync(string language, string pageType, object data);
        Task<List<SeoIssue>> ValidateSeoAsync(string url);
        Task<SeoMetadata> GenerateMetadataForPageAsync(HttpContext context, string language, string pageName, PageContent? customContent = null);
        Task<string> GenerateCanonicalUrlAsync(HttpContext context, string language, string pageName);
        Task<Dictionary<string, string>> GenerateMetaTagsAsync(SeoMetadata metadata);
        Task<string> GenerateOpenGraphTagsAsync(SeoMetadata metadata);
        Task<string> GenerateTwitterCardTagsAsync(SeoMetadata metadata);
        Task<string> GenerateJsonLdAsync(string language, string pageType, object data);
        bool ValidateMetadata(SeoMetadata metadata, out List<SeoIssue> issues);
    }

    // Interfaces adicionais
    public interface ICacheService
    {
        Task<T?> GetAsync<T>(string key) where T : class;
        Task SetAsync<T>(string key, T value, TimeSpan? expiration = null) where T : class;
        Task RemoveAsync(string key);
        Task RemoveByPatternAsync(string pattern);
        Task<bool> ExistsAsync(string key);
    }

    public interface IAnalyticsService
    {
        Task TrackPageViewAsync(string language, string pageName, string userAgent, string ipAddress, string? referrer = null);
        Task TrackEventAsync(string eventName, string language, Dictionary<string, object> properties);
        Task<AnalyticsData> GetAnalyticsAsync(string language, DateTime from, DateTime to);
        Task<List<PopularPage>> GetPopularPagesAsync(string language, int limit = 10);
        Task<Dictionary<string, int>> GetTrafficSourcesAsync(string language, DateTime from, DateTime to);
    }

    public interface IEmailService
    {
        Task<bool> SendConversionNotificationAsync(ConversionRecord conversion, string toEmail);
        Task<bool> SendWelcomeEmailAsync(string toEmail, string name, string language);
        Task<bool> SendBulkEmailAsync(List<string> emails, string subject, string content, string language);
        Task<EmailTemplate?> GetEmailTemplateAsync(string templateName, string language);
    }

    public interface IValidationService
    {
        ValidationResult ValidateFormData(Dictionary<string, string> formData, List<FormField> formFields);
        bool ValidateEmail(string email);
        bool ValidatePhone(string phone, string? countryCode = null);
        bool ValidateRequired(string value);
        bool ValidateRegex(string value, string pattern);
        ValidationResult ValidateConversionData(ConversionData data);
    }

    public interface IFileService
    {
        Task<string> UploadImageAsync(IFormFile file, string folder = "uploads");
        Task<bool> DeleteFileAsync(string filePath);
        Task<string> ResizeImageAsync(string filePath, int width, int height);
        Task<List<string>> GetImagesAsync(string folder = "uploads");
        bool IsValidImageFile(IFormFile file);
    }

    public interface IAdsService
    {
        bool IsAdsEnabled();
        string GetPublisherID();
        AdConfiguration GetAdConfiguration(string adPosition);
        string GenerateAdHtml(string position, string adSlot, AdSize size = AdSize.Responsive);
    }

    public enum AdSize
    {
        Responsive,
        Banner_728x90,
        Rectangle_300x250,
        Sidebar_160x600,
        Sidebar_300x600,
        Mobile_320x50,
        Square_250x250
    }

    public record AdConfiguration(
        string Position,
        string SlotId,
        AdSize Size,
        bool IsSticky = false,
        bool ShowOnMobile = true,
        bool ShowOnDesktop = true,
        int Priority = 1
    );

    // DTOs e Records
    public record ConversionData(
        string Language,
        string PageName,
        Dictionary<string, string> FormData,
        string UserAgent,
        string IpAddress,
        string? Referrer = null,
        string? SessionId = null,
        Dictionary<string, string>? UtmParameters = null
    );

    public record ConversionStats(
        int TotalVisits,
        int TotalConversions,
        double ConversionRate,
        Dictionary<string, int> ConversionsBySource,
        Dictionary<string, int>? ConversionsByMedium = null,
        Dictionary<string, int>? ConversionsByPage = null,
        DateTime? LastUpdated = null
    );

    public record ConversionTrend(
        DateTime Date,
        int Conversions,
        int Visits,
        double ConversionRate
    );

    public record SeoMetadata(
        string Title,
        string Description,
        string Keywords,
        string OgTitle,
        string OgDescription,
        string OgImage,
        string CanonicalUrl,
        Dictionary<string, string> HreflangUrls,
        string StructuredData,
        string? OgType = "website",
        string? TwitterCard = "summary_large_image",
        string? Author = null,
        DateTime? LastModified = null
    );

    public record SeoIssue(
        string Type,
        string Message,
        string Severity,
        string Element,
        string? Recommendation = null,
        string? HelpUrl = null
    );

    public record AnalyticsData(
        int TotalPageViews,
        int UniqueVisitors,
        Dictionary<string, int> PageViews,
        Dictionary<string, int> TrafficSources,
        double AverageSessionDuration,
        double BounceRate
    );

    public record PopularPage(
        string PageName,
        string Title,
        int Views,
        double ConversionRate
    );

    public record EmailTemplate(
        string Name,
        string Subject,
        string HtmlContent,
        string TextContent,
        string Language
    );

    public record ValidationResult(
        bool IsValid,
        List<string> Errors,
        Dictionary<string, string>? FieldErrors = null
    );

    // Enums
    public enum SeoIssueSeverity
    {
        Low,
        Medium,
        High,
        Critical
    }

    public enum ConversionEventType
    {
        PageView,
        FormStart,
        FormSubmit,
        ConversionComplete,
        DownloadStart,
        VideoPlay,
        EmailClick,
        PhoneClick
    }

    public enum CacheType
    {
        Memory,
        Distributed,
        Database
    }
}
