package linguistics

import (
	"strings"
)

// POSTagger assigns part-of-speech tags to tokens
type POSTagger struct{}

// NewPOSTagger creates a new POSTagger
func NewPOSTagger() *POSTagger {
	return &POSTagger{}
}

// Tag assigns part-of-speech tags to tokens and returns them
func (t *POSTagger) Tag(tokens []Token) ([]string, error) {
	// This is a simplified implementation
	// In a real-world scenario, you would use a library like github.com/jdkato/prose
	// or call an external API for POS tagging

	tags := make([]string, len(tokens))

	for i, token := range tokens {
		// Simplified POS tagging based on word endings and common words
		word := strings.ToLower(token.Text)

		// Check for common nouns
		if isCommonNoun(word) {
			tags[i] = "NOUN"
			continue
		}

		// Check for verbs
		if isVerb(word) {
			tags[i] = "VERB"
			continue
		}

		// Check for adjectives
		if isAdjective(word) {
			tags[i] = "ADJ"
			continue
		}

		// Check for adverbs
		if isAdverb(word) {
			tags[i] = "ADV"
			continue
		}

		// Check for prepositions
		if isPreposition(word) {
			tags[i] = "PREP"
			continue
		}

		// Check for determiners
		if isDeterminer(word) {
			tags[i] = "DET"
			continue
		}

		// Check for pronouns
		if isPronoun(word) {
			tags[i] = "PRON"
			continue
		}

		// Default to noun
		tags[i] = "NOUN"
	}

	return tags, nil
}

// Helper functions for POS tagging

func isCommonNoun(word string) bool {
	commonNouns := []string{"time", "person", "year", "way", "day", "thing", "man", "world", "life", "hand", "part", "child", "eye", "woman", "place", "work", "week", "case", "point", "government", "company", "number", "group", "problem", "fact"}
	for _, noun := range commonNouns {
		if word == noun {
			return true
		}
	}
	return false
}

func isVerb(word string) bool {
	// Check for common verbs
	commonVerbs := []string{"is", "are", "was", "were", "be", "have", "has", "had", "do", "does", "did", "will", "would", "can", "could", "shall", "should", "may", "might", "must", "go", "come", "get", "take", "make", "see", "look", "find", "give", "tell", "think", "say", "know"}
	for _, verb := range commonVerbs {
		if word == verb {
			return true
		}
	}

	// Check for verb endings
	verbEndings := []string{"ed", "ing", "es", "s"}
	for _, ending := range verbEndings {
		if strings.HasSuffix(word, ending) && len(word) > len(ending)+1 {
			return true
		}
	}

	return false
}

func isAdjective(word string) bool {
	// Check for common adjectives
	commonAdjectives := []string{"good", "new", "first", "last", "long", "great", "little", "own", "other", "old", "right", "big", "high", "different", "small", "large", "next", "early", "young", "important", "few", "public", "bad", "same", "able"}
	for _, adj := range commonAdjectives {
		if word == adj {
			return true
		}
	}

	// Check for adjective endings
	adjEndings := []string{"al", "ful", "ive", "ous", "ish", "able", "less"}
	for _, ending := range adjEndings {
		if strings.HasSuffix(word, ending) && len(word) > len(ending)+1 {
			return true
		}
	}

	return false
}

func isAdverb(word string) bool {
	// Check for common adverbs
	commonAdverbs := []string{"up", "so", "out", "just", "now", "how", "then", "more", "also", "here", "well", "only", "very", "even", "back", "there", "down", "still", "in", "as", "too", "when", "never", "really", "most"}
	for _, adv := range commonAdverbs {
		if word == adv {
			return true
		}
	}

	// Check for adverb endings
	if strings.HasSuffix(word, "ly") && len(word) > 3 {
		return true
	}

	return false
}

func isPreposition(word string) bool {
	prepositions := []string{"in", "on", "at", "by", "for", "with", "about", "against", "between", "into", "through", "during", "before", "after", "above", "below", "to", "from", "up", "down", "over", "under", "of"}
	for _, prep := range prepositions {
		if word == prep {
			return true
		}
	}
	return false
}

func isDeterminer(word string) bool {
	determiners := []string{"the", "a", "an", "this", "that", "these", "those", "my", "your", "his", "her", "its", "our", "their", "some", "any", "all", "both", "each", "every", "no", "many", "much", "little", "few", "other", "another", "such", "what", "which", "whose"}
	for _, det := range determiners {
		if word == det {
			return true
		}
	}
	return false
}

func isPronoun(word string) bool {
	pronouns := []string{"i", "you", "he", "she", "it", "we", "they", "me", "him", "her", "us", "them", "who", "whom", "whose", "which", "what", "whoever", "whatever", "whichever", "myself", "yourself", "himself", "herself", "itself", "ourselves", "themselves"}
	for _, pron := range pronouns {
		if word == pron {
			return true
		}
	}
	return false
}
