package uniformitys

import (
	"context"
	"fmt"

	"github.com/go-playground/validator/v10"
	"github.com/gofiber/fiber/v2"
	"gorm.io/gorm"

	commonRepo "gitlab.com/mbugroup/lti-api.git/internal/common/repository"
	commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
	rProductionStandard "gitlab.com/mbugroup/lti-api.git/internal/modules/master/production-standards/repositories"
	rProjectFlock "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/repositories"
	rUniformity "gitlab.com/mbugroup/lti-api.git/internal/modules/production/uniformities/repositories"
	sUniformity "gitlab.com/mbugroup/lti-api.git/internal/modules/production/uniformities/services"

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

type UniformityModule struct{}

func (UniformityModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Validate) {
	uniformityRepo := rUniformity.NewUniformityRepository(db)
	documentRepo := commonRepo.NewDocumentRepository(db)
	approvalRepo := commonRepo.NewApprovalRepository(db)
	projectFlockKandangRepo := rProjectFlock.NewProjectFlockKandangRepository(db)
	productionStandardRepo := rProductionStandard.NewProductionStandardRepository(db)
	standardGrowthDetailRepo := rProductionStandard.NewStandardGrowthDetailRepository(db)
	userRepo := rUser.NewUserRepository(db)

	documentSvc, err := commonSvc.NewDocumentServiceFromConfig(context.Background(), documentRepo)
	if err != nil {
		panic(fmt.Sprintf("failed to create document service: %v", err))
	}

	approvalSvc := commonSvc.NewApprovalService(approvalRepo)
	if err := approvalSvc.RegisterWorkflowSteps(utils.ApprovalWorkflowUniformity, utils.UniformityApprovalSteps); err != nil {
		panic(fmt.Sprintf("failed to register uniformity approval workflow: %v", err))
	}

	uniformityService := sUniformity.NewUniformityService(
		uniformityRepo,
		documentSvc,
		approvalRepo,
		approvalSvc,
		projectFlockKandangRepo,
		productionStandardRepo,
		standardGrowthDetailRepo,
		validate,
	)
	userService := sUser.NewUserService(userRepo, validate)

	UniformityRoutes(router, userService, uniformityService)
}
