package monitoring

import (
	e "simrs-vx/internal/domain/bpjs-entities/monitoring"

	dg "github.com/karincake/apem/db-gorm-pg"

	pl "simrs-vx/pkg/logger"
	pu "simrs-vx/pkg/use-case-helper"

	"gorm.io/gorm"
)

const source = "monitoring"

func ReadList(input e.ReadListDto) (*e.Response, error) {
	var data e.Response
	var err error

	event := pl.Event{
		Feature: "ReadList",
		Source:  source,
	}

	// Start log
	pl.SetLogInfo(&event, input, "started", "readList")

	err = dg.I.Transaction(func(tx *gorm.DB) error {
		mwRunner := newMiddlewareRunner(&event, tx)
		mwRunner.setMwType(pu.MWTPre)
		// Run pre-middleware
		if err := mwRunner.RunReadListMiddleware(readListPreMw, &input, &data); err != nil {
			return err
		}

		mwRunner.setMwType(pu.MWTPost)
		// Run post-middleware
		if err := mwRunner.RunReadListMiddleware(readListPostMw, &input, &data); err != nil {
			return err
		}

		return nil
	})

	if err != nil {
		return nil, err
	}

	return &data, nil
}
