package transactions

import (
	"fmt"

	"github.com/go-playground/validator/v10"
	"github.com/gofiber/fiber/v2"
	commonRepo "gitlab.com/mbugroup/lti-api.git/internal/common/repository"
	commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
	"gitlab.com/mbugroup/lti-api.git/internal/utils"
	"gorm.io/gorm"

	rTransaction "gitlab.com/mbugroup/lti-api.git/internal/modules/finance/transactions/repositories"
	sTransaction "gitlab.com/mbugroup/lti-api.git/internal/modules/finance/transactions/services"

	rUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/repositories"
	sUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
)

type TransactionModule struct{}

func (TransactionModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Validate) {
	transactionRepo := rTransaction.NewTransactionRepository(db)
	userRepo := rUser.NewUserRepository(db)

	approvalRepo := commonRepo.NewApprovalRepository(db)
	approvalService := commonSvc.NewApprovalService(approvalRepo)
	if err := approvalService.RegisterWorkflowSteps(utils.ApprovalWorkflowPayment, utils.PaymentApprovalSteps); err != nil {
		panic(fmt.Sprintf("failed to register payment approval workflow: %v", err))
	}
	if err := approvalService.RegisterWorkflowSteps(utils.ApprovalWorkflowInitial, utils.InitialApprovalSteps); err != nil {
		panic(fmt.Sprintf("failed to register initial approval workflow: %v", err))
	}
	if err := approvalService.RegisterWorkflowSteps(utils.ApprovalWorkflowInjection, utils.InjectionApprovalSteps); err != nil {
		panic(fmt.Sprintf("failed to register injection approval workflow: %v", err))
	}

	transactionService := sTransaction.NewTransactionService(transactionRepo, approvalService, validate)
	userService := sUser.NewUserService(userRepo, validate)

	TransactionRoutes(router, userService, transactionService)
}
