package news

import (
	"fmt"
	"strings"
	"time"

	nigiri "git.sharkk.net/Sharkk/Nigiri"
)

// News represents a news post in the game
type News struct {
	ID      int    `json:"id"`
	Author  int    `json:"author" db:"index"`
	Posted  int64  `json:"posted"`
	Content string `json:"content" db:"required"`
}

// Global store
var store *nigiri.BaseStore[News]

// Init sets up the Nigiri store and indices
func Init(collection *nigiri.Collection) {
	store = nigiri.NewBaseStore[News]()

	// Register custom indices
	store.RegisterIndex("byAuthor", nigiri.BuildIntGroupIndex(func(n *News) int {
		return n.Author
	}))

	store.RegisterIndex("allByPosted", nigiri.BuildSortedListIndex(func(a, b *News) bool {
		if a.Posted != b.Posted {
			return a.Posted > b.Posted // DESC
		}
		return a.ID > b.ID // DESC
	}))

	store.RebuildIndices()
}

// GetStore returns the news store
func GetStore() *nigiri.BaseStore[News] {
	if store == nil {
		panic("news store not initialized - call Init first")
	}
	return store
}

// Creates a new News with sensible defaults
func New() *News {
	return &News{
		Author:  0,                 // No author by default
		Posted:  time.Now().Unix(), // Current time
		Content: "",                // Empty content
	}
}

// Validate checks if news has valid values
func (n *News) Validate() error {
	if n.Posted < 0 {
		return fmt.Errorf("news Posted timestamp cannot be negative")
	}
	if strings.TrimSpace(n.Content) == "" {
		return fmt.Errorf("news Content cannot be empty")
	}
	return nil
}

// CRUD operations
func (n *News) Save() error {
	if n.ID == 0 {
		id, err := store.Create(n)
		if err != nil {
			return err
		}
		n.ID = id
		return nil
	}
	return store.Update(n.ID, n)
}

func (n *News) Delete() error {
	store.Remove(n.ID)
	return nil
}

// Insert with ID assignment
func (n *News) Insert() error {
	id, err := store.Create(n)
	if err != nil {
		return err
	}
	n.ID = id
	return nil
}

// Query functions
func Find(id int) (*News, error) {
	news, exists := store.Find(id)
	if !exists {
		return nil, fmt.Errorf("news with ID %d not found", id)
	}
	return news, nil
}

func All() ([]*News, error) {
	return store.AllSorted("allByPosted"), nil
}

func ByAuthor(authorID int) ([]*News, error) {
	return store.GroupByIndex("byAuthor", authorID), nil
}

func Recent(limit int) ([]*News, error) {
	all := store.AllSorted("allByPosted")
	if limit > len(all) {
		limit = len(all)
	}
	return all[:limit], nil
}

func Since(since int64) ([]*News, error) {
	return store.FilterByIndex("allByPosted", func(n *News) bool {
		return n.Posted >= since
	}), nil
}

func Between(start, end int64) ([]*News, error) {
	return store.FilterByIndex("allByPosted", func(n *News) bool {
		return n.Posted >= start && n.Posted <= end
	}), nil
}

func Search(term string) ([]*News, error) {
	lowerTerm := strings.ToLower(term)
	return store.FilterByIndex("allByPosted", func(n *News) bool {
		return strings.Contains(strings.ToLower(n.Content), lowerTerm)
	}), nil
}

// Helper methods
func (n *News) PostedTime() time.Time {
	return time.Unix(n.Posted, 0)
}

func (n *News) SetPostedTime(t time.Time) {
	n.Posted = t.Unix()
}

func (n *News) IsRecent() bool {
	return time.Since(n.PostedTime()) < 24*time.Hour
}

func (n *News) Age() time.Duration {
	return time.Since(n.PostedTime())
}

func (n *News) ReadableTime() string {
	return n.PostedTime().Format("Jan 2, 2006 3:04 PM")
}

func (n *News) IsAuthor(userID int) bool {
	return n.Author == userID
}

func (n *News) Preview(maxLength int) string {
	if len(n.Content) <= maxLength {
		return n.Content
	}

	if maxLength < 3 {
		return n.Content[:maxLength]
	}

	return n.Content[:maxLength-3] + "..."
}

func (n *News) WordCount() int {
	if n.Content == "" {
		return 0
	}

	// Simple word count by splitting on spaces
	words := 0
	inWord := false

	for _, char := range n.Content {
		if char == ' ' || char == '\t' || char == '\n' || char == '\r' {
			if inWord {
				words++
				inWord = false
			}
		} else {
			inWord = true
		}
	}

	if inWord {
		words++
	}

	return words
}

func (n *News) Length() int {
	return len(n.Content)
}

func (n *News) Contains(term string) bool {
	return strings.Contains(strings.ToLower(n.Content), strings.ToLower(term))
}

func (n *News) IsEmpty() bool {
	return strings.TrimSpace(n.Content) == ""
}

// Legacy compatibility functions (will be removed later)
func LoadData(dataPath string) error {
	// No longer needed - Nigiri handles this
	return nil
}

func SaveData(dataPath string) error {
	// No longer needed - Nigiri handles this
	return nil
}
