package project_flock_kandangs

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"
	rExpense "gitlab.com/mbugroup/lti-api.git/internal/modules/expenses/repositories"
	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"
	rWarehouse "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/repositories"
	sProjectFlockKandang "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project-flock-kandangs/services"
	rProjectFlockKandang "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/repositories"
	"gitlab.com/mbugroup/lti-api.git/internal/utils"

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

type ProjectFlockKandangModule struct{}

func (ProjectFlockKandangModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Validate) {
	projectFlockKandangRepo := rProjectFlockKandang.NewProjectFlockKandangRepository(db)
	projectFlockPopulationRepo := rProjectFlockKandang.NewProjectFlockPopulationRepository(db)
	userRepo := rUser.NewUserRepository(db)
	warehouseRepo := rWarehouse.NewWarehouseRepository(db)
	productWarehouseRepo := rProductWarehouse.NewProductWarehouseRepository(db)
	kandangRepo := rKandang.NewKandangRepository(db)

	approvalRepo := commonRepo.NewApprovalRepository(db)
	approvalService := commonSvc.NewApprovalService(approvalRepo)
	fifoStockV2Service := commonSvc.NewFifoStockV2Service(db, utils.Log)
	// register workflow steps for chickin approvals
	if err := approvalService.RegisterWorkflowSteps(utils.ApprovalWorkflowProjectFlockKandang, utils.ProjectFlockKandangApprovalSteps); err != nil {
		panic(fmt.Sprintf("failed to register chickin approval workflow: %v", err))
	}

	expenseRepo := rExpense.NewExpenseRepository(db)
	projectFlockKandangService := sProjectFlockKandang.NewProjectFlockKandangService(projectFlockKandangRepo, approvalService, fifoStockV2Service, expenseRepo, warehouseRepo, productWarehouseRepo, projectFlockPopulationRepo, kandangRepo, validate)
	userService := sUser.NewUserService(userRepo, validate)

	ProjectFlockKandangRoutes(router, userService, projectFlockKandangService)
}
