package de.cannamanage.domain.entity;

import de.cannamanage.domain.enums.InfoBoardCategory;
import jakarta.persistence.*;

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

/**
 * Info board post entity — club-scoped announcements (Schwarzes Brett).
 * Content is stored as HTML (from Tiptap rich text editor).
 */
@Entity
@Table(name = "info_board_posts", indexes = {
    @Index(name = "idx_info_board_posts_club_id", columnList = "club_id"),
    @Index(name = "idx_info_board_posts_category", columnList = "category"),
    @Index(name = "idx_info_board_posts_tenant", columnList = "tenant_id")
})
public class InfoBoardPost extends AbstractTenantEntity {

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

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

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

    @Enumerated(EnumType.STRING)
    @Column(name = "category", nullable = false, length = 50)
    private InfoBoardCategory category;

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

    @Column(name = "is_archived", nullable = false)
    private boolean archived = false;

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

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

    @OneToMany(mappedBy = "post", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<PostAttachment> attachments = new ArrayList<>();

    public InfoBoardPost() {}

    public InfoBoardPost(UUID clubId, String title, String content, InfoBoardCategory category, UUID authorId) {
        this.clubId = clubId;
        this.title = title;
        this.content = content;
        this.category = category;
        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 InfoBoardCategory getCategory() { return category; }
    public void setCategory(InfoBoardCategory category) { this.category = category; }

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

    public boolean isArchived() { return archived; }
    public void setArchived(boolean archived) { this.archived = archived; }

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

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

    public List<PostAttachment> getAttachments() { return attachments; }
    public void setAttachments(List<PostAttachment> attachments) { this.attachments = attachments; }
}
