package search

import (
	"strings"

	"github.com/yeho/klp/internal/database"
)

type Result struct {
	Entry *database.Entry
}

type Options struct {
	MaxResults int
}

func DefaultOptions() Options {
	return Options{
		MaxResults: 20,
	}
}

type Searcher struct {
	db *database.Database
}

func NewSearcher(db *database.Database) *Searcher {
	return &Searcher{db: db}
}

func (s *Searcher) Search(query string, opts Options) []Result {
	queryLower := strings.ToLower(query)
	results := []Result{}

	entries := s.db.List(0) // Get all entries
	for _, entry := range entries {
		if strings.Contains(strings.ToLower(entry.Value), queryLower) {
			results = append(results, Result{Entry: entry})
			if opts.MaxResults > 0 && len(results) >= opts.MaxResults {
				break
			}
		}
	}

	return results
}
