package com.storycove.entity;

import jakarta.persistence.*;
import org.hibernate.annotations.CreationTimestamp;
import com.fasterxml.jackson.annotation.JsonBackReference;

import java.time.LocalDateTime;

@Entity
@Table(name = "collection_stories", 
       uniqueConstraints = {
           @UniqueConstraint(columnNames = {"collection_id", "position"})
       })
public class CollectionStory {
    
    @EmbeddedId
    private CollectionStoryId id;
    
    @ManyToOne(fetch = FetchType.LAZY)
    @MapsId("collectionId")
    @JoinColumn(name = "collection_id")
    @JsonBackReference("collection-stories")
    private Collection collection;
    
    @ManyToOne(fetch = FetchType.LAZY)
    @MapsId("storyId")
    @JoinColumn(name = "story_id")
    private Story story;
    
    @Column(nullable = false)
    private Integer position;
    
    @CreationTimestamp
    @Column(name = "added_at", nullable = false, updatable = false)
    private LocalDateTime addedAt;
    
    public CollectionStory() {}
    
    public CollectionStory(Collection collection, Story story, Integer position) {
        this.id = new CollectionStoryId(collection.getId(), story.getId());
        this.collection = collection;
        this.story = story;
        this.position = position;
    }
    
    // Getters and Setters
    public CollectionStoryId getId() {
        return id;
    }
    
    public void setId(CollectionStoryId id) {
        this.id = id;
    }
    
    public Collection getCollection() {
        return collection;
    }
    
    public void setCollection(Collection collection) {
        this.collection = collection;
        if (this.story != null) {
            this.id = new CollectionStoryId(collection.getId(), this.story.getId());
        }
    }
    
    public Story getStory() {
        return story;
    }
    
    public void setStory(Story story) {
        this.story = story;
        if (this.collection != null) {
            this.id = new CollectionStoryId(this.collection.getId(), story.getId());
        }
    }
    
    public Integer getPosition() {
        return position;
    }
    
    public void setPosition(Integer position) {
        this.position = position;
    }
    
    public LocalDateTime getAddedAt() {
        return addedAt;
    }
    
    public void setAddedAt(LocalDateTime addedAt) {
        this.addedAt = addedAt;
    }
    
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof CollectionStory)) return false;
        CollectionStory that = (CollectionStory) o;
        return id != null && id.equals(that.id);
    }
    
    @Override
    public int hashCode() {
        return getClass().hashCode();
    }
    
    @Override
    public String toString() {
        return "CollectionStory{" +
                "collectionId=" + (collection != null ? collection.getId() : null) +
                ", storyId=" + (story != null ? story.getId() : null) +
                ", position=" + position +
                '}';
    }
}