package doctor

import (
	e "simrs-vx/internal/domain/main-entities/doctor"
	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
}

// NewMiddlewareExecutor creates a new middleware executor
func newMiddlewareRunner(event *pl.Event, tx *gorm.DB) *middlewareRunner {
	return &middlewareRunner{
		Event: event,
		Tx:    tx,
	}
}

// ExecuteCreateMiddleware executes create middleware
func (me *middlewareRunner) RunCreateMiddleware(middlewares []createMw, input *e.CreateDto, data *e.Doctor) 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) RunReadListMiddleware(middlewares []readListMw, input *e.ReadListDto, data *e.Doctor) 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.Doctor) 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 []readDetailMw, input *e.ReadDetailDto, data *e.Doctor) 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) RunDeleteMiddleware(middlewares []readDetailMw, input *e.ReadDetailDto, data *e.Doctor) 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) setMwType(mwType pu.MWType) {
	me.MwType = mwType
}
