
param(
    [string]$MigrationDirectory = "C:\AxiUmDatabaseMigration"
)

# Import helper modules
$scriptDir = $null
if ($MyInvocation.MyCommand.Path) {
    $scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
}
if (-not $scriptDir) {
    $scriptDir = Get-Location -Parent
}

$global:ScriptDirectory =  Split-Path ($scriptDir) -Parent

# Set the global migration directory
$global:MigrationDirectory = $MigrationDirectory

$global:Timestamp = Get-Date -Format "yyyyMMdd_HHmmss"

# Import utility module for shared helper functions
# hardcoded to avoid duplicatation
$utilityPath = Join-Path $global:ScriptDirectory "helper\Helper-Utilities.psm1"
if (Test-Path $utilityPath) {
    Import-Module $utilityPath -Force
    Write-Host "Imported shared utility module" -ForegroundColor Cyan
} else {
    Write-Host "Warning: Helper utility module not found at $utilityPath" -ForegroundColor Yellow
}


# Import logger module and initialize shared log file for the entire migration process
# All helper modules will use this shared log file instead of creating individual log files
$loggerPath = Get-HelperScriptPath "Helper-Logger.psm1"
if (Test-Path $loggerPath) {
    Import-Module $loggerPath -Force
    
    # Initialize shared log file for the entire migration process
    $sharedLogFile = New-SharedLogFile -MigrationDirectory $global:MigrationDirectory
    Write-Host "Using log file: $sharedLogFile" -ForegroundColor Cyan
    
    # Test the logger to ensure it's working
    Write-HelperLog -Message "Migration script started" -Level "Info" -LogPrefix "Migration" -MigrationDirectory $global:MigrationDirectory
} else {
    Write-Host "Warning: Helper logger module not found at $loggerPath" -ForegroundColor Yellow
}

Show-Info "Migration Directory: $global:MigrationDirectory" -ForegroundColor Cyan
Show-Info "Script Directory: $global:ScriptDirectory" -ForegroundColor Cyan
Show-Info "Axium Upgrade Path: $(Get-AxiumUpgradePath)" -ForegroundColor Cyan
Show-Info "App Settings Path: $(Get-AppSettingsPath)" -ForegroundColor Cyan
Show-Info "Helper Script Path: $(Get-HelperScriptPath)" -ForegroundColor Cyan
Show-Info "Postgres Output Path: $(Get-PostgresOutputPath)" -ForegroundColor Cyan
Show-Info "Oracle Output Path: $(Get-OracleOutputPath)" -ForegroundColor Cyan
Show-Info "SQL Files Path: $(Get-SQLFilesPath)" -ForegroundColor Cyan
Show-Info "Output Path: $(Get-OutputPath)" -ForegroundColor Cyan
Show-Info "Properties File: $(Get-PropertiesFilePath)" -ForegroundColor Cyan

# Global variables to store properties
$script:Properties = @{}

# Test reading properties

function Read-Properties {
    param([string]$PropertiesFile)
    
    Show-Info "Loading properties from: $PropertiesFile"
    
    if (-not (Test-Path $PropertiesFile)) {
        Show-Error "Properties file not found: $PropertiesFile"
        return $false
    }
    
    try {
        $content = Get-Content $PropertiesFile -ErrorAction Stop
        foreach ($line in $content) {
            if ($line -match '^([^=]+)=(.*)$') {
                $key = $matches[1].Trim()
                $value = $matches[2].Trim()
                $script:Properties[$key] = $value
            }
        }
        
        Show-Success "Successfully loaded $($script:Properties.Count) properties"
        return $true
    } catch {
        Show-Error "Failed to load properties: $($_.Exception.Message)"
        return $false
    }
}


$propertiesFile = Get-PropertiesFilePath
if (Read-Properties $propertiesFile) {
    Show-Success "Database credentials obtained successfully!"
} else {
}

# Test removing non-trigger ddl in files
$runMtkModule = Get-HelperScriptPath "Helper-Run-MTK-Missing-Oracle-Objects.psm1"
Import-Module $runMtkModule -Force
$filesToKeep = Get-ChildItem -Path $(Get-SQLFilesPath) -Filter "*.sql"
Remove-NonTriggerDDLInFiles -Files $filesToKeep

exit 0
