
// secretInboxEpoch provides a function to calculate start and end times of secret inbox epochs.
// A secret inbox epoch is the time period during which a secret inbox is active.

package secretInboxEpoch

// When a secret inbox is active, the sender should send their messages to the inbox.
// Once the epoch has passed, the recipient will check the inbox to a sufficient degree and then stop checking it.

// Here is an example of how the epoch time works
//	 Example epoch origin time = 100
//	 Example Epoch Duration = 10
//	Example epochs:
//	 1st epoch start time: 100
//	 1st epoch end time: 110
//	 2nd epoch start time: 111
//	 2nd epoch end time: 121
//	 3rd epoch start time: 122
//	 3rd epoch end time: 132
//	 ...

import "seekia/internal/helpers"
import "seekia/internal/parameters/getParameters"

import "errors"

// This is a unix time value from which the secret inbox epochs are calculated.
// This value should never change (once the Seekia network goes live)
const secretInboxEpochOriginTime int64 = 1670000000


// This function returns the epoch start and end times at the time of the provided messageCreationTime
//Outputs:
//	-bool: Parameters exist
//	-int64: Current epoch start time
//	-int64: Current epoch end time
//	-int64: Next epoch start time
//	-int64: Next epoch start time
//	-error
func GetSecretInboxEpochStartAndEndTimes(networkType byte, messageCreationTime int64)(bool, int64, int64, int64, int64, error){

	isValid := helpers.VerifyNetworkType(networkType)
	if (isValid == false){
		networkTypeString := helpers.ConvertByteToString(networkType)
		return false, 0, 0, 0, 0, errors.New("GetSecretInboxEpochStartAndEndTimes called with invalid networkType: " + networkTypeString)
	}

	if (messageCreationTime < secretInboxEpochOriginTime){
		return false, 0, 0, 0, 0, errors.New("GetSecretInboxEpochStartAndEndTimes called with invalid messageCreationTime.")
	}

	parametersExist, epochDuration, err := getParameters.GetSecretInboxEpochDuration(networkType, messageCreationTime)
	if (err != nil) { return false, 0, 0, 0, 0, err }
	if (parametersExist == false){
		return false, 0, 0, 0, 0, nil
	}

	remainder := (messageCreationTime - secretInboxEpochOriginTime) % (epochDuration + 1)

	currentEpochStartTime := messageCreationTime - remainder
	currentEpochEndTime := currentEpochStartTime + epochDuration

	nextEpochStartTime := currentEpochEndTime + 1
	nextEpochEndTime := nextEpochStartTime + epochDuration

	return true, currentEpochStartTime, currentEpochEndTime, nextEpochStartTime, nextEpochEndTime, nil
}



