# Fusionne les 25 migrations Flyway (dans legacy/) en un seul fichier V1__UnionFlow_Complete_Schema.sql
$migrationDir = Join-Path $PSScriptRoot "..\src\main\resources\db\migration"
$legacyDir = Join-Path (Split-Path $migrationDir -Parent) "legacy-migrations"
$sourceDir = if (Test-Path $legacyDir) { $legacyDir } else { $migrationDir }
$order = @('V1.2','V1.3','V1.4','V1.5','V1.6','V1.7','V2.0','V2.1','V2.2','V2.3','V2.4','V2.5','V2.6','V2.7','V2.8','V2.9','V2.10','V3.0','V3.1','V3.2','V3.3','V3.4','V3.5','V3.6','V3.7')
$out = @()
$out += '-- UnionFlow : schema complet (consolidation des migrations V1.2 a V3.7)'
$out += '-- Nouvelle base : ce script suffit. Bases existantes : voir README_CONSOLIDATION.md'
$out += ''
foreach ($ver in $order) {
  $f = Get-ChildItem -Path $sourceDir -Filter "${ver}__*.sql" -ErrorAction SilentlyContinue | Select-Object -First 1
  if ($f) {
    $out += "-- ========== $($f.Name) =========="
    $out += [System.IO.File]::ReadAllText($f.FullName)
    $out += ''
  }
}
$outPath = Join-Path $migrationDir "V1__UnionFlow_Complete_Schema.sql"
[System.IO.File]::WriteAllText($outPath, ($out -join "`r`n"))
$lines = (Get-Content $outPath | Measure-Object -Line).Lines
Write-Host "Ecrit $outPath ($lines lignes)"
