package purchases

import (
	"context"
	"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"
	expenseRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/expenses/repositories"
	expenseService "gitlab.com/mbugroup/lti-api.git/internal/modules/expenses/services"
	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"
	rNonstock "gitlab.com/mbugroup/lti-api.git/internal/modules/master/nonstocks/repositories"
	rProduct "gitlab.com/mbugroup/lti-api.git/internal/modules/master/products/repositories"
	rSupplier "gitlab.com/mbugroup/lti-api.git/internal/modules/master/suppliers/repositories"
	rWarehouse "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/repositories"
	projectFlockKandangRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/repositories"
	rPurchase "gitlab.com/mbugroup/lti-api.git/internal/modules/purchases/repositories"
	service "gitlab.com/mbugroup/lti-api.git/internal/modules/purchases/services"
	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"
	"gorm.io/gorm"
)

type PurchaseModule struct{}

func (PurchaseModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Validate) {
	purchaseRepo := rPurchase.NewPurchaseRepository(db)
	productRepo := rProduct.NewProductRepository(db)
	warehouseRepo := rWarehouse.NewWarehouseRepository(db)
	supplierRepo := rSupplier.NewSupplierRepository(db)
	productWarehouseRepo := rProductWarehouse.NewProductWarehouseRepository(db)
	nonstockRepo := rNonstock.NewNonstockRepository(db)
	kandangRepo := rKandang.NewKandangRepository(db)
	expenseRepository := expenseRepo.NewExpenseRepository(db)
	expenseRealizationRepo := expenseRepo.NewExpenseRealizationRepository(db)
	projectFlockKandangRepository := projectFlockKandangRepo.NewProjectFlockKandangRepository(db)

	approvalRepo := commonRepo.NewApprovalRepository(db)
	approvalService := commonSvc.NewApprovalService(approvalRepo)
	documentRepo := commonRepo.NewDocumentRepository(db)
	documentSvc, err := commonSvc.NewDocumentServiceFromConfig(context.Background(), documentRepo)
	if err != nil {
		panic(fmt.Sprintf("failed to create document service: %v", err))
	}
	if err := approvalService.RegisterWorkflowSteps(utils.ApprovalWorkflowPurchase, utils.PurchaseApprovalSteps); err != nil {
		panic(fmt.Sprintf("failed to register purchase approval workflow: %v", err))
	}
	if err := approvalService.RegisterWorkflowSteps(utils.ApprovalWorkflowExpense, utils.ExpenseApprovalSteps); err != nil {
		panic(fmt.Sprintf("failed to register expense approval workflow: %v", err))
	}
	expenseServiceInstance := expenseService.NewExpenseService(
		expenseRepository,
		supplierRepo,
		nonstockRepo,
		approvalService,
		expenseRealizationRepo,
		projectFlockKandangRepository,
		documentSvc,
		validate,
	)
	expenseBridge := service.NewExpenseBridge(
		db,
		purchaseRepo,
		projectFlockKandangRepository,
		kandangRepo,
		expenseServiceInstance,
	)

	fifoStockV2Service := commonSvc.NewFifoStockV2Service(db, utils.Log)

	purchaseService := service.NewPurchaseService(
		validate,
		purchaseRepo,
		productRepo,
		warehouseRepo,
		supplierRepo,
		productWarehouseRepo,
		projectFlockKandangRepository,
		approvalService,
		expenseBridge,
		fifoStockV2Service,
		documentSvc,
	)

	userRepo := rUser.NewUserRepository(db)
	userService := sUser.NewUserService(userRepo, validate)

	Routes(router, purchaseService, userService)
}
