package de.cannamanage.domain.entity;

import de.cannamanage.domain.enums.TargetType;
import jakarta.persistence.*;
import lombok.*;

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

/**
 * Tracks a notification send operation (admin broadcast or targeted send).
 * One NotificationSend creates many individual Notification records.
 */
@Entity
@Table(name = "notification_sends", indexes = {
    @Index(name = "idx_notification_sends_tenant", columnList = "tenant_id, sent_at DESC")
})
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class NotificationSend extends AbstractTenantEntity {

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

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

    @Column(name = "link", length = 500)
    private String link;

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

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

    @Column(name = "target_count", nullable = false)
    private int targetCount;

    @Column(name = "read_count", nullable = false)
    private int readCount;

    @Column(name = "sent_at", nullable = false)
    private Instant sentAt;

    @PrePersist
    void onPersist() {
        if (this.sentAt == null) {
            this.sentAt = Instant.now();
        }
    }
}
