<#
Nama Script   : IndexDestroyer.ps1
Deskripsi     :
Script ini mencari dan memindahkan file index (default: index.md) ke Recycle Bin
dari direktori yang dipilih user, dengan opsi kedalaman folder seperti FolderTreePrinter.
 
Alur User     :
1. User memasukkan path folder target.
2. Script validasi path, jika tidak ada minta ulang.
3. User bisa ganti nama file target (default: index.md) atau Enter untuk pakai default.
4. User pilih scope:
   - Y  : Semua subfolder (rekursif penuh)
   - N  : Batal, kembali ke input path
   - Angka : Sampai level kedalaman tertentu
5. Script preview dulu file yang akan di-move (dry run tampilan).
6. User konfirmasi Y/N sebelum eksekusi.
7. Script pindahkan ke Recycle Bin dan tampilkan summary.
#>
 
# ── Shell.Application untuk Recycle Bin ───────────────────────────────────────
$shell = New-Object -ComObject Shell.Application
 
function Move-ToRecycleBin {
    param ([string]$FilePath)
    $item = $shell.Namespace(0).ParseName($FilePath)
    if ($item) {
        $item.InvokeVerb("delete")
        return $true
    }
    return $false
}
 
# ── Collect files sampai level tertentu ───────────────────────────────────────
function Get-TargetFiles {
    param (
        [string]$Path,
        [string]$FileName,
        [int]$Level = 0,
        [int]$MaxLevel = -1   # -1 = unlimited
    )
 
    $results = @()
 
    # Cek file di level ini
    $filePath = Join-Path $Path $FileName
    if (Test-Path $filePath -PathType Leaf) {
        $results += $filePath
    }
 
    # Stop kalau sudah capai max level
    if ($MaxLevel -ne -1 -and $Level -ge $MaxLevel) {
        return $results
    }
 
    # Rekursi ke subfolder
    $subFolders = Get-ChildItem -Path $Path -Directory -ErrorAction SilentlyContinue | Sort-Object Name
    foreach ($folder in $subFolders) {
        $results += Get-TargetFiles -Path $folder.FullName -FileName $FileName -Level ($Level + 1) -MaxLevel $MaxLevel
    }
 
    return $results
}
 
# ── Print tree preview dengan highlight file target ───────────────────────────
function Print-PreviewTree {
    param (
        [string]$Path,
        [string]$FileName,
        [int]$Level = 0,
        [int]$MaxLevel = -1,
        [ref]$FoundCount
    )
 
    if ($MaxLevel -ne -1 -and $Level -ge $MaxLevel) { return }
 
    $indent = "  " * $Level
 
    # Cek apakah ada file target di folder ini
    $filePath = Join-Path $Path $FileName
    if (Test-Path $filePath -PathType Leaf) {
        Write-Host "$indent  🗑️  $FileName" -ForegroundColor Red
        $FoundCount.Value++
    }
 
    $subFolders = Get-ChildItem -Path $Path -Directory -ErrorAction SilentlyContinue | Sort-Object Name
    foreach ($folder in $subFolders) {
        Write-Host "$indent  📁 $($folder.Name)" -ForegroundColor DarkGray
        Print-PreviewTree -Path $folder.FullName -FileName $FileName -Level ($Level + 1) -MaxLevel $MaxLevel -FoundCount $FoundCount
    }
}
 
# ── Banner ─────────────────────────────────────────────────────────────────────
Clear-Host
Write-Host "╔══════════════════════════════════════════════╗" -ForegroundColor DarkRed
Write-Host "║        🗑️   INDEX DESTROYER  v1.0            ║" -ForegroundColor DarkRed
Write-Host "║   Move index files to Recycle Bin by depth   ║" -ForegroundColor DarkRed
Write-Host "╚══════════════════════════════════════════════╝" -ForegroundColor DarkRed
Write-Host ""
 
# ── MAIN LOOP ─────────────────────────────────────────────────────────────────
do {
 
    # ── Step 1: Input path ────────────────────────────────────────────────────
    do {
        $targetFolder = Read-Host "📂 Masukkan path folder target (contoh: D:\MyProject)"
        $targetFolder = $targetFolder.Trim('"').Trim("'").Trim()
 
        if (-not (Test-Path $targetFolder -PathType Container)) {
            Write-Host "`n❌ Path tidak ditemukan atau bukan folder. Periksa kembali!`n" -ForegroundColor Red
            $validPath = $false
        } else {
            $validPath = $true
        }
    } while (-not $validPath)
 
    # ── Step 2: Input nama file target ────────────────────────────────────────
    Write-Host ""
    $inputFileName = Read-Host "📄 Nama file target (Enter = pakai default 'index.md')"
    $inputFileName = $inputFileName.Trim()
 
    if ([string]::IsNullOrWhiteSpace($inputFileName)) {
        $targetFileName = "index.md"
    } else {
        $targetFileName = $inputFileName
    }
 
    Write-Host "  → Target file: " -NoNewline
    Write-Host $targetFileName -ForegroundColor Cyan
 
    # ── Step 3: Pilih scope / kedalaman ───────────────────────────────────────
    $restartToPath = $false
 
    do {
        Write-Host ""
        $inputLevel = Read-Host "🔍 Scope pencarian? (Y = semua subfolder, N = batal, atau angka level kedalaman)"
        $inputLevel = $inputLevel.Trim().ToLower()
 
        if ($inputLevel -eq 'n') {
            Write-Host "`n⚠️  Dibatalkan. Kembali ke pemilihan folder...`n" -ForegroundColor Yellow
            $restartToPath = $true
            break
        }
        elseif ($inputLevel -eq 'y') {
            $maxLevel = -1
            $scopeLabel = "semua subfolder (rekursif penuh)"
            $validScope = $true
        }
        elseif ([int]::TryParse($inputLevel, [ref]$null)) {
            $maxLevel = [int]$inputLevel
            if ($maxLevel -le 0) {
                Write-Host "⚠️  Masukkan angka lebih besar dari 0." -ForegroundColor Yellow
                $validScope = $false
            } else {
                $scopeLabel = "kedalaman level $maxLevel"
                $validScope = $true
            }
        }
        else {
            Write-Host "❌ Input tidak valid. Masukkan 'Y', 'N', atau angka." -ForegroundColor Red
            $validScope = $false
        }
    } while (-not $validScope -and -not $restartToPath)
 
    if ($restartToPath) { continue }
 
    # ── Step 4: Preview / Dry Run ─────────────────────────────────────────────
    Write-Host ""
    Write-Host "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" -ForegroundColor DarkGray
    Write-Host "🔎 PREVIEW — File yang akan dipindah ke Recycle Bin:" -ForegroundColor Yellow
    Write-Host "   Folder  : $targetFolder" -ForegroundColor Gray
    Write-Host "   File    : $targetFileName" -ForegroundColor Gray
    Write-Host "   Scope   : $scopeLabel" -ForegroundColor Gray
    Write-Host "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" -ForegroundColor DarkGray
    Write-Host ""
 
    $foundCount = [ref]0
    Write-Host "📁 $targetFolder" -ForegroundColor White
    Print-PreviewTree -Path $targetFolder -FileName $targetFileName -MaxLevel $maxLevel -FoundCount $foundCount
 
    Write-Host ""
    Write-Host "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" -ForegroundColor DarkGray
 
    if ($foundCount.Value -eq 0) {
        Write-Host "✅ Tidak ada file '$targetFileName' ditemukan dalam scope ini." -ForegroundColor Green
        Write-Host ""
        $retry = Read-Host "🔁 Coba folder/file lain? (Y = ya, N = keluar)"
        if ($retry.Trim().ToLower() -ne 'y') {
            break
        } else {
            continue
        }
    }
 
    Write-Host "📊 Total ditemukan: " -NoNewline
    Write-Host "$($foundCount.Value) file" -ForegroundColor Red -NoNewline
    Write-Host " akan dipindah ke Recycle Bin."
    Write-Host ""
 
    # ── Step 5: Konfirmasi eksekusi ───────────────────────────────────────────
    $confirm = Read-Host "⚠️  Lanjutkan? File TIDAK langsung terhapus, masuk Recycle Bin dulu (Y/N)"
    $confirm = $confirm.Trim().ToLower()
 
    if ($confirm -ne 'y') {
        Write-Host "`n❌ Dibatalkan. Tidak ada file yang dipindah.`n" -ForegroundColor Yellow
        $retry = Read-Host "🔁 Coba lagi? (Y = ya, N = keluar)"
        if ($retry.Trim().ToLower() -ne 'y') { break } else { continue }
    }
 
    # ── Step 6: Eksekusi ──────────────────────────────────────────────────────
    Write-Host ""
    Write-Host "🚀 Memindahkan file..." -ForegroundColor Cyan
 
    $filesToMove = Get-TargetFiles -Path $targetFolder -FileName $targetFileName -MaxLevel $maxLevel
    $successCount = 0
    $failCount    = 0
    $failedFiles  = @()
 
    foreach ($file in $filesToMove) {
        try {
            $fileItem = $shell.Namespace(0).ParseName($file)
            if ($fileItem) {
                $fileItem.InvokeVerb("delete")
                Write-Host "  ✅ Moved → $file" -ForegroundColor Green
                $successCount++
            } else {
                throw "ParseName returned null"
            }
        }
        catch {
            Write-Host "  ❌ Gagal  → $file" -ForegroundColor Red
            $failCount++
            $failedFiles += $file
        }
    }
 
    # ── Step 7: Summary ───────────────────────────────────────────────────────
    Write-Host ""
    Write-Host "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" -ForegroundColor DarkGray
    Write-Host "📊 SUMMARY" -ForegroundColor White
    Write-Host "   ✅ Berhasil dipindah : $successCount file" -ForegroundColor Green
    if ($failCount -gt 0) {
        Write-Host "   ❌ Gagal             : $failCount file" -ForegroundColor Red
        foreach ($f in $failedFiles) {
            Write-Host "      - $f" -ForegroundColor DarkRed
        }
    }
    Write-Host "   🗑️  Semua file ada di Recycle Bin — bisa di-restore kalau perlu." -ForegroundColor DarkGray
    Write-Host "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" -ForegroundColor DarkGray
    Write-Host ""
 
    # ── Selesai / ulangi ──────────────────────────────────────────────────────
    $again = Read-Host "🔁 Destroy folder/file lain? (Y = ya, N = keluar)"
    if ($again.Trim().ToLower() -ne 'y') { break }
 
} while ($true)
 
Write-Host ""
Write-Host "👋 IndexDestroyer selesai. Sampai jumpa!" -ForegroundColor DarkCyan
Write-Host ""