package com.social.media.domain.content.valueobject;

import java.util.List;
import java.util.regex.Pattern;

/**
 * Value Object representing post content with hashtags and mentions
 */
public record PostContent(
    String text,
    List<String> hashtags,
    List<String> mentions
) {
    
    private static final Pattern HASHTAG_PATTERN = Pattern.compile("#[a-zA-Z0-9_]+");
    private static final Pattern MENTION_PATTERN = Pattern.compile("@[a-zA-Z0-9_.]+");
    
    public PostContent {
        if (text == null) {
            text = "";
        }
        hashtags = hashtags != null ? List.copyOf(hashtags) : extractHashtags(text);
        mentions = mentions != null ? List.copyOf(mentions) : extractMentions(text);
    }
    
    public static PostContent of(String text) {
        return new PostContent(text, null, null);
    }
    
    public static PostContent empty() {
        return new PostContent("", List.of(), List.of());
    }
    
    public boolean isEmpty() {
        return text.trim().isEmpty();
    }
    
    public boolean hasHashtags() {
        return !hashtags.isEmpty();
    }
    
    public boolean hasMentions() {
        return !mentions.isEmpty();
    }
    
    public int getCharacterCount() {
        return text.length();
    }
    
    public boolean isWithinLimit(int characterLimit) {
        return characterLimit <= 0 || getCharacterCount() <= characterLimit;
    }
    
    public String getTextWithoutHashtags() {
        String result = text;
        for (String hashtag : hashtags) {
            result = result.replaceAll("#" + hashtag, "").trim();
        }
        return result.replaceAll("\\s+", " ").trim();
    }
    
    public String getTextWithoutMentions() {
        String result = text;
        for (String mention : mentions) {
            result = result.replaceAll("@" + mention, "").trim();
        }
        return result.replaceAll("\\s+", " ").trim();
    }
    
    public PostContent withText(String newText) {
        return new PostContent(newText, null, null);
    }
    
    public PostContent withHashtags(List<String> newHashtags) {
        return new PostContent(text, newHashtags, mentions);
    }
    
    public PostContent withMentions(List<String> newMentions) {
        return new PostContent(text, hashtags, newMentions);
    }
    
    public PostContent addHashtag(String hashtag) {
        List<String> newHashtags = List.copyOf(hashtags);
        newHashtags.add(hashtag.startsWith("#") ? hashtag.substring(1) : hashtag);
        return new PostContent(text, newHashtags, mentions);
    }
    
    public PostContent addMention(String mention) {
        List<String> newMentions = List.copyOf(mentions);
        newMentions.add(mention.startsWith("@") ? mention.substring(1) : mention);
        return new PostContent(text, hashtags, newMentions);
    }
    
    private static List<String> extractHashtags(String text) {
        if (text == null || text.isEmpty()) {
            return List.of();
        }
        
        return HASHTAG_PATTERN.matcher(text)
                .results()
                .map(match -> match.group().substring(1)) // Remove the #
                .distinct()
                .toList();
    }
    
    private static List<String> extractMentions(String text) {
        if (text == null || text.isEmpty()) {
            return List.of();
        }
        
        return MENTION_PATTERN.matcher(text)
                .results()
                .map(match -> match.group().substring(1)) // Remove the @
                .distinct()
                .toList();
    }
    
    @Override
    public String toString() {
        return text;
    }
    
    public List<String> getHashtags() {
        return hashtags;
    }
    
    public List<String> getMentions() {
        return mentions;
    }
}
