package mindflow

import (
	"errors"
	"fmt"
	"strings"
	"time"
)

type Category struct {
	ID   int64
	Name string
}

func NewCategory(name string) (*Category, error) {
	if name == "" {
		return nil, errors.New("category's name should not be empty")
	}
	return &Category{Name: name}, nil
}

type Post struct {
	ID       int64
	Category Category
	Date     time.Time
	Title    string
	Body     string
}

func NewPost(category Category, title, body string) (*Post, error) {
	if title == "" || body == "" {
		return nil, errors.New("empty title or body is not allowed")
	}
	return &Post{
		Category: category,
		Date:     time.Now().UTC(),
		Title:    title,
		Body:     body}, nil
}

func (p *Post) PostID() string {
	name := strings.ToLower(p.Category.Name)
	name = strings.ReplaceAll(name, " ", ".")
	return fmt.Sprint(name, "-", p.Date.UTC().Format("20060102-150405"))
}

type Mindflow interface {
	NewPost(post *Post) error
	EditPost(post *Post) error
	DeletePost(id int64) error
	Posts() ([]Post, error)
	NewCategory(category *Category) (int64, error)
	EditCategory(category *Category) error
	DeleteCategory(id int64) error
	Categories() ([]Category, error)
	Close() error
}
