function Copy-FilesContent {
    param (
        [string]$Directory,
        [string]$OutputFile
    )

    # Clear output file if it exists
    if (Test-Path -Path $OutputFile) {
        try {
            Remove-Item -Path $OutputFile -Force
        }
        catch {
            Write-Output "Error deleting $($OutputFile): $($_.Exception.Message)"
            return
        }
    }

    try {
        New-Item -Path $OutputFile -ItemType File -Force | Out-Null
    }
    catch {
        Write-Output "Error creating $($OutputFile): $($_.Exception.Message)"
        return
    }

    # Include package.json, tsconfig, and eslint files first
    $packageJsonPath = Join-Path -Path $Directory -ChildPath "package.json"
    $tsconfigPath = Join-Path -Path $Directory -ChildPath "tsconfig.json"
    $eslintPath = Join-Path -Path $Directory -ChildPath ".eslintrc.json"

    if (Test-Path -Path $packageJsonPath) {
        Add-Content -Path $OutputFile -Value "`n`n// $packageJsonPath"
        $packageJsonContent = Get-Content -Path $packageJsonPath
        Add-Content -Path $OutputFile -Value $packageJsonContent
    }

    if (Test-Path -Path $tsconfigPath) {
        Add-Content -Path $OutputFile -Value "`n`n// $tsconfigPath"
        $tsconfigContent = Get-Content -Path $tsconfigPath
        Add-Content -Path $OutputFile -Value $tsconfigContent
    }

    if (Test-Path -Path $eslintPath) {
        Add-Content -Path $OutputFile -Value "`n`n// $eslintPath"
        $eslintContent = Get-Content -Path $eslintPath
        Add-Content -Path $OutputFile -Value $eslintContent
    }

    # Loop through files and directories recursively
    Get-ChildItem -Path $Directory -Recurse | ForEach-Object {
        $file = $_.FullName
        $extension = [System.IO.Path]::GetExtension($file)

        # Exclude specific file types and directories
        if ($extension -match "\.(ico|jpg|jpeg|png|svg|bmp|gif|db|gz)" -or
            $file -like "*node_modules*" -or
            $file -like "*settings.json" -or
            $file -like "*.next*" -or
            $file -like "*ssr*" -or
            $file -like "*vendor-chunks*" -or
            $file -like "*next.config.js" -or
            $file -like "*package-lock.json") {
            Write-Output "Skipping file: $file"
            return
        }

        # Include text-based file types
        if ($extension -in @(".tsx", ".js", ".ts", ".config.mjs", ".json", ".alo")) {
            try {
                Add-Content -Path $OutputFile -Value "`n`n// $file"
                $fileContent = Get-Content -Path $file | Where-Object { $_ -notmatch 'node_modules' -and $_ -notmatch 'webpack' }
                Add-Content -Path $OutputFile -Value $fileContent
            }
            catch {
                Write-Output "Error writing to $($OutputFile): $($_.Exception.Message)"
                return
            }
        }
    }
}

# Main script
$outputFile = "output.txt"
Copy-FilesContent -Directory "." -OutputFile $outputFile

Write-Output "Text files content copied to $($outputFile)"


Get-Content : No existe ningún objeto en la ruta de acceso especificada C:\Users\israe\Documents\GitHub\webapp\apps\web\src\app\(non-authenticated)\reset-password\[token]\page.tsx, o bien se 
ha filtrado por el parámetro -Include o -Exclude.
En C:\Users\israe\Documents\GitHub\webapp\Copy-FilesContent.ps1: 71 Carácter: 32
+                 $fileContent = Get-Content -Path $file | Where-Object ...
+                                ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (System.String[]:String[]) [Get-Content], Exception
    + FullyQualifiedErrorId : ItemNotFound,Microsoft.PowerShell.Commands.GetContentCommand
 
Get-Content : No existe ningún objeto en la ruta de acceso especificada 
C:\Users\israe\Documents\GitHub\webapp\apps\web\src\app\(non-authenticated)\reset-password\[token]\components\ErrorAlert\index.tsx, o bien se ha filtrado por el parámetro -Include o -Exclude.   
En C:\Users\israe\Documents\GitHub\webapp\Copy-FilesContent.ps1: 71 Carácter: 32
+                 $fileContent = Get-Content -Path $file | Where-Object ...
+                                ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (System.String[]:String[]) [Get-Content], Exception
    + FullyQualifiedErrorId : ItemNotFound,Microsoft.PowerShell.Commands.GetContentCommand
 
Get-Content : No existe ningún objeto en la ruta de acceso especificada 
C:\Users\israe\Documents\GitHub\webapp\apps\web\src\app\(non-authenticated)\reset-password\[token]\components\form\index.tsx, o bien se ha filtrado por el parámetro -Include o -Exclude.
En C:\Users\israe\Documents\GitHub\webapp\Copy-FilesContent.ps1: 71 Carácter: 32
+                 $fileContent = Get-Content -Path $file | Where-Object ...
+                                ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (System.String[]:String[]) [Get-Content], Exception
    + FullyQualifiedErrorId : ItemNotFound,Microsoft.PowerShell.Commands.GetContentCommand
 
Get-Content : No existe ningún objeto en la ruta de acceso especificada 
C:\Users\israe\Documents\GitHub\webapp\apps\web\src\app\(non-authenticated)\reset-password\[token]\components\messageSuccess\index.tsx, o bien se ha filtrado por el parámetro -Include o         
-Exclude.
En C:\Users\israe\Documents\GitHub\webapp\Copy-FilesContent.ps1: 71 Carácter: 32
+                 $fileContent = Get-Content -Path $file | Where-Object ...
+                                ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (System.String[]:String[]) [Get-Content], Exception
    + FullyQualifiedErrorId : ItemNotFound,Microsoft.PowerShell.Commands.GetContentCommand
 
Text files content copied to output.txt
PS C:\Users\israe\Documents\GitHub\webapp>
PS C:\Users\israe\Documents\GitHub\webapp> . 'C:\Users\israe\Documents\GitHub\webapp\Copy-FilesContent.ps1'
Skipping file: C:\Users\israe\Documents\GitHub\webapp\next.config.js
Skipping file: C:\Users\israe\Documents\GitHub\webapp\.vscode\settings.json
Skipping file: C:\Users\israe\Documents\GitHub\webapp\apps\server\fb3c7a85fc543a9224a6ad03c093624efc9294423178697797b38963f82a7358.tar.gz
Skipping file: C:\Users\israe\Documents\GitHub\webapp\apps\server\data\mailpit.db
Skipping file: C:\Users\israe\Documents\GitHub\webapp\apps\server\data\mailpit.db-shm
Skipping file: C:\Users\israe\Documents\GitHub\webapp\apps\server\data\mailpit.db-wal
Skipping file: C:\Users\israe\Documents\GitHub\webapp\apps\web\.next
Skipping file: C:\Users\israe\Documents\GitHub\webapp\apps\web\public\favicon.ico
Get-Content : No existe ningún objeto en la ruta de acceso especificada C:\Users\israe\Documents\GitHub\webapp\apps\web\src\app\(non-authenticated)\reset-password\[token]\page.tsx, o bien se 
ha filtrado por el parámetro -Include o -Exclude.
En C:\Users\israe\Documents\GitHub\webapp\Copy-FilesContent.ps1: 71 Carácter: 32
+                 $fileContent = Get-Content -Path $file | Where-Object ...
+                                ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (System.String[]:String[]) [Get-Content], Exception
    + FullyQualifiedErrorId : ItemNotFound,Microsoft.PowerShell.Commands.GetContentCommand
 
Get-Content : No existe ningún objeto en la ruta de acceso especificada 
C:\Users\israe\Documents\GitHub\webapp\apps\web\src\app\(non-authenticated)\reset-password\[token]\components\ErrorAlert\index.tsx, o bien se ha filtrado por el parámetro -Include o -Exclude.   
En C:\Users\israe\Documents\GitHub\webapp\Copy-FilesContent.ps1: 71 Carácter: 32
+                 $fileContent = Get-Content -Path $file | Where-Object ...
+                                ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (System.String[]:String[]) [Get-Content], Exception
    + FullyQualifiedErrorId : ItemNotFound,Microsoft.PowerShell.Commands.GetContentCommand
 
Get-Content : No existe ningún objeto en la ruta de acceso especificada 
C:\Users\israe\Documents\GitHub\webapp\apps\web\src\app\(non-authenticated)\reset-password\[token]\components\form\index.tsx, o bien se ha filtrado por el parámetro -Include o -Exclude.
En C:\Users\israe\Documents\GitHub\webapp\Copy-FilesContent.ps1: 71 Carácter: 32
+                 $fileContent = Get-Content -Path $file | Where-Object ...
+                                ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (System.String[]:String[]) [Get-Content], Exception
    + FullyQualifiedErrorId : ItemNotFound,Microsoft.PowerShell.Commands.GetContentCommand
 
Get-Content : No existe ningún objeto en la ruta de acceso especificada 
C:\Users\israe\Documents\GitHub\webapp\apps\web\src\app\(non-authenticated)\reset-password\[token]\components\messageSuccess\index.tsx, o bien se ha filtrado por el parámetro -Include o         
-Exclude.
En C:\Users\israe\Documents\GitHub\webapp\Copy-FilesContent.ps1: 71 Carácter: 32
+                 $fileContent = Get-Content -Path $file | Where-Object ...
+                                ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (System.String[]:String[]) [Get-Content], Exception
    + FullyQualifiedErrorId : ItemNotFound,Microsoft.PowerShell.Commands.GetContentCommand