$outputFile = "codebase.md"
$projectRoot = Get-Location

$includeExtensions = @(
    "*.php", "*.html", "*.htm", "*.css", "*.js", "*.json",
    "*.sql", "*.env", "*.env.example", "*.ini", "*.conf",
    "*.xml", "*.yaml", "*.yml", "*.txt", "*.md",
    "*.sh", "*.bash", "*.py", "*.twig",
    "*.htaccess", "*.config", "*.lock",
    "Dockerfile", "docker-compose*", "captain-definition"
)

$excludeDirs = @(
    "vendor", "node_modules", ".git", ".idea", ".vscode",
    "storage", "cache", ".cache", "tmp", "temp",
    "dist", "build", "public/build", ".docker", "logs"
)

$excludeFiles = @(
    "codebase.md", "codebase.txt", "combine-codebase.ps1",
    "collectcodebase.ps1", "composer.lock", "package-lock.json", "yarn.lock"
)

$excludePattern = ($excludeDirs | ForEach-Object { [regex]::Escape($_) }) -join "|"
$excludeRegex = "(\\|/)($excludePattern)(\\|/|$)"

$allFiles = @()
foreach ($ext in $includeExtensions) {
    $found = Get-ChildItem -Path $projectRoot -Filter $ext -Recurse -File -ErrorAction SilentlyContinue
    $allFiles += $found
}

$specialFiles = @("Dockerfile", "Makefile", "Procfile", "captain-definition", ".htaccess", ".env.example")
foreach ($name in $specialFiles) {
    $found = Get-ChildItem -Path $projectRoot -Filter $name -Recurse -File -ErrorAction SilentlyContinue
    $allFiles += $found
}

$allFiles = $allFiles | Sort-Object FullName -Unique

$filtered = $allFiles | Where-Object {
    $relativePath = $_.FullName.Substring($projectRoot.Path.Length)
    $fileName = $_.Name
    ($relativePath -notmatch $excludeRegex) -and ($fileName -notin $excludeFiles)
}

$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$header = @"
# Full Codebase Export
Generated: $timestamp
Project: $projectRoot
Files: $($filtered.Count)
---

"@

Set-Content -Path $outputFile -Value $header -Encoding UTF8

$fence = "``````"

$fileCount = 0
foreach ($file in $filtered) {
    $relativePath = $file.FullName.Substring($projectRoot.Path.Length + 1)
    $extension = $file.Extension.TrimStart(".")
    if (-not $extension) { $extension = "text" }

    $fileCount++

    $separator = "`n## File: $relativePath`n"
    Add-Content -Path $outputFile -Value $separator -Encoding UTF8

    $openFence = "${fence}${extension}"
    Add-Content -Path $outputFile -Value $openFence -Encoding UTF8

    try {
        $content = Get-Content -Path $file.FullName -Raw -ErrorAction Stop
        Add-Content -Path $outputFile -Value $content -Encoding UTF8
    }
    catch {
        Add-Content -Path $outputFile -Value "[ERROR: Could not read file]" -Encoding UTF8
    }

    Add-Content -Path $outputFile -Value $fence -Encoding UTF8

    Write-Host "[$fileCount] $relativePath" -ForegroundColor Green
}

$fileSize = [math]::Round((Get-Item $outputFile).Length / 1KB, 1)

Write-Host "`n========================================" -ForegroundColor Cyan
Write-Host " Done! $fileCount files combined" -ForegroundColor Cyan
Write-Host " Output: $outputFile ($fileSize KB)" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan