package transfer_layings

import (
	"fmt"
	"strings"

	"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"
	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"
	sTransferLaying "gitlab.com/mbugroup/lti-api.git/internal/modules/production/transfer_layings/services"
	"gitlab.com/mbugroup/lti-api.git/internal/utils"
	"gitlab.com/mbugroup/lti-api.git/internal/utils/fifo"

	rInventory "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/product-warehouses/repositories"
	rWarehouse "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/repositories"
	rUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/repositories"
	sUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
)

type TransferLayingModule struct{}

func (TransferLayingModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Validate) {
	transferLayingRepo := rTransferLaying.NewTransferLayingRepository(db)
	layingTransferSourceRepo := rTransferLaying.NewLayingTransferSourceRepository(db)
	layingTransferTargetRepo := rTransferLaying.NewLayingTransferTargetRepository(db)
	userRepo := rUser.NewUserRepository(db)
	projectFlockRepo := rProjectFlock.NewProjectflockRepository(db)
	projectFlockKandangRepo := rProjectFlock.NewProjectFlockKandangRepository(db)
	projectFlockPopulationRepo := rProjectFlock.NewProjectFlockPopulationRepository(db)
	productWarehouseRepo := rInventory.NewProductWarehouseRepository(db)
	warehouseRepo := rWarehouse.NewWarehouseRepository(db)

	stockAllocationRepo := commonRepo.NewStockAllocationRepository(db)
	fifoService := commonSvc.NewFifoService(db, stockAllocationRepo, productWarehouseRepo, utils.Log)
	fifoStockV2Service := commonSvc.NewFifoStockV2Service(db, utils.Log)

	// daftarin jadi stockable
	if err := fifoService.RegisterStockable(fifo.StockableConfig{
		Key:   fifo.StockableKeyTransferToLayingIn,
		Table: "laying_transfer_targets",
		Columns: fifo.StockableColumns{
			ID:                 "id",
			ProductWarehouseID: "product_warehouse_id",
			TotalQuantity:      "total_qty",
			TotalUsedQuantity:  "total_used",
			CreatedAt:          "created_at",
		},
		OrderBy: []string{"created_at ASC", "id ASC"},
	}); err != nil {
		if !strings.Contains(strings.ToLower(err.Error()), "already registered") {
			panic(fmt.Sprintf("failed to register transfer to laying stockable workflow: %v", err))
		}
	}

	// daftarin jadi usable
	if err := fifoService.RegisterUsable(fifo.UsableConfig{
		Key:   fifo.UsableKeyTransferToLayingOut,
		Table: "laying_transfers",
		Columns: fifo.UsableColumns{
			ID:                 "id",
			ProductWarehouseID: "source_product_warehouse_id",
			UsageQuantity:      "source_usage_qty",
			PendingQuantity:    "source_pending_usage_qty",
			CreatedAt:          "created_at",
		},
		OrderBy: []string{"created_at ASC", "id ASC"},
	}); err != nil {
		if !strings.Contains(strings.ToLower(err.Error()), "already registered") {
			panic(fmt.Sprintf("failed to register transfer to laying usable workflow: %v", err))
		}
	}

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

	transferLayingService := sTransferLaying.NewTransferLayingService(
		transferLayingRepo,
		layingTransferSourceRepo,
		layingTransferTargetRepo,
		projectFlockRepo,
		projectFlockKandangRepo,
		projectFlockPopulationRepo,
		productWarehouseRepo,
		warehouseRepo,
		approvalService,
		fifoStockV2Service,
		validate,
	)
	userService := sUser.NewUserService(userRepo, validate)

	TransferLayingRoutes(router, userService, transferLayingService)
}
