package com.social.media.domain.entity;

import jakarta.persistence.*;
import jakarta.validation.constraints.*;

import java.time.LocalDateTime;
import java.util.Objects;

@Entity
@Table(name = "following", schema = "core_business",
       uniqueConstraints = {
           @UniqueConstraint(name = "following_following_code_key", columnNames = {"following_code"}),
           @UniqueConstraint(name = "chk_following_social_account_profile_unique", 
                           columnNames = {"social_account_id", "profile_name"})
       },
       indexes = {
           @Index(name = "idx_following_social_account_id", columnList = "social_account_id"),
           @Index(name = "idx_following_profile_name", columnList = "profile_name"),
           @Index(name = "idx_following_verified", columnList = "verified")
       })
public class Following {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @Column(name = "following_code", nullable = false, unique = true, length = 20)
    private String followingCode;
    
    @NotNull(message = "Social Account ID é obrigatório")
    @Column(name = "social_account_id", nullable = false)
    private Long socialAccountId;
    
    @NotBlank(message = "Nome do perfil é obrigatório")
    @Size(max = 255, message = "Nome do perfil deve ter no máximo 255 caracteres")
    @Column(name = "profile_name", nullable = false, length = 255)
    private String profileName;
    
    @Size(max = 255, message = "Nome deve ter no máximo 255 caracteres")
    @Column(name = "name", length = 255)
    private String name;
    
    @Size(max = 1000, message = "URL do perfil deve ter no máximo 1000 caracteres")
    @Column(name = "profile_url", length = 1000)
    private String profileUrl;
    
    @Column(name = "verified", nullable = false)
    private Boolean verified = false;
    
    @Column(name = "created_at", nullable = false)
    private LocalDateTime createdAt;
    
    @Column(name = "updated_at", nullable = false)
    private LocalDateTime updatedAt;
    
    // Constructors
    public Following() {}
    
    public Following(Long socialAccountId, String profileName) {
        this.socialAccountId = socialAccountId;
        this.profileName = profileName;
        this.createdAt = LocalDateTime.now();
        this.updatedAt = LocalDateTime.now();
    }
    
    public Following(Long socialAccountId, String profileName, String name, String profileUrl, Boolean verified) {
        this(socialAccountId, profileName);
        this.name = name;
        this.profileUrl = profileUrl;
        this.verified = verified;
    }
    
    // Lifecycle callbacks
    @PrePersist
    protected void onCreate() {
        LocalDateTime now = LocalDateTime.now();
        if (createdAt == null) {
            createdAt = now;
        }
        updatedAt = now;
        
        // Set default values if not provided
        if (verified == null) {
            verified = false;
        }
    }
    
    @PreUpdate
    protected void onUpdate() {
        updatedAt = LocalDateTime.now();
    }
    
    // Getters and Setters
    public Long getId() {
        return id;
    }
    
    public void setId(Long id) {
        this.id = id;
    }
    
    public String getFollowingCode() {
        return followingCode;
    }
    
    public void setFollowingCode(String followingCode) {
        this.followingCode = followingCode;
    }
    
    public Long getSocialAccountId() {
        return socialAccountId;
    }
    
    public void setSocialAccountId(Long socialAccountId) {
        this.socialAccountId = socialAccountId;
    }
    
    public String getProfileName() {
        return profileName;
    }
    
    public void setProfileName(String profileName) {
        this.profileName = profileName;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public String getProfileUrl() {
        return profileUrl;
    }
    
    public void setProfileUrl(String profileUrl) {
        this.profileUrl = profileUrl;
    }
    
    public Boolean getVerified() {
        return verified;
    }
    
    public void setVerified(Boolean verified) {
        this.verified = verified;
    }
    
    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;
    }
    
    // equals and hashCode
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Following following = (Following) o;
        return Objects.equals(id, following.id) && Objects.equals(followingCode, following.followingCode);
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(id, followingCode);
    }
    
    @Override
    public String toString() {
        return "Following{" +
                "id=" + id +
                ", followingCode='" + followingCode + '\'' +
                ", socialAccountId=" + socialAccountId +
                ", profileName='" + profileName + '\'' +
                ", name='" + name + '\'' +
                ", verified=" + verified +
                '}';
    }
}
