package babble

import (
	"fmt"
	"sort"
	"strings"
	"time"

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

// Babble represents a global chat message in the game
type Babble struct {
	ID     int    `json:"id"`
	Posted int64  `json:"posted"`
	Author string `json:"author" db:"index"`
	Babble string `json:"babble"`
}

// Validate checks if babble has valid values
func (b *Babble) Validate() error {
	if b.Posted <= 0 {
		return fmt.Errorf("babble Posted timestamp must be positive")
	}
	if strings.TrimSpace(b.Author) == "" {
		return fmt.Errorf("babble Author cannot be empty")
	}
	if strings.TrimSpace(b.Babble) == "" {
		return fmt.Errorf("babble message cannot be empty")
	}
	return nil
}

// Global store with singleton pattern
var store *nigiri.BaseStore[Babble]

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

	// Register custom indices
	store.RegisterIndex("byAuthor", nigiri.BuildStringGroupIndex(func(b *Babble) string {
		return strings.ToLower(b.Author)
	}))

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

	store.RebuildIndices()
}

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

// Creates a new Babble with sensible defaults
func New() *Babble {
	return &Babble{
		Posted: time.Now().Unix(),
		Author: "",
		Babble: "",
	}
}

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

func (b *Babble) Delete() error {
	store.Remove(b.ID)
	return nil
}

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

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

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

func ByAuthor(author string) ([]*Babble, error) {
	messages := store.GroupByIndex("byAuthor", strings.ToLower(author))

	// Sort by posted DESC, then ID DESC
	sort.Slice(messages, func(i, j int) bool {
		if messages[i].Posted != messages[j].Posted {
			return messages[i].Posted > messages[j].Posted // DESC
		}
		return messages[i].ID > messages[j].ID // DESC
	})

	return messages, nil
}

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

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

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

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

func RecentByAuthor(author string, limit int) ([]*Babble, error) {
	messages, err := ByAuthor(author)
	if err != nil {
		return nil, err
	}
	if limit > len(messages) {
		limit = len(messages)
	}
	return messages[:limit], nil
}

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

func (b *Babble) SetPostedTime(t time.Time) {
	b.Posted = t.Unix()
}

func (b *Babble) IsRecent() bool {
	return time.Since(b.PostedTime()) < time.Hour
}

func (b *Babble) Age() time.Duration {
	return time.Since(b.PostedTime())
}

func (b *Babble) IsAuthor(username string) bool {
	return strings.EqualFold(b.Author, username)
}

func (b *Babble) Preview(maxLength int) string {
	if len(b.Babble) <= maxLength {
		return b.Babble
	}

	if maxLength < 3 {
		return b.Babble[:maxLength]
	}

	return b.Babble[:maxLength-3] + "..."
}

func (b *Babble) WordCount() int {
	if b.Babble == "" {
		return 0
	}

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

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

	if inWord {
		words++
	}

	return words
}

func (b *Babble) Length() int {
	return len(b.Babble)
}

func (b *Babble) Contains(term string) bool {
	return strings.Contains(strings.ToLower(b.Babble), strings.ToLower(term))
}

func (b *Babble) IsEmpty() bool {
	return strings.TrimSpace(b.Babble) == ""
}

func (b *Babble) IsLongMessage(threshold int) bool {
	return b.Length() > threshold
}

func (b *Babble) GetMentions() []string {
	words := strings.Fields(b.Babble)
	var mentions []string

	for _, word := range words {
		if strings.HasPrefix(word, "@") && len(word) > 1 {
			// Clean up punctuation from the end
			mention := strings.TrimRight(word[1:], ".,!?;:")
			if mention != "" {
				mentions = append(mentions, mention)
			}
		}
	}

	return mentions
}

func (b *Babble) HasMention(username string) bool {
	mentions := b.GetMentions()
	for _, mention := range mentions {
		if strings.EqualFold(mention, username) {
			return true
		}
	}
	return false
}

// 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
}
