package de.cannamanage.domain.entity;

import de.cannamanage.domain.enums.ForumTargetType;
import de.cannamanage.domain.enums.ReactionType;
import jakarta.persistence.*;

import java.time.Instant;
import java.util.UUID;

/**
 * Forum reaction entity — one reaction per user per target (topic or reply).
 * Toggle behavior: clicking again removes the reaction.
 */
@Entity
@Table(name = "forum_reactions", uniqueConstraints = {
    @UniqueConstraint(name = "uq_forum_reactions_target_user",
            columnNames = {"target_type", "target_id", "user_id"})
}, indexes = {
    @Index(name = "idx_forum_reactions_target", columnList = "target_type, target_id")
})
public class ForumReaction {

    @Id
    @GeneratedValue(strategy = GenerationType.UUID)
    @Column(name = "id", nullable = false, updatable = false)
    private UUID id;

    @Enumerated(EnumType.STRING)
    @Column(name = "target_type", nullable = false, length = 10)
    private ForumTargetType targetType;

    @Column(name = "target_id", nullable = false)
    private UUID targetId;

    @Column(name = "user_id", nullable = false)
    private UUID userId;

    @Enumerated(EnumType.STRING)
    @Column(name = "reaction_type", nullable = false, length = 20)
    private ReactionType reactionType;

    @Column(name = "created_at", nullable = false, updatable = false)
    private Instant createdAt;

    @PrePersist
    void onCreate() {
        this.createdAt = Instant.now();
    }

    public ForumReaction() {}

    public ForumReaction(ForumTargetType targetType, UUID targetId, UUID userId, ReactionType reactionType) {
        this.targetType = targetType;
        this.targetId = targetId;
        this.userId = userId;
        this.reactionType = reactionType;
    }

    // Getters and setters

    public UUID getId() { return id; }
    public void setId(UUID id) { this.id = id; }

    public ForumTargetType getTargetType() { return targetType; }
    public void setTargetType(ForumTargetType targetType) { this.targetType = targetType; }

    public UUID getTargetId() { return targetId; }
    public void setTargetId(UUID targetId) { this.targetId = targetId; }

    public UUID getUserId() { return userId; }
    public void setUserId(UUID userId) { this.userId = userId; }

    public ReactionType getReactionType() { return reactionType; }
    public void setReactionType(ReactionType reactionType) { this.reactionType = reactionType; }

    public Instant getCreatedAt() { return createdAt; }
    public void setCreatedAt(Instant createdAt) { this.createdAt = createdAt; }
}
