package com.storycove.entity;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import com.fasterxml.jackson.annotation.JsonManagedReference;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Entity
@Table(name = "authors")
public class Author {
    
    @Id
    @GeneratedValue(strategy = GenerationType.UUID)
    private UUID id;
    
    @NotBlank(message = "Author name is required")
    @Size(max = 255, message = "Author name must not exceed 255 characters")
    @Column(nullable = false)
    private String name;
    
    @Column(columnDefinition = "TEXT")
    private String notes;
    
    @Column(name = "avatar_image_path")
    private String avatarImagePath;
    
    @Column(name = "author_rating")
    private Integer authorRating;
    
    
    @ElementCollection
    @CollectionTable(name = "author_urls", joinColumns = @JoinColumn(name = "author_id"))
    @Column(name = "url")
    private List<String> urls = new ArrayList<>();
    
    @OneToMany(mappedBy = "author", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JsonManagedReference("author-stories")
    private List<Story> stories = new ArrayList<>();
    
    @CreationTimestamp
    @Column(name = "created_at", nullable = false, updatable = false)
    private LocalDateTime createdAt;
    
    @UpdateTimestamp
    @Column(name = "updated_at", nullable = false)
    private LocalDateTime updatedAt;
    
    public Author() {}
    
    public Author(String name) {
        this.name = name;
    }
    
    public void addStory(Story story) {
        stories.add(story);
        story.setAuthor(this);
    }
    
    public void removeStory(Story story) {
        stories.remove(story);
        story.setAuthor(null);
    }
    
    public void addUrl(String url) {
        if (url != null && !urls.contains(url)) {
            urls.add(url);
        }
    }
    
    public void removeUrl(String url) {
        urls.remove(url);
    }
    
    
    // Getters and Setters
    public UUID getId() {
        return id;
    }
    
    public void setId(UUID id) {
        this.id = id;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public String getNotes() {
        return notes;
    }
    
    public void setNotes(String notes) {
        this.notes = notes;
    }
    
    public String getAvatarImagePath() {
        return avatarImagePath;
    }
    
    public void setAvatarImagePath(String avatarImagePath) {
        this.avatarImagePath = avatarImagePath;
    }
    
    public Integer getAuthorRating() {
        return authorRating;
    }
    
    public void setAuthorRating(Integer authorRating) {
        this.authorRating = authorRating;
    }
    
    
    public List<String> getUrls() {
        return urls;
    }
    
    public void setUrls(List<String> urls) {
        this.urls = urls;
    }
    
    public List<Story> getStories() {
        return stories;
    }
    
    public void setStories(List<Story> stories) {
        this.stories = stories;
    }
    
    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;
    }
    
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Author)) return false;
        Author author = (Author) o;
        return id != null && id.equals(author.id);
    }
    
    @Override
    public int hashCode() {
        return getClass().hashCode();
    }
    
    @Override
    public String toString() {
        return "Author{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", authorRating=" + authorRating +
                '}';
    }
}