package vstp

import (
	"fmt"

	"git.fjla.uk/owlboard/go-types/pkg/database"
	"git.fjla.uk/owlboard/timetable-mgr/dbAccess"
	"git.fjla.uk/owlboard/timetable-mgr/log"
)

// Decide, based on the DB Formatted message type, what action needs taking
// then either insert, or delete from the database as required
func processEntryType(entry database.Service) {

	switch entry.TransactionType {
	case "Create":
		createEntry(entry)
	case "Update":
		updateEntry(entry)
	case "Delete":
		deleteEntry(entry)
	default:
		log.Msg.Error("Unknown transaction type: " + entry.TransactionType)
	}
}

func createEntry(entry database.Service) {
	log.Msg.Info("Entry Creation requested for: " + entry.TrainUid + " - " + entry.Headcode + " - " + entry.Operator)
	status := dbAccess.PutOneService(entry)
	if status {
		log.Msg.Info("Database entry created")
	} else {
		log.Msg.Error("Database entry failed, skipped service")
	}
}

func updateEntry(entry database.Service) {
	log.Msg.Info("Entry UPDATE requested for: " + entry.TrainUid + " - " + entry.Headcode + " - " + entry.Operator)
}

func deleteEntry(entry database.Service) {
	log.Msg.Info("Entry DELETE requested for: " + entry.TrainUid + " - " + entry.Headcode)
	var deletionQuery = database.DeleteQuery{
		TrainUid:          entry.TrainUid,
		ScheduleStartDate: entry.ScheduleStartDate,
		StpIndicator:      entry.StpIndicator,
	}
	status := dbAccess.DeleteOneService(deletionQuery)
	if status {
		log.Msg.Info("Database entry deleted")
	} else {
		log.Msg.Error("Database deletion failed, skipped deletion")
		fmt.Printf("%+v\n", deletionQuery)
	}
}
