package models

import (
	"time"
)

type Period string

const PeriodInterval = time.Minute * 5

func (p Period) StartTime() time.Time {
	loc, _ := time.LoadLocation("UTC")
	theTime, err := time.ParseInLocation("2006-01-02 15:04:05", string(p), loc)
	if err != nil {
		panic(err)
	}
	return theTime
}

func (p Period) EndTime() time.Time {
	return p.StartTime().Add(PeriodInterval)
}

func FromStartTime(startTime time.Time) Period {
	loc, _ := time.LoadLocation("UTC")
	return Period(startTime.In(loc).Format("2006-01-02 15:04:05"))
}

func FromEndTime(endTime time.Time) Period {
	return FromStartTime(endTime.Add(-1 * PeriodInterval))
}

func (p Period) Next() Period {
	return FromStartTime(p.EndTime())
}

var loc, _ = time.LoadLocation("UTC")

func GetCurrentMonth(d time.Time) time.Time {
	return time.Date(d.Year(), d.Month(), 1, 0, 0, 0, 0, loc)
}

func GetLastMonth(d time.Time) time.Time {
	d = d.AddDate(0, -1, 0)
	return time.Date(d.Year(), d.Month(), 1, 0, 0, 0, 0, loc)
}
