package com.storycove.entity;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import org.hibernate.annotations.CreationTimestamp;
import com.fasterxml.jackson.annotation.JsonManagedReference;

import java.time.LocalDateTime;
import java.util.UUID;

@Entity
@Table(name = "tag_aliases")
public class TagAlias {
    
    @Id
    @GeneratedValue(strategy = GenerationType.UUID)
    private UUID id;
    
    @NotBlank(message = "Alias name is required")
    @Size(max = 100, message = "Alias name must not exceed 100 characters")
    @Column(name = "alias_name", nullable = false, unique = true)
    private String aliasName;
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "canonical_tag_id", nullable = false)
    @JsonManagedReference("tag-aliases")
    private Tag canonicalTag;
    
    @Column(name = "created_from_merge", nullable = false)
    private Boolean createdFromMerge = false;
    
    @CreationTimestamp
    @Column(name = "created_at", nullable = false, updatable = false)
    private LocalDateTime createdAt;
    
    public TagAlias() {}
    
    public TagAlias(String aliasName, Tag canonicalTag) {
        this.aliasName = aliasName;
        this.canonicalTag = canonicalTag;
    }
    
    public TagAlias(String aliasName, Tag canonicalTag, Boolean createdFromMerge) {
        this.aliasName = aliasName;
        this.canonicalTag = canonicalTag;
        this.createdFromMerge = createdFromMerge;
    }
    
    // Getters and Setters
    public UUID getId() {
        return id;
    }
    
    public void setId(UUID id) {
        this.id = id;
    }
    
    public String getAliasName() {
        return aliasName;
    }
    
    public void setAliasName(String aliasName) {
        this.aliasName = aliasName;
    }
    
    public Tag getCanonicalTag() {
        return canonicalTag;
    }
    
    public void setCanonicalTag(Tag canonicalTag) {
        this.canonicalTag = canonicalTag;
    }
    
    public Boolean getCreatedFromMerge() {
        return createdFromMerge;
    }
    
    public void setCreatedFromMerge(Boolean createdFromMerge) {
        this.createdFromMerge = createdFromMerge;
    }
    
    public LocalDateTime getCreatedAt() {
        return createdAt;
    }
    
    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }
    
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof TagAlias)) return false;
        TagAlias tagAlias = (TagAlias) o;
        return id != null && id.equals(tagAlias.id);
    }
    
    @Override
    public int hashCode() {
        return getClass().hashCode();
    }
    
    @Override
    public String toString() {
        return "TagAlias{" +
                "id=" + id +
                ", aliasName='" + aliasName + '\'' +
                ", canonicalTag=" + (canonicalTag != null ? canonicalTag.getName() : null) +
                ", createdFromMerge=" + createdFromMerge +
                '}';
    }
}