package com.social.media.domain.socialaccount.valueobjects;

import com.social.media.domain.shared.valueobjects.ValueObject;
import jakarta.persistence.Embeddable;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * Value object representing a social media platform with its characteristics and capabilities.
 */
@Embeddable
public class SocialPlatform implements ValueObject {
    
    @Enumerated(EnumType.STRING)
    private PlatformType platformType;
    
    // Platform Configuration
    private String apiVersion;
    private String baseUrl;
    private Integer maxPostLength;
    private Boolean supportsImages;
    private Boolean supportsVideo;
    private Boolean supportsHashtags;
    private Boolean supportsMentions;
    private Boolean supportsScheduling;
    private Boolean supportsAnalytics;
    
    // Rate Limiting
    private Integer rateLimitPerHour;
    private Integer rateLimitPerDay;
    
    // Authentication
    private String authType;
    private Boolean requiresAppApproval;
    
    // Metadata
    private LocalDateTime lastUpdated;
    private Boolean isActive;

    /**
     * Enum representing different social media platforms
     */
    public enum PlatformType {
        FACEBOOK("Facebook", "https://graph.facebook.com"),
        INSTAGRAM("Instagram", "https://graph.instagram.com"),
        TWITTER("Twitter/X", "https://api.twitter.com"),
        LINKEDIN("LinkedIn", "https://api.linkedin.com"),
        TIKTOK("TikTok", "https://open-api.tiktok.com"),
        YOUTUBE("YouTube", "https://www.googleapis.com/youtube"),
        PINTEREST("Pinterest", "https://api.pinterest.com"),
        SNAPCHAT("Snapchat", "https://adsapi.snapchat.com");
        
        private final String displayName;
        private final String baseApiUrl;
        
        PlatformType(String displayName, String baseApiUrl) {
            this.displayName = displayName;
            this.baseApiUrl = baseApiUrl;
        }
        
        public String getDisplayName() {
            return displayName;
        }
        
        public String getBaseApiUrl() {
            return baseApiUrl;
        }
    }

    // Default constructor for JPA
    protected SocialPlatform() {}

    /**
     * Private constructor for creating SocialPlatform instances
     */
    private SocialPlatform(PlatformType platformType) {
        this.platformType = platformType;
        this.lastUpdated = LocalDateTime.now();
        this.isActive = true;
        
        // Set default configurations based on platform type
        configureDefaults(platformType);
    }

    /**
     * Factory method to create a SocialPlatform for a specific platform type
     */
    public static SocialPlatform of(PlatformType platformType) {
        Objects.requireNonNull(platformType, "Platform type cannot be null");
        return new SocialPlatform(platformType);
    }

    /**
     * Factory method to create a Facebook platform
     */
    public static SocialPlatform facebook() {
        return of(PlatformType.FACEBOOK);
    }

    /**
     * Factory method to create an Instagram platform
     */
    public static SocialPlatform instagram() {
        return of(PlatformType.INSTAGRAM);
    }

    /**
     * Factory method to create a Twitter platform
     */
    public static SocialPlatform twitter() {
        return of(PlatformType.TWITTER);
    }

    /**
     * Factory method to create a LinkedIn platform
     */
    public static SocialPlatform linkedin() {
        return of(PlatformType.LINKEDIN);
    }

    /**
     * Factory method to create a TikTok platform
     */
    public static SocialPlatform tiktok() {
        return of(PlatformType.TIKTOK);
    }

    /**
     * Factory method to create a YouTube platform
     */
    public static SocialPlatform youtube() {
        return of(PlatformType.YOUTUBE);
    }

    /**
     * Factory method to create a Pinterest platform
     */
    public static SocialPlatform pinterest() {
        return of(PlatformType.PINTEREST);
    }

    /**
     * Factory method to create a Snapchat platform
     */
    public static SocialPlatform snapchat() {
        return of(PlatformType.SNAPCHAT);
    }

    /**
     * Configure default settings for each platform type
     */
    private void configureDefaults(PlatformType type) {
        this.baseUrl = type.getBaseApiUrl();
        
        switch (type) {
            case FACEBOOK:
                this.apiVersion = "v18.0";
                this.maxPostLength = 63206;
                this.supportsImages = true;
                this.supportsVideo = true;
                this.supportsHashtags = true;
                this.supportsMentions = true;
                this.supportsScheduling = true;
                this.supportsAnalytics = true;
                this.rateLimitPerHour = 200;
                this.rateLimitPerDay = 4800;
                this.authType = "OAuth2";
                this.requiresAppApproval = true;
                break;
                
            case INSTAGRAM:
                this.apiVersion = "v18.0";
                this.maxPostLength = 2200;
                this.supportsImages = true;
                this.supportsVideo = true;
                this.supportsHashtags = true;
                this.supportsMentions = true;
                this.supportsScheduling = true;
                this.supportsAnalytics = true;
                this.rateLimitPerHour = 200;
                this.rateLimitPerDay = 4800;
                this.authType = "OAuth2";
                this.requiresAppApproval = true;
                break;
                
            case TWITTER:
                this.apiVersion = "2.0";
                this.maxPostLength = 280;
                this.supportsImages = true;
                this.supportsVideo = true;
                this.supportsHashtags = true;
                this.supportsMentions = true;
                this.supportsScheduling = false;
                this.supportsAnalytics = true;
                this.rateLimitPerHour = 300;
                this.rateLimitPerDay = 7200;
                this.authType = "OAuth2";
                this.requiresAppApproval = true;
                break;
                
            case LINKEDIN:
                this.apiVersion = "v2";
                this.maxPostLength = 3000;
                this.supportsImages = true;
                this.supportsVideo = true;
                this.supportsHashtags = true;
                this.supportsMentions = true;
                this.supportsScheduling = true;
                this.supportsAnalytics = true;
                this.rateLimitPerHour = 100;
                this.rateLimitPerDay = 2400;
                this.authType = "OAuth2";
                this.requiresAppApproval = true;
                break;
                
            case TIKTOK:
                this.apiVersion = "v1.3";
                this.maxPostLength = 150;
                this.supportsImages = true;
                this.supportsVideo = true;
                this.supportsHashtags = true;
                this.supportsMentions = true;
                this.supportsScheduling = false;
                this.supportsAnalytics = true;
                this.rateLimitPerHour = 50;
                this.rateLimitPerDay = 1200;
                this.authType = "OAuth2";
                this.requiresAppApproval = true;
                break;
                
            case YOUTUBE:
                this.apiVersion = "v3";
                this.maxPostLength = 5000;
                this.supportsImages = false;
                this.supportsVideo = true;
                this.supportsHashtags = true;
                this.supportsMentions = false;
                this.supportsScheduling = true;
                this.supportsAnalytics = true;
                this.rateLimitPerHour = 10000;
                this.rateLimitPerDay = 240000;
                this.authType = "OAuth2";
                this.requiresAppApproval = true;
                break;
                
            case PINTEREST:
                this.apiVersion = "v5";
                this.maxPostLength = 500;
                this.supportsImages = true;
                this.supportsVideo = true;
                this.supportsHashtags = true;
                this.supportsMentions = false;
                this.supportsScheduling = true;
                this.supportsAnalytics = true;
                this.rateLimitPerHour = 1000;
                this.rateLimitPerDay = 24000;
                this.authType = "OAuth2";
                this.requiresAppApproval = false;
                break;
                
            case SNAPCHAT:
                this.apiVersion = "v1";
                this.maxPostLength = 31;
                this.supportsImages = true;
                this.supportsVideo = true;
                this.supportsHashtags = false;
                this.supportsMentions = false;
                this.supportsScheduling = false;
                this.supportsAnalytics = true;
                this.rateLimitPerHour = 100;
                this.rateLimitPerDay = 2400;
                this.authType = "OAuth2";
                this.requiresAppApproval = true;
                break;
        }
    }

    /**
     * Get the platform type
     */
    public PlatformType getPlatformType() {
        return platformType;
    }

    /**
     * Get the display name of the platform
     */
    public String getName() {
        return platformType != null ? platformType.getDisplayName() : null;
    }

    /**
     * Get the API version
     */
    public String getApiVersion() {
        return apiVersion;
    }

    /**
     * Get the base URL
     */
    public String getBaseUrl() {
        return baseUrl;
    }

    /**
     * Get the maximum post length
     */
    public Integer getMaxPostLength() {
        return maxPostLength;
    }

    /**
     * Check if platform supports images
     */
    public Boolean getSupportsImages() {
        return supportsImages;
    }

    /**
     * Check if platform supports video
     */
    public Boolean getSupportsVideo() {
        return supportsVideo;
    }

    /**
     * Check if platform supports hashtags
     */
    public Boolean getSupportsHashtags() {
        return supportsHashtags;
    }

    /**
     * Check if platform supports mentions
     */
    public Boolean getSupportsMentions() {
        return supportsMentions;
    }

    /**
     * Check if platform supports scheduling
     */
    public Boolean getSupportsScheduling() {
        return supportsScheduling;
    }

    /**
     * Check if platform supports analytics
     */
    public Boolean getSupportsAnalytics() {
        return supportsAnalytics;
    }

    /**
     * Get rate limit per hour
     */
    public Integer getRateLimitPerHour() {
        return rateLimitPerHour;
    }

    /**
     * Get rate limit per day
     */
    public Integer getRateLimitPerDay() {
        return rateLimitPerDay;
    }

    /**
     * Get authentication type
     */
    public String getAuthType() {
        return authType;
    }

    /**
     * Check if requires app approval
     */
    public Boolean getRequiresAppApproval() {
        return requiresAppApproval;
    }

    /**
     * Get last updated timestamp
     */
    public LocalDateTime getLastUpdated() {
        return lastUpdated;
    }

    /**
     * Check if platform is active
     */
    public Boolean getIsActive() {
        return isActive;
    }

    /**
     * Check if platform can post now based on rate limits
     */
    public boolean canPostNow(int postsThisHour, int postsToday) {
        if (!Boolean.TRUE.equals(isActive)) {
            return false;
        }
        
        if (rateLimitPerHour != null && postsThisHour >= rateLimitPerHour) {
            return false;
        }
        
        if (rateLimitPerDay != null && postsToday >= rateLimitPerDay) {
            return false;
        }
        
        return true;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        SocialPlatform that = (SocialPlatform) obj;
        return Objects.equals(platformType, that.platformType);
    }

    @Override
    public int hashCode() {
        return Objects.hash(platformType);
    }

    @Override
    public String toString() {
        return "SocialPlatform{" +
                "platformType=" + platformType +
                ", apiVersion='" + apiVersion + '\'' +
                ", maxPostLength=" + maxPostLength +
                ", supportsImages=" + supportsImages +
                ", supportsVideo=" + supportsVideo +
                ", supportsScheduling=" + supportsScheduling +
                ", isActive=" + isActive +
                '}';
    }
}