# curatenie.ps1 - sterge artefactele de lucru dinaintea unui commit (regula 1 din COMUN\docs\reguli_lucru.md).
# Sterge DOAR fisiere neurmarite de git; orice fisier tracked (in proiect sau in COMUN) e sarit.
# Utilizare:
#   powershell -File COMUN\utile\curatenie.ps1            # curata proiectul si cache-ul text
#   powershell -File COMUN\utile\curatenie.ps1 -DryRun    # doar listeaza ce ar sterge
param(
    [string]$ProjectRoot,
    [string]$CacheRoot,
    [switch]$DryRun
)

$ErrorActionPreference = 'Stop'

if (-not $ProjectRoot) { $ProjectRoot = Split-Path (Split-Path $PSScriptRoot -Parent) -Parent }
$comun = Join-Path $ProjectRoot 'COMUN'
if (-not $CacheRoot) {
    $CacheRoot = Join-Path (Split-Path $ProjectRoot -Parent) ('_vfp_textcache\' + (Split-Path $ProjectRoot -Leaf).ToLower())
}

# fisierele urmarite de git (proiect + COMUN) - lista de protectie
$tracked = New-Object 'System.Collections.Generic.HashSet[string]' ([StringComparer]::OrdinalIgnoreCase)
foreach ($repo in @($ProjectRoot, $comun)) {
    if (-not (Test-Path (Join-Path $repo '.git'))) { continue }
    foreach ($f in (& git -C $repo ls-files)) {
        [void]$tracked.Add((Join-Path $repo ($f -replace '/', '\')))
    }
}

$tinte = New-Object System.Collections.ArrayList

function Adauga($cale, $tipare, $recursiv, $director) {
    if (-not (Test-Path $cale)) { return }
    $p = @{ Include = $tipare; ErrorAction = 'SilentlyContinue' }
    if ($recursiv) { $p['Path'] = $cale; $p['Recurse'] = $true } else { $p['Path'] = (Join-Path $cale '*') }
    foreach ($it in (Get-ChildItem @p)) {
        if ($director -ne $it.PSIsContainer) { continue }
        if ($tracked.Contains($it.FullName)) { continue }
        [void]$tinte.Add($it)
    }
}

$docs = Join-Path $ProjectRoot 'docs'
$teste = @((Join-Path $comun 'utile\Teste'), (Join-Path $ProjectRoot 'utile\Teste')) | Where-Object { Test-Path $_ }

# patch-uri de review, handoff-uri, planuri de runda, propuneri implementate, note de rezolvare
Adauga $docs @('diff_runda*.patch', 'handoff*.md', 'plan_runda*.md', 'runda*.md', 'propuneri_*.md', 'fix_*.md', 'fix_*.diff') $false $false
# backup-uri de runda si de cache text
Adauga $ProjectRoot @('*.pre_runda*.bak') $true $false
Adauga $CacheRoot @('*.pre_runda*.bak', '*.vc2.bak', '*.sc2.bak') $true $false
# artefacte din rulari de test
foreach ($t in $teste) {
    Adauga $t @('screenshots*', 'uisync*') $true $true
    Adauga $t @('*.fxp', '*.err', '*.log', '*_log.txt', 'bash.exe.stackdump') $true $false
}
Adauga $ProjectRoot @('bash.exe.stackdump') $false $false

# directoarele inglobate in alt director-tinta se sterg oricum odata cu parintele
$dir = @($tinte | Where-Object { $_.PSIsContainer } | ForEach-Object { $_.FullName + '\' })
$lista = @($tinte | Where-Object { $f = $_.FullName; -not ($dir | Where-Object { $f.StartsWith($_, 'OrdinalIgnoreCase') }) })

if ($lista.Count -eq 0) { Write-Output 'curatenie: nimic de sters'; exit 0 }

foreach ($it in ($lista | Sort-Object FullName)) {
    $eticheta = $it.FullName.Replace($ProjectRoot + '\', '').Replace($CacheRoot + '\', 'cache\')
    if ($it.PSIsContainer) { $eticheta = $eticheta + '\' }
    if ($DryRun) {
        Write-Output ('  ar sterge  ' + $eticheta)
    } else {
        Remove-Item -LiteralPath $it.FullName -Recurse -Force
        Write-Output ('  sters      ' + $eticheta)
    }
}
Write-Output ('curatenie: ' + $lista.Count + ' intrari')
