package chickins

import (
	"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"

	rProductWarehouse "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/product-warehouses/repositories"
	rKandang "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs/repositories"
	rProduct "gitlab.com/mbugroup/lti-api.git/internal/modules/master/products/repositories"
	rWarehouse "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/repositories"
	rChickin "gitlab.com/mbugroup/lti-api.git/internal/modules/production/chickins/repositories"
	sChickin "gitlab.com/mbugroup/lti-api.git/internal/modules/production/chickins/services"

	rProjectFlock "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/repositories"
	rTransferLaying "gitlab.com/mbugroup/lti-api.git/internal/modules/production/transfer_layings/repositories"

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

	utils "gitlab.com/mbugroup/lti-api.git/internal/utils"
)

type ChickinModule struct{}

func (ChickinModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Validate) {
	chickinRepo := rChickin.NewChickinRepository(db)
	chickinDetailRepo := rChickin.NewChickinDetailRepository(db)
	kandangRepo := rKandang.NewKandangRepository(db)

	warehouseRepo := rWarehouse.NewWarehouseRepository(db)
	projectflockkandangrepo := rProjectFlock.NewProjectFlockKandangRepository(db)
	projectflockpopulationrepo := rProjectFlock.NewProjectFlockPopulationRepository(db)
	projectFlockRepo := rProjectFlock.NewProjectflockRepository(db)
	transferLayingRepo := rTransferLaying.NewTransferLayingRepository(db)
	productWarehouseRepo := rProductWarehouse.NewProductWarehouseRepository(db)
	productRepo := rProduct.NewProductRepository(db)
	fifoStockV2Service := commonSvc.NewFifoStockV2Service(db, utils.Log)
	userRepo := rUser.NewUserRepository(db)

	approvalRepo := commonRepo.NewApprovalRepository(db)
	approvalService := commonSvc.NewApprovalService(approvalRepo)
	if err := approvalService.RegisterWorkflowSteps(utils.ApprovalWorkflowChickin, utils.ChickinApprovalSteps); err != nil {
		panic(fmt.Sprintf("failed to register chickin approval workflow: %v", err))
	}

	chickinService := sChickin.NewChickinService(
		chickinRepo,
		kandangRepo,
		warehouseRepo,
		productWarehouseRepo,
		productRepo,
		projectFlockRepo,
		projectflockkandangrepo,
		projectflockpopulationrepo,
		chickinDetailRepo,
		transferLayingRepo,
		validate,
		fifoStockV2Service)
	userService := sUser.NewUserService(userRepo, validate)

	ChickinRoutes(router, userService, chickinService)
}
