package com.social.media.infrastructure.persistence.entity;

import jakarta.persistence.*;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;

/**
 * JPA Entity for UserListMember aggregate
 */
@Entity
@Table(name = "user_list_members",
       uniqueConstraints = @UniqueConstraint(columnNames = {"user_list_id", "social_network_type", "username"}))
@EntityListeners(AuditingEntityListener.class)
public class UserListMemberEntity {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;
    
    @Column(name = "user_list_id", nullable = false)
    private Long userListId;
    
    @Column(name = "username", nullable = false, length = 100)
    private String username;
    
    @Column(name = "display_name", length = 150)
    private String displayName;
    
    @Enumerated(EnumType.STRING)
    @Column(name = "social_network_type", nullable = false)
    private SocialNetworkTypeEntity socialNetworkType;
    
    @Column(name = "external_user_id", length = 100)
    private String externalUserId;
    
    @Column(name = "profile_url", length = 500)
    private String profileUrl;
    
    @Column(name = "notes", columnDefinition = "TEXT")
    private String notes;
    
    @Column(name = "is_active", nullable = false)
    private boolean isActive = true;
    
    @Column(name = "added_at", nullable = false)
    private LocalDateTime addedAt;
    
    @CreatedDate
    @Column(name = "created_at", nullable = false, updatable = false)
    private LocalDateTime createdAt;
    
    @LastModifiedDate
    @Column(name = "updated_at", nullable = false)
    private LocalDateTime updatedAt;
    
    // Constructors
    public UserListMemberEntity() {}
    
    public UserListMemberEntity(Long id, Long userListId, String username, String displayName,
                               SocialNetworkTypeEntity socialNetworkType, String externalUserId,
                               String profileUrl, String notes, boolean isActive, LocalDateTime addedAt) {
        this.id = id;
        this.userListId = userListId;
        this.username = username;
        this.displayName = displayName;
        this.socialNetworkType = socialNetworkType;
        this.externalUserId = externalUserId;
        this.profileUrl = profileUrl;
        this.notes = notes;
        this.isActive = isActive;
        this.addedAt = addedAt;
    }
    
    @PrePersist
    protected void onCreate() {
        if (addedAt == null) {
            addedAt = LocalDateTime.now();
        }
    }
    
    // Getters and Setters
    public Long getId() {
        return id;
    }
    
    public void setId(Long id) {
        this.id = id;
    }
    
    public Long getUserListId() {
        return userListId;
    }
    
    public void setUserListId(Long userListId) {
        this.userListId = userListId;
    }
    
    public String getUsername() {
        return username;
    }
    
    public void setUsername(String username) {
        this.username = username;
    }
    
    public String getDisplayName() {
        return displayName;
    }
    
    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }
    
    public SocialNetworkTypeEntity getSocialNetworkType() {
        return socialNetworkType;
    }
    
    public void setSocialNetworkType(SocialNetworkTypeEntity socialNetworkType) {
        this.socialNetworkType = socialNetworkType;
    }
    
    public String getExternalUserId() {
        return externalUserId;
    }
    
    public void setExternalUserId(String externalUserId) {
        this.externalUserId = externalUserId;
    }
    
    public String getProfileUrl() {
        return profileUrl;
    }
    
    public void setProfileUrl(String profileUrl) {
        this.profileUrl = profileUrl;
    }
    
    public String getNotes() {
        return notes;
    }
    
    public void setNotes(String notes) {
        this.notes = notes;
    }
    
    public boolean isActive() {
        return isActive;
    }
    
    public void setActive(boolean active) {
        isActive = active;
    }
    
    public LocalDateTime getAddedAt() {
        return addedAt;
    }
    
    public void setAddedAt(LocalDateTime addedAt) {
        this.addedAt = addedAt;
    }
    
    public LocalDateTime getCreatedAt() {
        return createdAt;
    }
    
    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }
    
    public LocalDateTime getUpdatedAt() {
        return updatedAt;
    }
    
    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }
}
