# PowerShell script to rename the project from iYHCT360 to a new name
# Usage: .\rename-project.ps1 -NewName "NewProjectName"

param(
    [Parameter(Mandatory=$true)]
    [string]$NewName
)

$OldName = "iYHCT360"

Write-Host "🔄 Renaming project from '$OldName' to '$NewName'..." -ForegroundColor Cyan

# Clean build artifacts first
Write-Host "🧹 Cleaning build artifacts..." -ForegroundColor Yellow
Get-ChildItem -Path . -Include bin,obj -Recurse -Directory | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue

# Replace content in files
Write-Host "📝 Replacing content in files..." -ForegroundColor Yellow
$extensions = @("*.cs", "*.csproj", "*.sln", "*.json", "*.xml", "*.md", "*.props", "*.targets", "*.http")
$excludePaths = @("*\bin\*", "*\obj\*", "*\.git\*")

foreach ($ext in $extensions) {
    Get-ChildItem -Path . -Filter $ext -Recurse -File | 
    Where-Object { 
        $path = $_.FullName
        -not ($excludePaths | Where-Object { $path -like $_ })
    } |
    ForEach-Object {
        $content = Get-Content $_.FullName -Raw -ErrorAction SilentlyContinue
        if ($content -and $content -match $OldName) {
            Write-Host "  Updating: $($_.FullName)" -ForegroundColor Gray
            $newContent = $content -replace $OldName, $NewName
            Set-Content -Path $_.FullName -Value $newContent -NoNewline
        }
    }
}

# Rename files
Write-Host "📁 Renaming files..." -ForegroundColor Yellow
Get-ChildItem -Path . -Recurse -File | 
Where-Object { $_.Name -match $OldName } |
ForEach-Object {
    $newFileName = $_.Name -replace $OldName, $NewName
    $newPath = Join-Path $_.Directory.FullName $newFileName
    Write-Host "  Renaming: $($_.Name) -> $newFileName" -ForegroundColor Gray
    Rename-Item -Path $_.FullName -NewName $newFileName
}

# Rename directories (from deepest to shallowest)
Write-Host "📁 Renaming directories..." -ForegroundColor Yellow
Get-ChildItem -Path . -Recurse -Directory | 
Where-Object { $_.Name -match $OldName } |
Sort-Object { $_.FullName.Length } -Descending |
ForEach-Object {
    $newDirName = $_.Name -replace $OldName, $NewName
    Write-Host "  Renaming: $($_.Name) -> $newDirName" -ForegroundColor Gray
    Rename-Item -Path $_.FullName -NewName $newDirName
}

Write-Host ""
Write-Host "  Project renamed successfully from '$OldName' to '$NewName'!" -ForegroundColor Green
Write-Host ""
Write-Host "Next steps:" -ForegroundColor Cyan
Write-Host "  1. Review the changes"
Write-Host "  2. Update your connection string in appsettings.json"
Write-Host "  3. Run 'dotnet restore'"
Write-Host "  4. Run 'dotnet build'"
