package com.codenetworkz.blog.models;

import com.codenetworkz.blog.config.SlugGenerator;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
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;


@Getter
@Setter
@Entity
@AllArgsConstructor
public class Category extends BaseModel {

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

    private String slug;

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

    public Category() {
        super();
    }


    @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;
        }
    }

}