package com.pablotj.ai.chat.domain.model;

import java.util.Objects;
import java.util.UUID;

/**
 * Value Object representing a unique conversation identifier.
 * Ensures type safety and encapsulates conversation ID logic.
 */
public final class ConversationId {

    private final String uuid;
    private Long id;

    public ConversationId() {
        this(UUID.randomUUID().toString());
    }

    public ConversationId(Long id, String uuid) {
        this.id = Objects.requireNonNull(id, "Conversation ID cannot be null");
        this.uuid = Objects.requireNonNull(uuid, "Conversation UUID cannot be null");
    }

    private ConversationId(String uuid) {
        this.id = null;
        this.uuid = Objects.requireNonNull(uuid, "Conversation UUID cannot be null");
    }

    public static ConversationId generate() {
        return new ConversationId(UUID.randomUUID().toString());
    }

    public static ConversationId of(String value) {
        if (value == null || value.trim().isEmpty()) {
            throw new IllegalArgumentException("Conversation UUID cannot be null or empty");
        }
        return new ConversationId(value.trim());
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUuid() {
        return uuid;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        ConversationId that = (ConversationId) obj;
        return Objects.equals(uuid, that.uuid);
    }

    @Override
    public int hashCode() {
        return Objects.hash(uuid);
    }

    @Override
    public String toString() {
        return uuid;
    }
}
