package referral

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

	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 = "referral"

func ReadDetail(input e.ReadDetailDto) (*e.Response, error) {
	var data e.Response
	var err error

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

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

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

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

		return nil
	})

	if err != nil {
		return nil, err
	}

	return &data, nil
}
