package entities

import (
	"errors"
	"time"

	"github.com/google/uuid"
)

const PostTable = "post"

type Post struct {
	Id          uuid.UUID `db:"id"`
	UserId      uuid.UUID `db:"userid"`
	Title       string    `db:"title"`
	Description string    `db:"description"`
	Content     string    `db:"content"`
	CreatedAt   time.Time `db:"createdat"`
	UpdatedAt   time.Time `db:"updatedat"`
}

func CreatePost(userId uuid.UUID, title string, description string, content string) (post Post, err error) {
	post = Post{
		Id:          uuid.New(),
		UserId:      userId,
		Title:       title,
		Description: description,
		Content:     content,
		CreatedAt:   time.Now(),
		UpdatedAt:   time.Now(),
	}

	err = post.Validate()

	return
}

func (p *Post) Validate() error {
	if err := uuid.Validate(p.Id.String()); err != nil {
		return errors.New("Empty post.id")
	}

	if err := uuid.Validate(p.UserId.String()); err != nil {
		return errors.New("Empty post.userId")
	}

	if p.Title == "" {
		return errors.New("Empty post.title")
	}

	if p.Description == "" {
		return errors.New("Empty post.description")
	}

	if p.Content == "" {
		return errors.New("Empty post.content path")
	}

	if p.CreatedAt.IsZero() {
		return errors.New("Empty post.createdAt")
	}

	if p.UpdatedAt.IsZero() {
		return errors.New("Empty post.updatedAt")
	}

	return nil
}
