package recordings

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"
	rProductWarehouse "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/product-warehouses/repositories"
	rFlock "gitlab.com/mbugroup/lti-api.git/internal/modules/master/flocks/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"
	rProductionStandard "gitlab.com/mbugroup/lti-api.git/internal/modules/master/production-standards/repositories"
	sProductionStandard "gitlab.com/mbugroup/lti-api.git/internal/modules/master/production-standards/services"
	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"
	sProjectFlock "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/services"
	rRecording "gitlab.com/mbugroup/lti-api.git/internal/modules/production/recordings/repositories"
	sRecording "gitlab.com/mbugroup/lti-api.git/internal/modules/production/recordings/services"
	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"
	rStockLogs "gitlab.com/mbugroup/lti-api.git/internal/modules/shared/repositories"
	"gitlab.com/mbugroup/lti-api.git/internal/utils"
	"gitlab.com/mbugroup/lti-api.git/internal/utils/fifo"

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

type RecordingModule struct{}

func (RecordingModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Validate) {
	recordingRepo := rRecording.NewRecordingRepository(db)
	projectFlockRepo := rProjectFlock.NewProjectflockRepository(db)
	projectFlockKandangRepo := rProjectFlock.NewProjectFlockKandangRepository(db)
	projectFlockPopulationRepo := rProjectFlock.NewProjectFlockPopulationRepository(db)
	projectBudgetRepo := rProjectFlock.NewProjectBudgetRepository(db)
	productWarehouseRepo := rProductWarehouse.NewProductWarehouseRepository(db)
	flockRepo := rFlock.NewFlockRepository(db)
	kandangRepo := rKandang.NewKandangRepository(db)
	warehouseRepo := rWarehouse.NewWarehouseRepository(db)
	nonstockRepo := rNonstock.NewNonstockRepository(db)
	productRepo := rProduct.NewProductRepository(db)
	chickinRepo := rChickin.NewChickinRepository(db)
	chickinDetailRepo := rChickin.NewChickinDetailRepository(db)
	transferLayingRepo := rTransferLaying.NewTransferLayingRepository(db)
	layingTransferSourceRepo := rTransferLaying.NewLayingTransferSourceRepository(db)
	layingTransferTargetRepo := rTransferLaying.NewLayingTransferTargetRepository(db)
	stockLogRepo := rStockLogs.NewStockLogRepository(db)
	productionStandardRepo := rProductionStandard.NewProductionStandardRepository(db)
	productionStandardDetailRepo := rProductionStandard.NewProductionStandardDetailRepository(db)
	standardGrowthDetailRepo := rProductionStandard.NewStandardGrowthDetailRepository(db)

	productionStandardService := sProductionStandard.NewProductionStandardService(
		productionStandardRepo,
		productionStandardDetailRepo,
		standardGrowthDetailRepo,
		validate,
	)

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

	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))
		}
	}

	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.ApprovalWorkflowRecording, utils.RecordingApprovalSteps); err != nil {
		panic(fmt.Sprintf("failed to register recording approval workflow: %v", err))
	}
	if err := approvalService.RegisterWorkflowSteps(utils.ApprovalWorkflowProjectFlock, utils.ProjectFlockApprovalSteps); err != nil {
		panic(fmt.Sprintf("failed to register project flock approval workflow: %v", err))
	}

	userRepo := rUser.NewUserRepository(db)

	projectFlockService := sProjectFlock.NewProjectflockService(
		projectFlockRepo,
		flockRepo,
		kandangRepo,
		projectFlockKandangRepo,
		warehouseRepo,
		productWarehouseRepo,
		projectBudgetRepo,
		nonstockRepo,
		projectFlockPopulationRepo,
		recordingRepo,
		transferLayingRepo,
		approvalService,
		validate,
	)

	chickinService := sChickin.NewChickinService(
		chickinRepo,
		kandangRepo,
		warehouseRepo,
		productWarehouseRepo,
		productRepo,
		projectFlockRepo,
		projectFlockKandangRepo,
		projectFlockPopulationRepo,
		chickinDetailRepo,
		transferLayingRepo,
		validate,
		fifoStockV2Service,
	)

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

	recordingService := sRecording.NewRecordingService(
		recordingRepo,
		projectFlockKandangRepo,
		productWarehouseRepo,
		projectFlockPopulationRepo,
		approvalRepo,
		approvalService,
		fifoStockV2Service,
		stockLogRepo,
		productionStandardService,
		projectFlockService,
		chickinService,
		transferLayingRepo,
		transferLayingService,
		validate,
	)
	userService := sUser.NewUserService(userRepo, validate)

	RecordingRoutes(router, userService, recordingService)
}
