package dbAccess

import (
	"context"

	"git.fjla.uk/owlboard/go-types/pkg/database"
)

func PutManyCorpus(corpusData []database.CorpusEntry) error {
	collection := MongoClient.Database(databaseName).Collection(CorpusCollection)

	documents := convertCorpusToInterfaceSlice(corpusData)

	_, err := collection.InsertMany(context.Background(), documents)
	if err != nil {
		return err
	}

	SetUpdateTime(CorpusCollection)
	return nil
}

func PutManyStations(stationsData []database.StationEntry) error {
	collection := MongoClient.Database(databaseName).Collection(StationsCollection)

	documents := convertStationsToInterfaceSlice(stationsData)

	_, err := collection.InsertMany(context.Background(), documents)
	if err != nil {
		return err
	}

	SetUpdateTime(StationsCollection)
	return nil
}

func convertCorpusToInterfaceSlice(corpusData []database.CorpusEntry) []interface{} {
	interfaceSlice := make([]interface{}, len(corpusData))
	for i, doc := range corpusData {
		interfaceSlice[i] = doc
	}
	return interfaceSlice
}

func convertStationsToInterfaceSlice(stationsData []database.StationEntry) []interface{} {
	interfaceSlice := make([]interface{}, len(stationsData))
	for i, doc := range stationsData {
		interfaceSlice[i] = doc
	}
	return interfaceSlice
}
