# PowerShell script to rebuild the Jellyfin solution properly
# This script cleans and rebuilds in the correct order to resolve CS0006 metadata errors

Write-Host "Starting Jellyfin solution rebuild..." -ForegroundColor Cyan

# Step 1: Clean the solution
Write-Host "`n[1/3] Cleaning solution..." -ForegroundColor Yellow
dotnet clean Jellyfin.sln --configuration Debug

# Remove obj and bin directories to ensure clean state
Write-Host "`n[2/3] Removing build artifacts..." -ForegroundColor Yellow
Get-ChildItem -Path . -Include bin,obj -Recurse -Directory | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue

# Step 3: Restore NuGet packages
Write-Host "`n[3/3] Restoring NuGet packages..." -ForegroundColor Yellow
dotnet restore Jellyfin.sln

# Step 4: Build the solution
Write-Host "`n[4/5] Building solution..." -ForegroundColor Yellow
dotnet build Jellyfin.sln --configuration Debug --no-restore

# Step 5: Publish the solution for SelfContained-Linux-x64
Write-Host "`n[5/5] Publishing solution for Linux-x64..." -ForegroundColor Yellow
dotnet publish .\Jellyfin.Server\Jellyfin.Server.csproj /p:PublishProfile=SelfContained-Linux-x64

Write-Host "`nBuild and publish complete!" -ForegroundColor Green
Write-Host "If you still see errors, try building specific projects in this order:" -ForegroundColor Cyan
Write-Host "  1. Jellyfin.Database.Implementations" -ForegroundColor Gray
Write-Host "  2. Jellyfin.Database.Providers.Postgres" -ForegroundColor Gray
Write-Host "  3. Jellyfin.Server.Implementations" -ForegroundColor Gray
Write-Host "  4. Emby.Server.Implementations" -ForegroundColor Gray
Write-Host "  5. Jellyfin.Server" -ForegroundColor Gray
