package com.social.media.dto;

import jakarta.validation.constraints.*;
import com.fasterxml.jackson.annotation.JsonFormat;

import java.time.LocalDateTime;
import java.util.Map;

public class SocialNetworkDto {
    
    private Long id;
    
    private String networkCode;
    
    @NotBlank(message = "Nome da rede social é obrigatório")
    @Size(max = 100, message = "Nome deve ter no máximo 100 caracteres")
    private String name;
    
    @NotBlank(message = "Plataforma é obrigatória")
    @Size(max = 50, message = "Plataforma deve ter no máximo 50 caracteres")
    private String platform;
    
    @Size(max = 500, message = "URL base da API deve ter no máximo 500 caracteres")
    private String apiBaseUrl;
    
    @Size(max = 20, message = "Versão da API deve ter no máximo 20 caracteres")
    private String apiVersion;
    
    @Size(max = 500, message = "URL OAuth deve ter no máximo 500 caracteres")
    private String oauthUrl;
    
    @Size(max = 500, message = "URL do webhook deve ter no máximo 500 caracteres")
    private String webhookUrl;
    
    private Map<String, Object> apiConfig;
    
    private Map<String, Object> rateLimits;
    
    private Map<String, Object> supportedFeatures;
    
    @NotNull(message = "Status ativo é obrigatório")
    private Boolean isActive;
    
    @NotNull(message = "Flag de aprovação é obrigatória")
    private Boolean requiresApproval;
    
    @Size(max = 500, message = "URL do logo deve ter no máximo 500 caracteres")
    private String logoUrl;
    
    @Pattern(regexp = "^#[0-9A-Fa-f]{6}$", message = "Cor da marca deve estar no formato hexadecimal (#RRGGBB)")
    @Size(max = 7, message = "Cor da marca deve ter exatamente 7 caracteres")
    private String brandColor;
    
    @NotNull(message = "Ordem de classificação é obrigatória")
    @Min(value = 0, message = "Ordem de classificação deve ser no mínimo 0")
    private Integer sortOrder;
    
    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
    private LocalDateTime createdAt;
    
    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
    private LocalDateTime updatedAt;
    
    private Long createdBy;
    
    private Long updatedBy;
    
    // Derived fields for UI
    private Boolean isReadyForProduction;
    private String displayName;
    private Integer perHourRateLimit;
    private Integer perMinuteRateLimit;
    
    // Constructors
    public SocialNetworkDto() {
    }
    
    public SocialNetworkDto(String name, String platform) {
        this.name = name;
        this.platform = platform;
        this.isActive = true;
        this.requiresApproval = false;
        this.sortOrder = 0;
    }
    
    public SocialNetworkDto(Long id, String name, String platform, Boolean isActive, Integer sortOrder) {
        this.id = id;
        this.name = name;
        this.platform = platform;
        this.isActive = isActive;
        this.sortOrder = sortOrder;
        this.requiresApproval = false;
    }
    
    // Helper methods
    
    /**
     * Checks if the network supports a specific feature
     */
    public boolean supportsFeature(String featureName) {
        if (supportedFeatures == null) {
            return false;
        }
        return Boolean.TRUE.equals(supportedFeatures.get(featureName));
    }
    
    /**
     * Gets the display name for UI purposes
     */
    public String getDisplayName() {
        return name != null ? name : platform;
    }
    
    /**
     * Gets the per hour rate limit
     */
    public Integer getPerHourRateLimit() {
        if (rateLimits == null) {
            return null;
        }
        Object limit = rateLimits.get("perHour");
        return limit instanceof Number ? ((Number) limit).intValue() : null;
    }
    
    /**
     * Gets the per minute rate limit
     */
    public Integer getPerMinuteRateLimit() {
        if (rateLimits == null) {
            return null;
        }
        Object limit = rateLimits.get("perMinute");
        return limit instanceof Number ? ((Number) limit).intValue() : null;
    }
    
    /**
     * Checks if the network is ready for production use
     */
    public Boolean getIsReadyForProduction() {
        return isActive != null && isActive && 
               apiBaseUrl != null && !apiBaseUrl.trim().isEmpty() &&
               oauthUrl != null && !oauthUrl.trim().isEmpty();
    }
    
    // Getters and Setters
    
    public Long getId() {
        return id;
    }
    
    public void setId(Long id) {
        this.id = id;
    }
    
    public String getNetworkCode() {
        return networkCode;
    }
    
    public void setNetworkCode(String networkCode) {
        this.networkCode = networkCode;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public String getPlatform() {
        return platform;
    }
    
    public void setPlatform(String platform) {
        this.platform = platform;
    }
    
    public String getApiBaseUrl() {
        return apiBaseUrl;
    }
    
    public void setApiBaseUrl(String apiBaseUrl) {
        this.apiBaseUrl = apiBaseUrl;
    }
    
    public String getApiVersion() {
        return apiVersion;
    }
    
    public void setApiVersion(String apiVersion) {
        this.apiVersion = apiVersion;
    }
    
    public String getOauthUrl() {
        return oauthUrl;
    }
    
    public void setOauthUrl(String oauthUrl) {
        this.oauthUrl = oauthUrl;
    }
    
    public String getWebhookUrl() {
        return webhookUrl;
    }
    
    public void setWebhookUrl(String webhookUrl) {
        this.webhookUrl = webhookUrl;
    }
    
    public Map<String, Object> getApiConfig() {
        return apiConfig;
    }
    
    public void setApiConfig(Map<String, Object> apiConfig) {
        this.apiConfig = apiConfig;
    }
    
    public Map<String, Object> getRateLimits() {
        return rateLimits;
    }
    
    public void setRateLimits(Map<String, Object> rateLimits) {
        this.rateLimits = rateLimits;
    }
    
    public Map<String, Object> getSupportedFeatures() {
        return supportedFeatures;
    }
    
    public void setSupportedFeatures(Map<String, Object> supportedFeatures) {
        this.supportedFeatures = supportedFeatures;
    }
    
    public Boolean getIsActive() {
        return isActive;
    }
    
    public void setIsActive(Boolean isActive) {
        this.isActive = isActive;
    }
    
    public Boolean getRequiresApproval() {
        return requiresApproval;
    }
    
    public void setRequiresApproval(Boolean requiresApproval) {
        this.requiresApproval = requiresApproval;
    }
    
    public String getLogoUrl() {
        return logoUrl;
    }
    
    public void setLogoUrl(String logoUrl) {
        this.logoUrl = logoUrl;
    }
    
    public String getBrandColor() {
        return brandColor;
    }
    
    public void setBrandColor(String brandColor) {
        this.brandColor = brandColor;
    }
    
    public Integer getSortOrder() {
        return sortOrder;
    }
    
    public void setSortOrder(Integer sortOrder) {
        this.sortOrder = sortOrder;
    }
    
    public LocalDateTime getCreatedAt() {
        return createdAt;
    }
    
    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }
    
    public LocalDateTime getUpdatedAt() {
        return updatedAt;
    }
    
    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }
    
    public Long getCreatedBy() {
        return createdBy;
    }
    
    public void setCreatedBy(Long createdBy) {
        this.createdBy = createdBy;
    }
    
    public Long getUpdatedBy() {
        return updatedBy;
    }
    
    public void setUpdatedBy(Long updatedBy) {
        this.updatedBy = updatedBy;
    }
    
    public void setIsReadyForProduction(Boolean isReadyForProduction) {
        this.isReadyForProduction = isReadyForProduction;
    }
    
    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }
    
    public void setPerHourRateLimit(Integer perHourRateLimit) {
        this.perHourRateLimit = perHourRateLimit;
    }
    
    public void setPerMinuteRateLimit(Integer perMinuteRateLimit) {
        this.perMinuteRateLimit = perMinuteRateLimit;
    }
}
