package online.wesal.wesal.entity;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.time.LocalDateTime;
import java.util.List;
import java.util.ArrayList;

@Entity
@Table(name = "posts")
public class Post {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @Column(nullable = false)
    private Long creatorId;
    
    @Column(nullable = false, length = 2000)
    @NotBlank
    private String body;
    
    @Column(nullable = false)
    private int likes = 0;
    
    @Column(nullable = false)
    private int comments = 0;
    
    @Column(nullable = false)
    private LocalDateTime creationDate = LocalDateTime.now();
    
    @Column(columnDefinition = "TEXT")
    private String images = "[]";
    
    public Post() {}
    
    public Post(Long creatorId, String body) {
        this.creatorId = creatorId;
        this.body = body;
    }
    
    public Post(Long creatorId, String body, List<String> images) {
        this.creatorId = creatorId;
        this.body = body;
        this.setImages(images);
    }
    
    public Long getId() {
        return id;
    }
    
    public void setId(Long id) {
        this.id = id;
    }
    
    public Long getCreatorId() {
        return creatorId;
    }
    
    public void setCreatorId(Long creatorId) {
        this.creatorId = creatorId;
    }
    
    public String getBody() {
        return body;
    }
    
    public void setBody(String body) {
        this.body = body;
    }
    
    public int getLikes() {
        return likes;
    }
    
    public void setLikes(int likes) {
        this.likes = likes;
    }
    
    public int getComments() {
        return comments;
    }
    
    public void setComments(int comments) {
        this.comments = comments;
    }
    
    public LocalDateTime getCreationDate() {
        return creationDate;
    }
    
    public void setCreationDate(LocalDateTime creationDate) {
        this.creationDate = creationDate;
    }
    
    public List<String> getImages() {
        if (images == null || images.isEmpty()) {
            return new ArrayList<>();
        }
        try {
            ObjectMapper mapper = new ObjectMapper();
            return mapper.readValue(images, new TypeReference<List<String>>() {});
        } catch (Exception e) {
            return new ArrayList<>();
        }
    }
    
    public void setImages(List<String> images) {
        if (images == null || images.isEmpty()) {
            this.images = "[]";
            return;
        }
        
        // Limit to 10 images
        List<String> limitedImages = images.size() > 10 ? images.subList(0, 10) : images;
        
        try {
            ObjectMapper mapper = new ObjectMapper();
            this.images = mapper.writeValueAsString(limitedImages);
        } catch (Exception e) {
            this.images = "[]";
        }
    }
}