package soapi

import (
	e "simrs-vx/internal/domain/main-entities/soapi"
	esync "simrs-vx/internal/domain/sync-entities/log"
	sync "simrs-vx/internal/infra/sync-consumer-cfg"
	pl "simrs-vx/pkg/logger"
	pu "simrs-vx/pkg/use-case-helper"

	"gorm.io/gorm"
)

type middlewareRunner struct {
	Event  *pl.Event
	Tx     *gorm.DB
	MwType pu.MWType
	SyncOn bool
}

// NewMiddlewareExecutor creates a new middleware executor
func newMiddlewareRunner(event *pl.Event, syncFromOldSource bool) *middlewareRunner {
	activateSync := sync.O.Enable
	if syncFromOldSource {
		activateSync = false
	}

	return &middlewareRunner{
		Event:  event,
		SyncOn: activateSync,
	}
}

// ExecuteCreateMiddleware executes create middleware
func (me *middlewareRunner) RunCreateMiddleware(middlewares []createMw, input *e.Soapi) error {
	for _, middleware := range middlewares {
		logData := pu.GetLogData(input, nil)

		pl.SetLogInfo(me.Event, logData, "started", middleware.Name)

		if err := middleware.Func(input); err != nil {
			return pu.HandleMiddlewareError(me.Event, string(me.MwType), middleware.Name, logData, err)
		}

		pl.SetLogInfo(me.Event, nil, "complete")
	}
	return nil
}

func (me *middlewareRunner) RunCreateLogMiddleware(middlewares []createLogMw, input *esync.SimxLogDto) error {
	for _, middleware := range middlewares {
		logData := pu.GetLogData(input, nil)

		pl.SetLogInfo(me.Event, logData, "started", middleware.Name)

		if err := middleware.Func(input); err != nil {
			return pu.HandleMiddlewareError(me.Event, string(me.MwType), middleware.Name, logData, err)
		}

		pl.SetLogInfo(me.Event, nil, "complete")
	}
	return nil
}

func (me *middlewareRunner) RunReadListMiddleware(middlewares []readListMw, input *e.ReadListDto, data *e.Soapi) error {
	for _, middleware := range middlewares {
		logData := pu.GetLogData(input, data)

		pl.SetLogInfo(me.Event, logData, "started", middleware.Name)

		if err := middleware.Func(input, data, me.Tx); err != nil {
			return pu.HandleMiddlewareError(me.Event, string(me.MwType), middleware.Name, logData, err)
		}

		pl.SetLogInfo(me.Event, nil, "complete")
	}
	return nil
}

func (me *middlewareRunner) RunReadDetailMiddleware(middlewares []readDetailMw, input *e.ReadDetailDto, data *e.Soapi) error {
	for _, middleware := range middlewares {
		logData := pu.GetLogData(input, data)

		pl.SetLogInfo(me.Event, logData, "started", middleware.Name)

		if err := middleware.Func(input, data, me.Tx); err != nil {
			return pu.HandleMiddlewareError(me.Event, string(me.MwType), middleware.Name, logData, err)
		}

		pl.SetLogInfo(me.Event, nil, "complete")
	}
	return nil
}

func (me *middlewareRunner) RunUpdateMiddleware(middlewares []updateMw, input *e.Soapi) error {
	for _, middleware := range middlewares {
		logData := pu.GetLogData(input, nil)

		pl.SetLogInfo(me.Event, logData, "started", middleware.Name)

		if err := middleware.Func(input); err != nil {
			return pu.HandleMiddlewareError(me.Event, string(me.MwType), middleware.Name, logData, err)
		}

		pl.SetLogInfo(me.Event, nil, "complete")
	}
	return nil
}

func (me *middlewareRunner) RunDeleteMiddleware(middlewares []deleteMw, input *e.DeleteDto) error {
	for _, middleware := range middlewares {
		logData := pu.GetLogData(input, nil)

		pl.SetLogInfo(me.Event, logData, "started", middleware.Name)

		if err := middleware.Func(input); err != nil {
			return pu.HandleMiddlewareError(me.Event, string(me.MwType), middleware.Name, logData, err)
		}

		pl.SetLogInfo(me.Event, nil, "complete")
	}
	return nil
}

func (me *middlewareRunner) setMwType(mwType pu.MWType) {
	me.MwType = mwType
}

func (me *middlewareRunner) ExecuteIfSyncOn(fn func() error) error {
	if !me.SyncOn {
		return nil
	}
	return fn()
}
