using Microsoft.EntityFrameworkCore;

namespace Meezi.Infrastructure.Data;

/// <summary>Idempotent SQL fixes for databases where EF history and schema diverged.</summary>
public static class DatabaseSchemaPatches
{
    public static async Task ApplyAsync(AppDbContext db, CancellationToken cancellationToken = default)
    {
        await db.Database.ExecuteSqlRawAsync(
            """
            ALTER TABLE "OrderItems" ADD COLUMN IF NOT EXISTS "IsVoided" boolean NOT NULL DEFAULT false;
            ALTER TABLE "OrderItems" ADD COLUMN IF NOT EXISTS "VoidedAt" timestamp with time zone;
            ALTER TABLE "OrderItems" ADD COLUMN IF NOT EXISTS "VoidedByUserId" text;
            """,
            cancellationToken);
    }
}
