package com.social.media.domain.shared.valueobjects;

import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.Objects;

/**
 * Value object representing a non-empty text with maximum length validation.
 * 
 * Used for names, descriptions, and other textual content.
 * 
 * @author Social Media Manager Team
 * @since 2.0.0
 */
@Embeddable
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Text {
    
    @Column(name = "text_value", nullable = false, length = 1000)
    private String value;
    
    /**
     * Get the text value
     */
    public String getValue() {
        return value;
    }
    
    private Text(String value, int maxLength) {
        this.value = Objects.requireNonNull(value, "Text cannot be null").trim();
        validate(maxLength);
    }
    
    /**
     * Create a Text with custom max length
     */
    public static Text of(String value, int maxLength) {
        return new Text(value, maxLength);
    }
    
    /**
     * Create a Text with default max length of 1000 characters
     */
    public static Text of(String value) {
        return new Text(value, 1000);
    }
    
    /**
     * Create a short text (max 255 characters)
     */
    public static Text shortText(String value) {
        return new Text(value, 255);
    }
    
    /**
     * Create a medium text (max 1000 characters)
     */
    public static Text mediumText(String value) {
        return new Text(value, 1000);
    }
    
    /**
     * Create a long text (max 5000 characters)
     */
    public static Text longText(String value) {
        return new Text(value, 5000);
    }
    
    /**
     * Validate text constraints
     */
    private void validate(int maxLength) {
        if (value.isEmpty()) {
            throw new IllegalArgumentException("Text cannot be empty");
        }
        
        if (value.length() > maxLength) {
            throw new IllegalArgumentException(
                String.format("Text cannot exceed %d characters, got %d", maxLength, value.length())
            );
        }
    }
    
    /**
     * Check if text is empty or only whitespace
     */
    public boolean isEmpty() {
        return value.trim().isEmpty();
    }
    
    /**
     * Get text length
     */
    public int length() {
        return value.length();
    }
    
    /**
     * Check if text contains substring (case insensitive)
     */
    public boolean contains(String substring) {
        return value.toLowerCase().contains(substring.toLowerCase());
    }
    
    /**
     * Truncate text to specified length with ellipsis
     */
    public String truncate(int length) {
        if (value.length() <= length) {
            return value;
        }
        return value.substring(0, length - 3) + "...";
    }
    
    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        Text text = (Text) obj;
        return Objects.equals(value, text.value);
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(value);
    }
    
    @Override
    public String toString() {
        return value;
    }
}
