package de.cannamanage.domain.entity;

import jakarta.persistence.*;

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

/**
 * Forum topic entity — club-scoped discussion thread.
 * Content is stored as HTML (from Tiptap rich text editor).
 * Extends AbstractTenantEntity for automatic tenant isolation.
 */
@Entity
@Table(name = "forum_topics", indexes = {
    @Index(name = "idx_forum_topics_club_id", columnList = "club_id"),
    @Index(name = "idx_forum_topics_tenant_id", columnList = "tenant_id")
})
public class ForumTopic extends AbstractTenantEntity {

    @Column(name = "club_id", nullable = false)
    private UUID clubId;

    @Column(name = "title", nullable = false, length = 300)
    private String title;

    @Column(name = "content", nullable = false, columnDefinition = "TEXT")
    private String content;

    @Column(name = "author_id", nullable = false)
    private UUID authorId;

    @Column(name = "is_locked", nullable = false)
    private boolean locked = false;

    @Column(name = "is_pinned", nullable = false)
    private boolean pinned = false;

    @Column(name = "reply_count", nullable = false)
    private int replyCount = 0;

    @Column(name = "last_reply_at")
    private Instant lastReplyAt;

    @Column(name = "updated_at")
    private Instant updatedAt;

    public ForumTopic() {}

    public ForumTopic(UUID clubId, String title, String content, UUID authorId) {
        this.clubId = clubId;
        this.title = title;
        this.content = content;
        this.authorId = authorId;
        this.updatedAt = Instant.now();
    }

    @PrePersist
    @Override
    void onCreate() {
        super.onCreate();
        if (this.updatedAt == null) {
            this.updatedAt = Instant.now();
        }
    }

    @PreUpdate
    void onUpdate() {
        this.updatedAt = Instant.now();
    }

    // Getters and setters

    public UUID getClubId() { return clubId; }
    public void setClubId(UUID clubId) { this.clubId = clubId; }

    public String getTitle() { return title; }
    public void setTitle(String title) { this.title = title; }

    public String getContent() { return content; }
    public void setContent(String content) { this.content = content; }

    public UUID getAuthorId() { return authorId; }
    public void setAuthorId(UUID authorId) { this.authorId = authorId; }

    public boolean isLocked() { return locked; }
    public void setLocked(boolean locked) { this.locked = locked; }

    public boolean isPinned() { return pinned; }
    public void setPinned(boolean pinned) { this.pinned = pinned; }

    public int getReplyCount() { return replyCount; }
    public void setReplyCount(int replyCount) { this.replyCount = replyCount; }

    public Instant getLastReplyAt() { return lastReplyAt; }
    public void setLastReplyAt(Instant lastReplyAt) { this.lastReplyAt = lastReplyAt; }

    public Instant getUpdatedAt() { return updatedAt; }
    public void setUpdatedAt(Instant updatedAt) { this.updatedAt = updatedAt; }
}
