package org.raddatz.familienarchiv.geschichte.journeyitem;

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.persistence.*;
import lombok.*;
import org.raddatz.familienarchiv.document.Document;
import org.raddatz.familienarchiv.geschichte.Geschichte;

import java.util.UUID;

@Entity
@Table(name = "journey_items")
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class JourneyItem {

    @Id
    @GeneratedValue(strategy = GenerationType.UUID)
    @Schema(requiredMode = Schema.RequiredMode.REQUIRED)
    private UUID id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "geschichte_id", nullable = false)
    @JsonIgnore
    private Geschichte geschichte;

    // Sort key; gaps fine. Duplicate positions within a journey yield undefined relative order
    // — the editor is responsible for keeping them distinct.
    @Column(nullable = false)
    @Schema(requiredMode = Schema.RequiredMode.REQUIRED)
    private int position;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "document_id")
    @JsonIgnore
    private Document document;

    /**
     * Plain text — not HTML-sanitized. Renderers MUST NOT use {@code @html} or equivalent unsafe output.
     *
     * <p>CWE-79 tripwire: stored verbatim; only Svelte {note} interpolation is auto-safe.</p>
     */
    @Column(columnDefinition = "TEXT")
    private String note;

    // JPA uses field access — this getter is not persisted. Jackson serializes it as documentId.
    // Exposing only the UUID prevents circular references and large nested payloads.
    public UUID getDocumentId() {
        return document != null ? document.getId() : null;
    }
}
