package corpus

import (
	"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 RunCorpusUpdate(cfg *helpers.Configuration) error {
	resp, err := fetchCorpus(cfg)
	if err != nil {
		log.Msg.Error("Failed to update Corpus data", zap.Error(err))
		return err
	}

	datastring, err := extractCorpusResponse(resp)
	if err != nil {
		log.Msg.Error("Error extracting Corpus data", zap.Error(err))
		return err
	}

	unsortedCorpusData, err := parseCorpusData(datastring)
	if err != nil {
		log.Msg.Error("Error parsing Corpus data", zap.Error(err))
		return err
	}

	corpusData := pruneCorpusEntries(unsortedCorpusData)
	stationData := createStationEntries(corpusData)

	if err := dbAccess.DropCollection(dbAccess.CorpusCollection); err != nil {
		log.Msg.Warn("CORPUS data may be incomplete")
		log.Msg.Error("Error dropping CORPUS Data", zap.Error(err))
	}
	if err := dbAccess.DropCollection(dbAccess.StationsCollection); err != nil {
		log.Msg.Warn("Stations data may be incomplete")
		log.Msg.Error("Error dropping stations Data", zap.Error(err))
	}

	if err := dbAccess.PutManyCorpus(corpusData); err != nil {
		log.Msg.Warn("CORPUS data may be incomplete")
		log.Msg.Error("Error inserting CORPUS Data", zap.Error(err))
	}
	if err := dbAccess.PutManyStations(stationData); err != nil {
		log.Msg.Warn("Stations data may be incomplete")
		log.Msg.Error("Error inserting stations data", zap.Error(err))
	}

	return nil
}
