package subspecialist

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

	"gorm.io/gorm"

	sync "simrs-vx/internal/infra/sync-cfg"

	e "simrs-vx/internal/domain/main-entities/subspecialist"
	esync "simrs-vx/internal/domain/sync-entities/log"
)

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) *middlewareRunner {
	return &middlewareRunner{
		Event:  event,
		SyncOn: sync.O.Enable,
	}
}

// ExecuteCreateMiddleware executes create middleware
func (me *middlewareRunner) RunCreateMiddleware(middlewares []createMw, input *e.CreateDto) error {
	if !me.SyncOn {
		return nil
	}

	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 {
	if !me.SyncOn {
		return nil
	}

	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.Subspecialist) error {
	if !me.SyncOn {
		return nil
	}

	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.Subspecialist) error {
	if !me.SyncOn {
		return nil
	}

	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.UpdateDto) error {
	if !me.SyncOn {
		return nil
	}

	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 {
	if !me.SyncOn {
		return nil
	}

	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
}
