package com.loremind.infrastructure.persistence.entity;

import com.loremind.domain.campaigncontext.Prerequisite;
import com.loremind.infrastructure.persistence.converter.PrerequisiteListJsonConverter;
import com.loremind.infrastructure.persistence.converter.StringListJsonConverter;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * Entité JPA pour la persistance des Chapters en base de données PostgreSQL.
 */
@Entity
@Table(name = "chapters")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ChapterJpaEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false)
    private String name;

    @Column(columnDefinition = "TEXT")
    private String description;

    @Column(name = "arc_id", nullable = false)
    private Long arcId;

    @Column(name = "\"order\"", nullable = false)
    private int order;

    /**
     * Conditions de déblocage (combinées en ET). Sérialisées en JSON dans une colonne TEXT
     * via {@link PrerequisiteListJsonConverter} (sealed type côté domaine).
     * Donnée de SCÉNARIO — l'état réel est sur Playthrough.QuestProgression.
     */
    @Column(name = "prerequisites", columnDefinition = "TEXT")
    @Convert(converter = PrerequisiteListJsonConverter.class)
    @Builder.Default
    private List<Prerequisite> prerequisites = new ArrayList<>();

    @Column
    private String icon;

    // Champs narratifs enrichis — ajoutés automatiquement par Hibernate DDL (ddl-auto=update)
    @Column(name = "gm_notes", columnDefinition = "TEXT")
    private String gmNotes;

    @Column(name = "player_objectives", columnDefinition = "TEXT")
    private String playerObjectives;

    @Column(name = "narrative_stakes", columnDefinition = "TEXT")
    private String narrativeStakes;

    @Column(name = "related_page_ids", columnDefinition = "TEXT")
    @Convert(converter = StringListJsonConverter.class)
    @Builder.Default
    private List<String> relatedPageIds = new ArrayList<>();

    @Column(name = "illustration_image_ids", columnDefinition = "TEXT")
    @Convert(converter = StringListJsonConverter.class)
    @Builder.Default
    private List<String> illustrationImageIds = new ArrayList<>();

    @Column(name = "map_image_ids", columnDefinition = "TEXT")
    @Convert(converter = StringListJsonConverter.class)
    @Builder.Default
    private List<String> mapImageIds = new ArrayList<>();

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

    @Column(name = "updated_at", nullable = false)
    private LocalDateTime updatedAt;

    @PrePersist
    protected void onCreate() {
        createdAt = LocalDateTime.now();
        updatedAt = LocalDateTime.now();
    }

    @PreUpdate
    protected void onUpdate() {
        updatedAt = LocalDateTime.now();
    }
}
