package com.codenetworkz.blog.models;


import com.codenetworkz.blog.config.SlugGenerator;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.ManyToMany;

import jakarta.persistence.PrePersist;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

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


@Getter
@Setter
@Entity
@NoArgsConstructor
@AllArgsConstructor
public class Tag extends BaseModel {


    //@Size(min = 3, max = 50,message ="Tag Name should be more then 3 and less then 50 character required." )
    private String name;

    private String slug;

   // @Size(min = 3, max = 250,message ="Description should be more then 3 and less then 50 character required." )
    private String description;


    @ManyToMany(mappedBy = "tags")
    private List<Post> posts;

    @PrePersist
    protected void onCreate() {
        if (super.getCreateAt() == null) {
            setCreateAt(LocalDateTime.now());
        }
        if (super.getIsDelete() == null) {
            setIsDelete(Status.NO);
        }

        if (super.getIsActive() == null) {
            setIsActive(Status.YES);
        }

        String newSlug = SlugGenerator.generateSlug(name);
        if (name != null && !slug.equals(newSlug)) {
            slug = newSlug;
        }

    }

}