# Rollback to .NET 10 Script
# This script reverts all .NET 11 changes back to .NET 10

Write-Host "Rolling back to .NET 10..." -ForegroundColor Yellow

# Revert all .csproj files from net11.0 to net10.0
$files = Get-ChildItem -Path . -Filter "*.csproj" -Recurse | Where-Object { 
    $_.FullName -notlike "*\obj\*" -and $_.FullName -notlike "*\bin\*" 
}

foreach ($file in $files) {
    $content = Get-Content $file.FullName -Raw
    if ($content -match '<TargetFramework>net11\.0</TargetFramework>') {
        $content = $content -replace '<TargetFramework>net11\.0</TargetFramework>', '<TargetFramework>net10.0</TargetFramework>'
        Set-Content -Path $file.FullName -Value $content -NoNewline
        Write-Host "Reverted: $($file.Name)" -ForegroundColor Green
    }
}

Write-Host "`nUpdating Directory.Packages.props..." -ForegroundColor Yellow

# Read Directory.Packages.props
$packagesFile = "Directory.Packages.props"
$content = Get-Content $packagesFile -Raw

# Revert Microsoft packages
$content = $content -replace 'Microsoft\.AspNetCore\.Authorization" Version="11\.0\.1"', 'Microsoft.AspNetCore.Authorization" Version="10.0.3"'
$content = $content -replace 'Microsoft\.AspNetCore\.Mvc\.Testing" Version="11\.0\.1"', 'Microsoft.AspNetCore.Mvc.Testing" Version="10.0.3"'
$content = $content -replace 'Microsoft\.Data\.Sqlite" Version="11\.0\.1"', 'Microsoft.Data.Sqlite" Version="10.0.3"'
$content = $content -replace 'Microsoft\.EntityFrameworkCore" Version="11\.0\.1"', 'Microsoft.EntityFrameworkCore" Version="10.0.3"'
$content = $content -replace 'Microsoft\.EntityFrameworkCore\.Design" Version="11\.0\.1"', 'Microsoft.EntityFrameworkCore.Design" Version="10.0.3"'
$content = $content -replace 'Microsoft\.EntityFrameworkCore\.Relational" Version="11\.0\.1"', 'Microsoft.EntityFrameworkCore.Relational" Version="10.0.3"'
$content = $content -replace 'Microsoft\.EntityFrameworkCore\.Sqlite" Version="11\.0\.1"', 'Microsoft.EntityFrameworkCore.Sqlite" Version="10.0.3"'
$content = $content -replace 'Microsoft\.EntityFrameworkCore\.Tools" Version="11\.0\.1"', 'Microsoft.EntityFrameworkCore.Tools" Version="10.0.3"'
$content = $content -replace 'Microsoft\.Extensions\.Caching\.Abstractions" Version="11\.0\.1"', 'Microsoft.Extensions.Caching.Abstractions" Version="10.0.3"'
$content = $content -replace 'Microsoft\.Extensions\.Caching\.Memory" Version="11\.0\.1"', 'Microsoft.Extensions.Caching.Memory" Version="10.0.3"'
$content = $content -replace 'Microsoft\.Extensions\.Configuration\.Abstractions" Version="11\.0\.1"', 'Microsoft.Extensions.Configuration.Abstractions" Version="10.0.3"'
$content = $content -replace 'Microsoft\.Extensions\.Configuration\.Binder" Version="11\.0\.1"', 'Microsoft.Extensions.Configuration.Binder" Version="10.0.3"'
$content = $content -replace 'Microsoft\.Extensions\.DependencyInjection" Version="11\.0\.1"', 'Microsoft.Extensions.DependencyInjection" Version="10.0.3"'
$content = $content -replace 'Microsoft\.Extensions\.Diagnostics\.HealthChecks\.EntityFrameworkCore" Version="11\.0\.1"', 'Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore" Version="10.0.3"'
$content = $content -replace 'Microsoft\.Extensions\.Hosting\.Abstractions" Version="11\.0\.1"', 'Microsoft.Extensions.Hosting.Abstractions" Version="10.0.3"'
$content = $content -replace 'Microsoft\.Extensions\.Http" Version="11\.0\.1"', 'Microsoft.Extensions.Http" Version="10.0.3"'
$content = $content -replace 'Microsoft\.Extensions\.Logging" Version="11\.0\.1"', 'Microsoft.Extensions.Logging" Version="10.0.3"'
$content = $content -replace 'Microsoft\.Extensions\.Options" Version="11\.0\.1"', 'Microsoft.Extensions.Options" Version="10.0.3"'

# Revert Serilog packages
$content = $content -replace 'Serilog\.AspNetCore" Version="11\.0\.1"', 'Serilog.AspNetCore" Version="10.0.0"'
$content = $content -replace 'Serilog\.Settings\.Configuration" Version="11\.0\.1"', 'Serilog.Settings.Configuration" Version="10.0.0"'

# Revert System packages
$content = $content -replace 'System\.Text\.Json" Version="11\.0\.1"', 'System.Text.Json" Version="10.0.3"'

# Revert Npgsql with comment
$content = $content -replace 'Npgsql\.EntityFrameworkCore\.PostgreSQL" Version="11\.0\.0-preview\.1"', 
    'Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.2" />' + "`n    <!-- Note: Using 9.0.2 with EF Core 10 - version constraint warnings expected"

Set-Content -Path $packagesFile -Value $content -NoNewline

Write-Host "Updated Directory.Packages.props" -ForegroundColor Green

Write-Host "`nRollback complete!" -ForegroundColor Cyan
Write-Host "Run these commands to restore and build:" -ForegroundColor Cyan
Write-Host "  dotnet restore Jellyfin.sln" -ForegroundColor White
Write-Host "  dotnet build Jellyfin.sln /p:TreatWarningsAsErrors=false" -ForegroundColor White
