package de.cannamanage.domain.entity;

import jakarta.persistence.*;

import java.io.Serializable;
import java.time.Instant;
import java.util.Objects;
import java.util.UUID;

/**
 * Tracks which members have read which info board posts.
 * Composite primary key: (post_id, member_id).
 */
@Entity
@Table(name = "post_read_status")
@IdClass(PostReadStatus.PostReadStatusId.class)
public class PostReadStatus {

    @Id
    @Column(name = "post_id", nullable = false)
    private UUID postId;

    @Id
    @Column(name = "member_id", nullable = false)
    private UUID memberId;

    @Column(name = "read_at", nullable = false)
    private Instant readAt;

    public PostReadStatus() {}

    public PostReadStatus(UUID postId, UUID memberId) {
        this.postId = postId;
        this.memberId = memberId;
        this.readAt = Instant.now();
    }

    // Getters and setters

    public UUID getPostId() { return postId; }
    public void setPostId(UUID postId) { this.postId = postId; }

    public UUID getMemberId() { return memberId; }
    public void setMemberId(UUID memberId) { this.memberId = memberId; }

    public Instant getReadAt() { return readAt; }
    public void setReadAt(Instant readAt) { this.readAt = readAt; }

    /**
     * Composite key class for PostReadStatus.
     */
    public static class PostReadStatusId implements Serializable {
        private UUID postId;
        private UUID memberId;

        public PostReadStatusId() {}

        public PostReadStatusId(UUID postId, UUID memberId) {
            this.postId = postId;
            this.memberId = memberId;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            PostReadStatusId that = (PostReadStatusId) o;
            return Objects.equals(postId, that.postId) && Objects.equals(memberId, that.memberId);
        }

        @Override
        public int hashCode() {
            return Objects.hash(postId, memberId);
        }
    }
}
