package main

import (
	"math/rand"
	"time"

	"gordenko.dev/dima/diploma/proto"
)

func GenerateCumulativeMeasures(days int) []proto.Measure {
	var (
		measures    []proto.Measure
		minutes     = []int{14, 29, 44, 59}
		hoursPerDay = 24
		totalHours  = days * hoursPerDay
		since       = time.Now().AddDate(0, 0, -days)
		totalValue  float64
	)

	for i := range totalHours {
		hourTime := since.Add(time.Duration(i) * time.Hour)
		for _, m := range minutes {
			measureTime := time.Date(
				hourTime.Year(),
				hourTime.Month(),
				hourTime.Day(),
				hourTime.Hour(),
				m, // minutes
				0, // seconds
				0, // nanoseconds
				time.Local,
			)

			measure := proto.Measure{
				Timestamp: uint32(measureTime.Unix()),
				Value:     totalValue,
			}
			measures = append(measures, measure)

			totalValue += rand.Float64()
		}
	}
	return measures
}

func GenerateInstantMeasures(days int, baseValue float64) []proto.Measure {
	var (
		measures    []proto.Measure
		minutes     = []int{14, 29, 44, 59}
		hoursPerDay = 24
		totalHours  = days * hoursPerDay
		since       = time.Now().AddDate(0, 0, -days)
	)

	for i := range totalHours {
		hourTime := since.Add(time.Duration(i) * time.Hour)
		for _, m := range minutes {
			measureTime := time.Date(
				hourTime.Year(),
				hourTime.Month(),
				hourTime.Day(),
				hourTime.Hour(),
				m, // minutes
				0, // seconds
				0, // nanoseconds
				time.Local,
			)

			// value = +-10% from base value
			fluctuation := baseValue * 0.1
			value := baseValue + (rand.Float64()*2-1)*fluctuation

			measure := proto.Measure{
				Timestamp: uint32(measureTime.Unix()),
				Value:     value,
			}
			measures = append(measures, measure)
		}
	}
	return measures
}
