package com.social.media.domain.socialnetwork;

import com.social.media.domain.shared.BaseValueObject;

import java.util.Set;
import java.util.Objects;

/**
 * Value object representing the features supported by a social network platform.
 * Defines what capabilities are available for automation and content management.
 */
public final class PlatformFeatures extends BaseValueObject {
    
    private final boolean supportsTextPosts;
    private final boolean supportsImagePosts;
    private final boolean supportsVideoPosts;
    private final boolean supportsCarouselPosts;
    private final boolean supportsStories;
    private final boolean supportsReels;
    private final boolean supportsLiveStreaming;
    private final boolean supportsScheduling;
    private final boolean supportsHashtags;
    private final boolean supportsMentions;
    private final boolean supportsLocationTagging;
    private final boolean supportsDirectMessages;
    private final boolean supportsComments;
    private final boolean supportsLikes;
    private final boolean supportsShares;
    private final boolean supportsFollowing;
    private final boolean supportsAnalytics;
    private final boolean supportsAds;
    private final boolean supportsEcommerce;
    private final boolean supportsPolls;
    private final boolean supportsEvents;
    private final boolean supportsGroups;
    private final boolean supportsPages;
    private final boolean supportsMultipleAccounts;
    private final boolean supportsBusinessApi;
    private final boolean supportsWebhooks;
    private final boolean supportsContentModeration;
    private final boolean supportsAutoPosting;
    private final boolean supportsAutoFollowing;
    private final boolean supportsAutoLiking;
    private final boolean supportsAutoCommenting;
    private final Set<String> supportedImageFormats;
    private final Set<String> supportedVideoFormats;
    private final Integer maxImageSize; // in MB
    private final Integer maxVideoSize; // in MB
    private final Integer maxVideoLength; // in seconds
    private final Integer maxCaptionLength;
    private final Integer maxHashtagsPerPost;
    private final Integer maxMentionsPerPost;
    
    public PlatformFeatures(
            boolean supportsTextPosts,
            boolean supportsImagePosts,
            boolean supportsVideoPosts,
            boolean supportsCarouselPosts,
            boolean supportsStories,
            boolean supportsReels,
            boolean supportsLiveStreaming,
            boolean supportsScheduling,
            boolean supportsHashtags,
            boolean supportsMentions,
            boolean supportsLocationTagging,
            boolean supportsDirectMessages,
            boolean supportsComments,
            boolean supportsLikes,
            boolean supportsShares,
            boolean supportsFollowing,
            boolean supportsAnalytics,
            boolean supportsAds,
            boolean supportsEcommerce,
            boolean supportsPolls,
            boolean supportsEvents,
            boolean supportsGroups,
            boolean supportsPages,
            boolean supportsMultipleAccounts,
            boolean supportsBusinessApi,
            boolean supportsWebhooks,
            boolean supportsContentModeration,
            boolean supportsAutoPosting,
            boolean supportsAutoFollowing,
            boolean supportsAutoLiking,
            boolean supportsAutoCommenting,
            Set<String> supportedImageFormats,
            Set<String> supportedVideoFormats,
            Integer maxImageSize,
            Integer maxVideoSize,
            Integer maxVideoLength,
            Integer maxCaptionLength,
            Integer maxHashtagsPerPost,
            Integer maxMentionsPerPost) {
        
        this.supportsTextPosts = supportsTextPosts;
        this.supportsImagePosts = supportsImagePosts;
        this.supportsVideoPosts = supportsVideoPosts;
        this.supportsCarouselPosts = supportsCarouselPosts;
        this.supportsStories = supportsStories;
        this.supportsReels = supportsReels;
        this.supportsLiveStreaming = supportsLiveStreaming;
        this.supportsScheduling = supportsScheduling;
        this.supportsHashtags = supportsHashtags;
        this.supportsMentions = supportsMentions;
        this.supportsLocationTagging = supportsLocationTagging;
        this.supportsDirectMessages = supportsDirectMessages;
        this.supportsComments = supportsComments;
        this.supportsLikes = supportsLikes;
        this.supportsShares = supportsShares;
        this.supportsFollowing = supportsFollowing;
        this.supportsAnalytics = supportsAnalytics;
        this.supportsAds = supportsAds;
        this.supportsEcommerce = supportsEcommerce;
        this.supportsPolls = supportsPolls;
        this.supportsEvents = supportsEvents;
        this.supportsGroups = supportsGroups;
        this.supportsPages = supportsPages;
        this.supportsMultipleAccounts = supportsMultipleAccounts;
        this.supportsBusinessApi = supportsBusinessApi;
        this.supportsWebhooks = supportsWebhooks;
        this.supportsContentModeration = supportsContentModeration;
        this.supportsAutoPosting = supportsAutoPosting;
        this.supportsAutoFollowing = supportsAutoFollowing;
        this.supportsAutoLiking = supportsAutoLiking;
        this.supportsAutoCommenting = supportsAutoCommenting;
        this.supportedImageFormats = supportedImageFormats != null ? Set.copyOf(supportedImageFormats) : Set.of();
        this.supportedVideoFormats = supportedVideoFormats != null ? Set.copyOf(supportedVideoFormats) : Set.of();
        this.maxImageSize = validateSize(maxImageSize, "maxImageSize");
        this.maxVideoSize = validateSize(maxVideoSize, "maxVideoSize");
        this.maxVideoLength = validateSize(maxVideoLength, "maxVideoLength");
        this.maxCaptionLength = validateSize(maxCaptionLength, "maxCaptionLength");
        this.maxHashtagsPerPost = validateSize(maxHashtagsPerPost, "maxHashtagsPerPost");
        this.maxMentionsPerPost = validateSize(maxMentionsPerPost, "maxMentionsPerPost");
        
        validate();
    }
    
    private Integer validateSize(Integer size, String fieldName) {
        if (size != null && size <= 0) {
            throw new IllegalArgumentException(fieldName + " must be positive");
        }
        return size;
    }
    
    @Override
    public void validate() {
        // At least one post type must be supported
        if (!supportsTextPosts && !supportsImagePosts && !supportsVideoPosts) {
            throw new IllegalArgumentException("Platform must support at least one post type");
        }
        
        // If image posts are supported, image formats should be specified
        if (supportsImagePosts && supportedImageFormats.isEmpty()) {
            throw new IllegalArgumentException("Supported image formats must be specified if image posts are supported");
        }
        
        // If video posts are supported, video formats should be specified
        if (supportsVideoPosts && supportedVideoFormats.isEmpty()) {
            throw new IllegalArgumentException("Supported video formats must be specified if video posts are supported");
        }
    }
    
    /**
     * Creates Instagram-specific platform features.
     */
    public static PlatformFeatures forInstagram() {
        return new PlatformFeatures(
            true,   // supportsTextPosts
            true,   // supportsImagePosts
            true,   // supportsVideoPosts
            true,   // supportsCarouselPosts
            true,   // supportsStories
            true,   // supportsReels
            true,   // supportsLiveStreaming
            true,   // supportsScheduling
            true,   // supportsHashtags
            true,   // supportsMentions
            true,   // supportsLocationTagging
            true,   // supportsDirectMessages
            true,   // supportsComments
            true,   // supportsLikes
            false,  // supportsShares (Instagram doesn't have traditional shares)
            true,   // supportsFollowing
            true,   // supportsAnalytics
            true,   // supportsAds
            true,   // supportsEcommerce
            true,   // supportsPolls (in stories)
            false,  // supportsEvents
            false,  // supportsGroups
            false,  // supportsPages
            true,   // supportsMultipleAccounts
            true,   // supportsBusinessApi
            true,   // supportsWebhooks
            true,   // supportsContentModeration
            true,   // supportsAutoPosting
            true,   // supportsAutoFollowing
            true,   // supportsAutoLiking
            true,   // supportsAutoCommenting
            Set.of("jpg", "jpeg", "png", "webp"),
            Set.of("mp4", "mov"),
            30,     // maxImageSize (30MB)
            100,    // maxVideoSize (100MB)
            60,     // maxVideoLength (60 seconds for feed, longer for IGTV)
            2200,   // maxCaptionLength
            30,     // maxHashtagsPerPost
            20      // maxMentionsPerPost
        );
    }
    
    /**
     * Creates Facebook-specific platform features.
     */
    public static PlatformFeatures forFacebook() {
        return new PlatformFeatures(
            true,   // supportsTextPosts
            true,   // supportsImagePosts
            true,   // supportsVideoPosts
            true,   // supportsCarouselPosts
            true,   // supportsStories
            true,   // supportsReels
            true,   // supportsLiveStreaming
            true,   // supportsScheduling
            true,   // supportsHashtags
            true,   // supportsMentions
            true,   // supportsLocationTagging
            true,   // supportsDirectMessages
            true,   // supportsComments
            true,   // supportsLikes
            true,   // supportsShares
            true,   // supportsFollowing
            true,   // supportsAnalytics
            true,   // supportsAds
            true,   // supportsEcommerce
            true,   // supportsPolls
            true,   // supportsEvents
            true,   // supportsGroups
            true,   // supportsPages
            true,   // supportsMultipleAccounts
            true,   // supportsBusinessApi
            true,   // supportsWebhooks
            true,   // supportsContentModeration
            true,   // supportsAutoPosting
            true,   // supportsAutoFollowing
            true,   // supportsAutoLiking
            true,   // supportsAutoCommenting
            Set.of("jpg", "jpeg", "png", "gif", "webp", "bmp"),
            Set.of("mp4", "mov", "avi", "wmv", "flv", "3gp"),
            50,     // maxImageSize (50MB)
            4000,   // maxVideoSize (4GB)
            14400,  // maxVideoLength (4 hours)
            63206,  // maxCaptionLength
            null,   // maxHashtagsPerPost (no specific limit)
            null    // maxMentionsPerPost (no specific limit)
        );
    }
    
    /**
     * Creates Twitter/X-specific platform features.
     */
    public static PlatformFeatures forTwitter() {
        return new PlatformFeatures(
            true,   // supportsTextPosts
            true,   // supportsImagePosts
            true,   // supportsVideoPosts
            false,  // supportsCarouselPosts
            false,  // supportsStories (Fleets were discontinued)
            false,  // supportsReels
            true,   // supportsLiveStreaming (Spaces)
            true,   // supportsScheduling
            true,   // supportsHashtags
            true,   // supportsMentions
            true,   // supportsLocationTagging
            true,   // supportsDirectMessages
            true,   // supportsComments (replies)
            true,   // supportsLikes
            true,   // supportsShares (retweets)
            true,   // supportsFollowing
            true,   // supportsAnalytics
            true,   // supportsAds
            false,  // supportsEcommerce
            true,   // supportsPolls
            false,  // supportsEvents
            false,  // supportsGroups (Communities)
            false,  // supportsPages
            true,   // supportsMultipleAccounts
            true,   // supportsBusinessApi
            true,   // supportsWebhooks
            true,   // supportsContentModeration
            true,   // supportsAutoPosting
            true,   // supportsAutoFollowing
            true,   // supportsAutoLiking
            true,   // supportsAutoCommenting
            Set.of("jpg", "jpeg", "png", "gif", "webp"),
            Set.of("mp4", "mov"),
            5,      // maxImageSize (5MB)
            512,    // maxVideoSize (512MB)
            140,    // maxVideoLength (2 minutes 20 seconds)
            280,    // maxCaptionLength (280 characters)
            null,   // maxHashtagsPerPost (no hard limit, but performance degrades)
            null    // maxMentionsPerPost (no hard limit)
        );
    }
    
    /**
     * Creates LinkedIn-specific platform features.
     */
    public static PlatformFeatures forLinkedIn() {
        return new PlatformFeatures(
            true,   // supportsTextPosts
            true,   // supportsImagePosts
            true,   // supportsVideoPosts
            true,   // supportsCarouselPosts
            false,  // supportsStories
            false,  // supportsReels
            true,   // supportsLiveStreaming
            true,   // supportsScheduling
            true,   // supportsHashtags
            true,   // supportsMentions
            false,  // supportsLocationTagging
            true,   // supportsDirectMessages
            true,   // supportsComments
            true,   // supportsLikes
            true,   // supportsShares
            true,   // supportsFollowing
            true,   // supportsAnalytics
            true,   // supportsAds
            false,  // supportsEcommerce
            true,   // supportsPolls
            true,   // supportsEvents
            false,  // supportsGroups
            true,   // supportsPages (Company pages)
            true,   // supportsMultipleAccounts
            true,   // supportsBusinessApi
            true,   // supportsWebhooks
            true,   // supportsContentModeration
            true,   // supportsAutoPosting
            false,  // supportsAutoFollowing (limited)
            false,  // supportsAutoLiking (limited)
            false,  // supportsAutoCommenting (limited)
            Set.of("jpg", "jpeg", "png", "gif"),
            Set.of("mp4", "mov", "wmv", "flv", "avi", "asf", "3gp", "mkv"),
            20,     // maxImageSize (20MB)
            200,    // maxVideoSize (200MB)
            600,    // maxVideoLength (10 minutes)
            3000,   // maxCaptionLength
            null,   // maxHashtagsPerPost (no hard limit, but 3-5 recommended)
            null    // maxMentionsPerPost (no hard limit)
        );
    }
    
    /**
     * Checks if a specific feature is supported.
     */
    public boolean supportsFeature(String featureName) {
        return switch (featureName.toLowerCase()) {
            case "text_posts" -> supportsTextPosts;
            case "image_posts" -> supportsImagePosts;
            case "video_posts" -> supportsVideoPosts;
            case "carousel_posts" -> supportsCarouselPosts;
            case "stories" -> supportsStories;
            case "reels" -> supportsReels;
            case "live_streaming" -> supportsLiveStreaming;
            case "scheduling" -> supportsScheduling;
            case "hashtags" -> supportsHashtags;
            case "mentions" -> supportsMentions;
            case "location_tagging" -> supportsLocationTagging;
            case "direct_messages" -> supportsDirectMessages;
            case "comments" -> supportsComments;
            case "likes" -> supportsLikes;
            case "shares" -> supportsShares;
            case "following" -> supportsFollowing;
            case "analytics" -> supportsAnalytics;
            case "ads" -> supportsAds;
            case "ecommerce" -> supportsEcommerce;
            case "polls" -> supportsPolls;
            case "events" -> supportsEvents;
            case "groups" -> supportsGroups;
            case "pages" -> supportsPages;
            case "multiple_accounts" -> supportsMultipleAccounts;
            case "business_api" -> supportsBusinessApi;
            case "webhooks" -> supportsWebhooks;
            case "content_moderation" -> supportsContentModeration;
            case "auto_posting" -> supportsAutoPosting;
            case "auto_following" -> supportsAutoFollowing;
            case "auto_liking" -> supportsAutoLiking;
            case "auto_commenting" -> supportsAutoCommenting;
            default -> false;
        };
    }
    
    /**
     * Checks if an image format is supported.
     */
    public boolean supportsImageFormat(String format) {
        return supportedImageFormats.contains(format.toLowerCase());
    }
    
    /**
     * Checks if a video format is supported.
     */
    public boolean supportsVideoFormat(String format) {
        return supportedVideoFormats.contains(format.toLowerCase());
    }
    
    // Getters
    public boolean supportsTextPosts() { return supportsTextPosts; }
    public boolean supportsImagePosts() { return supportsImagePosts; }
    public boolean supportsVideoPosts() { return supportsVideoPosts; }
    public boolean supportsCarouselPosts() { return supportsCarouselPosts; }
    public boolean supportsStories() { return supportsStories; }
    public boolean supportsReels() { return supportsReels; }
    public boolean supportsLiveStreaming() { return supportsLiveStreaming; }
    public boolean supportsScheduling() { return supportsScheduling; }
    public boolean supportsHashtags() { return supportsHashtags; }
    public boolean supportsMentions() { return supportsMentions; }
    public boolean supportsLocationTagging() { return supportsLocationTagging; }
    public boolean supportsDirectMessages() { return supportsDirectMessages; }
    public boolean supportsComments() { return supportsComments; }
    public boolean supportsLikes() { return supportsLikes; }
    public boolean supportsShares() { return supportsShares; }
    public boolean supportsFollowing() { return supportsFollowing; }
    public boolean supportsAnalytics() { return supportsAnalytics; }
    public boolean supportsAds() { return supportsAds; }
    public boolean supportsEcommerce() { return supportsEcommerce; }
    public boolean supportsPolls() { return supportsPolls; }
    public boolean supportsEvents() { return supportsEvents; }
    public boolean supportsGroups() { return supportsGroups; }
    public boolean supportsPages() { return supportsPages; }
    public boolean supportsMultipleAccounts() { return supportsMultipleAccounts; }
    public boolean supportsBusinessApi() { return supportsBusinessApi; }
    public boolean supportsWebhooks() { return supportsWebhooks; }
    public boolean supportsContentModeration() { return supportsContentModeration; }
    public boolean supportsAutoPosting() { return supportsAutoPosting; }
    public boolean supportsAutoFollowing() { return supportsAutoFollowing; }
    public boolean supportsAutoLiking() { return supportsAutoLiking; }
    public boolean supportsAutoCommenting() { return supportsAutoCommenting; }
    public Set<String> getSupportedImageFormats() { return supportedImageFormats; }
    public Set<String> getSupportedVideoFormats() { return supportedVideoFormats; }
    public Integer getMaxImageSize() { return maxImageSize; }
    public Integer getMaxVideoSize() { return maxVideoSize; }
    public Integer getMaxVideoLength() { return maxVideoLength; }
    public Integer getMaxCaptionLength() { return maxCaptionLength; }
    public Integer getMaxHashtagsPerPost() { return maxHashtagsPerPost; }
    public Integer getMaxMentionsPerPost() { return maxMentionsPerPost; }
    
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        PlatformFeatures that = (PlatformFeatures) o;
        return supportsTextPosts == that.supportsTextPosts &&
               supportsImagePosts == that.supportsImagePosts &&
               supportsVideoPosts == that.supportsVideoPosts &&
               supportsCarouselPosts == that.supportsCarouselPosts &&
               supportsStories == that.supportsStories &&
               supportsReels == that.supportsReels &&
               supportsLiveStreaming == that.supportsLiveStreaming &&
               supportsScheduling == that.supportsScheduling &&
               supportsHashtags == that.supportsHashtags &&
               supportsMentions == that.supportsMentions &&
               supportsLocationTagging == that.supportsLocationTagging &&
               supportsDirectMessages == that.supportsDirectMessages &&
               supportsComments == that.supportsComments &&
               supportsLikes == that.supportsLikes &&
               supportsShares == that.supportsShares &&
               supportsFollowing == that.supportsFollowing &&
               supportsAnalytics == that.supportsAnalytics &&
               supportsAds == that.supportsAds &&
               supportsEcommerce == that.supportsEcommerce &&
               supportsPolls == that.supportsPolls &&
               supportsEvents == that.supportsEvents &&
               supportsGroups == that.supportsGroups &&
               supportsPages == that.supportsPages &&
               supportsMultipleAccounts == that.supportsMultipleAccounts &&
               supportsBusinessApi == that.supportsBusinessApi &&
               supportsWebhooks == that.supportsWebhooks &&
               supportsContentModeration == that.supportsContentModeration &&
               supportsAutoPosting == that.supportsAutoPosting &&
               supportsAutoFollowing == that.supportsAutoFollowing &&
               supportsAutoLiking == that.supportsAutoLiking &&
               supportsAutoCommenting == that.supportsAutoCommenting &&
               Objects.equals(supportedImageFormats, that.supportedImageFormats) &&
               Objects.equals(supportedVideoFormats, that.supportedVideoFormats) &&
               Objects.equals(maxImageSize, that.maxImageSize) &&
               Objects.equals(maxVideoSize, that.maxVideoSize) &&
               Objects.equals(maxVideoLength, that.maxVideoLength) &&
               Objects.equals(maxCaptionLength, that.maxCaptionLength) &&
               Objects.equals(maxHashtagsPerPost, that.maxHashtagsPerPost) &&
               Objects.equals(maxMentionsPerPost, that.maxMentionsPerPost);
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(supportsTextPosts, supportsImagePosts, supportsVideoPosts,
                          supportsCarouselPosts, supportsStories, supportsReels,
                          supportsLiveStreaming, supportsScheduling, supportsHashtags,
                          supportsMentions, supportsLocationTagging, supportsDirectMessages,
                          supportsComments, supportsLikes, supportsShares, supportsFollowing,
                          supportsAnalytics, supportsAds, supportsEcommerce, supportsPolls,
                          supportsEvents, supportsGroups, supportsPages, supportsMultipleAccounts,
                          supportsBusinessApi, supportsWebhooks, supportsContentModeration,
                          supportsAutoPosting, supportsAutoFollowing, supportsAutoLiking,
                          supportsAutoCommenting, supportedImageFormats, supportedVideoFormats,
                          maxImageSize, maxVideoSize, maxVideoLength, maxCaptionLength,
                          maxHashtagsPerPost, maxMentionsPerPost);
    }

    @Override
    public String toString() {
        return "PlatformFeatures{" +
                "supportsTextPosts=" + supportsTextPosts +
                ", supportsImagePosts=" + supportsImagePosts +
                ", supportsVideoPosts=" + supportsVideoPosts +
                ", supportsCarouselPosts=" + supportsCarouselPosts +
                ", supportsStories=" + supportsStories +
                ", supportsReels=" + supportsReels +
                ", supportsLiveStreaming=" + supportsLiveStreaming +
                ", supportsScheduling=" + supportsScheduling +
                ", supportsHashtags=" + supportsHashtags +
                ", supportsMentions=" + supportsMentions +
                ", supportsLocationTagging=" + supportsLocationTagging +
                ", supportsDirectMessages=" + supportsDirectMessages +
                ", supportsComments=" + supportsComments +
                ", supportsLikes=" + supportsLikes +
                ", supportsShares=" + supportsShares +
                ", supportsFollowing=" + supportsFollowing +
                ", supportsAnalytics=" + supportsAnalytics +
                ", supportsAds=" + supportsAds +
                ", supportsEcommerce=" + supportsEcommerce +
                ", supportsPolls=" + supportsPolls +
                ", supportsEvents=" + supportsEvents +
                ", supportsGroups=" + supportsGroups +
                ", supportsPages=" + supportsPages +
                ", supportsMultipleAccounts=" + supportsMultipleAccounts +
                ", supportsBusinessApi=" + supportsBusinessApi +
                ", supportsWebhooks=" + supportsWebhooks +
                ", supportsContentModeration=" + supportsContentModeration +
                ", supportsAutoPosting=" + supportsAutoPosting +
                ", supportsAutoFollowing=" + supportsAutoFollowing +
                ", supportsAutoLiking=" + supportsAutoLiking +
                ", supportsAutoCommenting=" + supportsAutoCommenting +
                ", supportedImageFormats=" + supportedImageFormats +
                ", supportedVideoFormats=" + supportedVideoFormats +
                ", maxImageSize=" + maxImageSize +
                ", maxVideoSize=" + maxVideoSize +
                ", maxVideoLength=" + maxVideoLength +
                ", maxCaptionLength=" + maxCaptionLength +
                ", maxHashtagsPerPost=" + maxHashtagsPerPost +
                ", maxMentionsPerPost=" + maxMentionsPerPost +
                '}';
    }
}
