package grpc

import (
	"context"
	"google.golang.org/grpc"
	pb "htwkalender/common/genproto/modules"
	"htwkalender/ical/model"
	"log/slog"
	"time"
)

func GetEvents(modules []model.FeedModule, conn *grpc.ClientConn) (model.Events, error) {
	c := pb.NewModuleServiceClient(conn)
	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()

	// List of uuids
	uuids := make([]string, 0)
	for _, module := range modules {
		uuids = append(uuids, module.UUID)
	}

	r, err := c.GetEventsForModules(ctx, &pb.GetModulesRequest{Uuids: uuids})
	if err != nil {
		slog.Error("could not get modules: %v", "error", err)
	}

	events := make(model.Events, 0)
	for _, event := range r.GetEvents() {
		events = append(events, protoToEvent(event))
	}

	return events, nil
}
