package news

import (
	"fmt"
	"strings"
	"time"

	"dk/internal/database"
)

// News represents a news post in the game
type News struct {
	ID      int
	Author  int
	Posted  int64
	Content string
}

// New creates a new News with sensible defaults
func New() *News {
	return &News{
		Author:  0,
		Posted:  time.Now().Unix(),
		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) Delete() error {
	return database.Exec("DELETE FROM news WHERE id = %d", n.ID)
}

func (n *News) Insert() error {
	id, err := database.Insert("news", n, "ID")
	if err != nil {
		return err
	}
	n.ID = int(id)
	return nil
}

// Query functions
func Find(id int) (*News, error) {
	var news News
	err := database.Get(&news, "SELECT * FROM news WHERE id = %d", id)
	if err != nil {
		return nil, fmt.Errorf("news with ID %d not found", id)
	}
	return &news, nil
}

func All() ([]*News, error) {
	var news []*News
	err := database.Select(&news, "SELECT * FROM news ORDER BY posted DESC, id DESC")
	return news, err
}

func ByAuthor(authorID int) ([]*News, error) {
	var news []*News
	err := database.Select(&news, "SELECT * FROM news WHERE author = %d ORDER BY posted DESC, id DESC", authorID)
	return news, err
}

func Recent(limit int) ([]*News, error) {
	var news []*News
	err := database.Select(&news, "SELECT * FROM news ORDER BY posted DESC, id DESC LIMIT %d", limit)
	return news, err
}

func Since(since int64) ([]*News, error) {
	var news []*News
	err := database.Select(&news, "SELECT * FROM news WHERE posted >= %d ORDER BY posted DESC, id DESC", since)
	return news, err
}

func Between(start, end int64) ([]*News, error) {
	var news []*News
	err := database.Select(&news, "SELECT * FROM news WHERE posted >= %d AND posted <= %d ORDER BY posted DESC, id DESC", start, end)
	return news, err
}

func Search(term string) ([]*News, error) {
	var news []*News
	searchTerm := "%" + term + "%"
	err := database.Select(&news, "SELECT * FROM news WHERE content LIKE %s ORDER BY posted DESC, id DESC", searchTerm)
	return news, err
}

// 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) == ""
}
