package cif

import (
	"errors"

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

// Runs a full update of the CIF Data, discarding any existing data and returns a new metadata struct
func runFullUpdate(cfg *helpers.Configuration) (*dbAccess.CifMetadata, error) {
	log.Msg.Warn("All existing timetable data will be deleted")
	url, err := getUpdateUrl("full")
	if err != nil {
		log.Msg.Error("Unable to get update URL", zap.Error(err))
		return nil, err
	}

	fullCifData, err := nrod.NrodDownload(url, cfg)
	if err != nil {
		log.Msg.Error("Unable to get CIF Data", zap.Error(err))
		return nil, err
	}

	log.Msg.Debug("CIF Data Downloaded", zap.ByteString("CIF Data", fullCifData))

	// I now need to define a processing function and ensure a valid type exists, then I can pass that type to a CIF Put Full function
	// which will handle placing the data into the database

	return nil, nil
}

// Run the specified update type.  Update type must be one of 'daily' or 'full'
// In the case of daily update, things get complicated as it needs to handle cases where up to five days have been missed.
func runUpdate(updateType string, metadata *dbAccess.CifMetadata) (*dbAccess.CifMetadata, error) {
	url, err := getUpdateUrl(updateType)
	if err != nil {
		log.Msg.Error("Unable to get the update URL", zap.Error(err))
		return nil, err
	}
	log.Msg.Debug("", zap.String("URL", url))
	return nil, errors.New("function is not yet defined")

	// Use the values in metadata to determine which day to attempt to update.
	// First check if the last update was today, if so, I can return nil, nil - No update required
	////// If the update was on the previous day, download todays data and check the sequence number and timestamp indicate that todays data is the next file that I need.
	////// If the sequence number and timestamp indicate I have missed a day, download that days data first, then todays.

	// Write a parsing function that can handle VSTP as well as SCHEDULE data
	// Handle database management
}
