package ana

import (
	"maps"
	"math"
	"regexp"
	"slices"
	"time"

	"gogs.inhome.blapointe.com/ana-ledger/src/ledger"
)

const (
	month = time.Hour * 24 * 365 / 12
)

type Predictor func(ledger.Balances, time.Duration) ledger.Balances

func NewInterestPredictor(namePattern string, currencyPattern string, apy float64) Predictor {
	nameMatcher := regexp.MustCompile(namePattern)
	currencyMatcher := regexp.MustCompile(currencyPattern)
	return func(given ledger.Balances, delta time.Duration) ledger.Balances {
		result := maps.Clone(given)
		for k, v := range result {
			result[k] = maps.Clone(v)
		}

		monthsPassed := float64(delta) / float64(month)
		scalar := math.Pow(1.0+apy/12.0, monthsPassed)
		for name := range result {
			if !nameMatcher.MatchString(name) {
				continue
			}
			for currency := range result[name] {
				if !currencyMatcher.MatchString(string(currency)) {
					continue
				}
				result[name][currency] *= scalar
			}
		}
		return result
	}
}

func NewAutoContributionPredictor(reg ledger.Register) Predictor {
	monthlyRate := getMonthlyAutoContributionRates(reg)
	return NewContributionPredictor(monthlyRate)
}

func NewContributionPredictor(monthlyRate ledger.Balances) Predictor {
	return func(given ledger.Balances, delta time.Duration) ledger.Balances {
		months := float64(delta) / float64(month)
		result := make(ledger.Balances)
		for k, v := range given {
			result[k] = maps.Clone(v)
		}
		for name := range monthlyRate {
			if _, ok := result[name]; !ok {
				result[name] = make(ledger.Balance)
			}
			for currency := range monthlyRate[name] {
				result[name][currency] += monthlyRate[name][currency] * months
			}
		}
		return result
	}
}

func getMonthlyAutoContributionRates(reg ledger.Register) map[string]ledger.Balance {
	window := 6 * month
	window = 12 * month
	contributions := getRecentContributions(reg, window)
	result := make(map[string]ledger.Balance)
	for name := range contributions {
		result[name] = getMonthlyAutoContributionRate(contributions[name], window)
	}
	return result
}

func getMonthlyAutoContributionRate(contributions []ledger.Balance, window time.Duration) ledger.Balance {
	currencies := map[ledger.Currency]int{}
	for _, balance := range contributions {
		for currency := range balance {
			currencies[currency] = 1
		}
	}
	result := make(ledger.Balance)
	for currency := range currencies {
		result[currency] = getMonthlyAutoContributionRateForCurrency(contributions, window, currency)
	}
	return result
}

func getMonthlyAutoContributionRateForCurrency(contributions []ledger.Balance, window time.Duration, currency ledger.Currency) float64 {
	values := []float64{}
	for i := range contributions {
		if v, ok := contributions[i][currency]; ok {
			values = append(values, v)
		}
	}
	slices.Sort(values)
	if len(values) < 3 {
		return 0
	}

	start := int(len(values) / 4)
	end := min(1+max(0, len(values)-len(values)/4), len(values))
	subvalues := values[start:end]
	sum := 0.0
	for _, v := range subvalues {
		sum += v
	}
	standard := sum / float64(end-start)
	standard = subvalues[len(subvalues)/2]
	totalIfAllWereStandard := standard * float64(len(contributions))
	result := totalIfAllWereStandard / float64(window) * float64(month)
	//log.Printf("%s: %v contributions of about %.2f over %v can TLDR as %.2f per month from %v", currency, len(contributions), standard, window, result, values[start:end])
	return result
}

func getRecentContributions(reg ledger.Register, window time.Duration) map[string][]ledger.Balance {
	return getContributions(reg.Between(time.Now().Add(-1*window), time.Now()))
}

func getContributions(reg ledger.Register) map[string][]ledger.Balance {
	contributions := make(map[string][]ledger.Balance)
	for _, name := range reg.Names() {
		contributions[name] = getContributionsFor(reg, name)
	}
	return contributions
}

func getContributionsFor(reg ledger.Register, name string) []ledger.Balance {
	result := make([]ledger.Balance, 0)
	var last ledger.Balance
	for _, date := range reg.Dates() {
		if _, ok := reg[date][name]; !ok {
			continue
		}

		if last == nil {
			last = reg[date][name]
			continue
		}

		result = append(result, make(ledger.Balance))
		this := result[len(result)-1]

		for k, v := range last {
			this[k] = -1 * v
		}
		for k, v := range reg[date][name] {
			this[k] += v
		}

		last = reg[date][name]
	}
	return result
}
