package data

import (
	datapayment "kra/internal/data/payment"
	datasystem "kra/internal/data/repository"
	"kra/pkg/database/migration"
	"kra/pkg/module"

	"gorm.io/gorm"
)

func InfrastructureMigrations() []migration.Step {
	return []migration.Step{
		{
			ID: "202608200001_data_infrastructure",
			Migrate: func(db *gorm.DB) error {
				return migration.CreateMissingTables(db, &integrationConfigPO{})
			},
		},
		{ID: "202608210001_communication_integration_defaults", Migrate: ensureCommunicationIntegrationConfigs},
	}
}

// migrateAll is the single data-layer migration entry point. Module-specific
// schema work stays with the module that owns its persistent objects.
func migrateAll(db *gorm.DB, catalogs ...module.Catalog) error {
	steps := InfrastructureMigrations()
	if len(catalogs) > 0 && len(catalogs[0].MigrationSteps()) > 0 {
		steps = append(steps, catalogs[0].MigrationSteps()...)
	} else {
		steps = append(steps, datasystem.Migrations()...)
		steps = append(steps, datapayment.Migrations()...)
	}
	return migration.Run(db, steps)
}
