package cif

import (
	"time"

	"git.fjla.uk/owlboard/timetable-mgr/dbAccess"
	"git.fjla.uk/owlboard/timetable-mgr/helpers"
	"git.fjla.uk/owlboard/timetable-mgr/log"
	"go.uber.org/zap"
)

func CifCheck(cfg *helpers.Configuration) {
	log.Msg.Info("Checking age of CIF Data")
	utime, err := dbAccess.CheckUpdateTime(dbAccess.TimetableCollection)
	if err != nil {
		log.Msg.Error("Error checking last timetable update", zap.Error(err))
	}

	lastUpdate := time.Unix(utime, 0)
	currentTime := time.Now()
	dataAge := currentTime.Sub(lastUpdate)
	day := 24 * time.Hour

	log.Msg.Debug("CIF Data", zap.Duration("Data Age", dataAge), zap.Duration("Max Age", day))

	if dataAge >= day {
		log.Msg.Warn("Timetable data is more than 24 hours old")
		// Call Update Here
	} else {
		log.Msg.Info("Timetable data is less than 24 hours old")
	}
}
