using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace DeepDrftData.Migrations
{
    /// <inheritdoc />
    // Data-only migration: no schema change, snapshot unchanged.
    public partial class SoftDeleteOrphanedReleases : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            // Backfill: soft-delete any live release whose tracks were all soft-deleted before the
            // delete-cascade in UnifiedTrackService existed. These show as 0-track rows in the albums
            // browser; this clears the pre-existing orphans the cascade now prevents going forward.
            migrationBuilder.Sql(@"
UPDATE release
SET is_deleted = true,
    updated_at = now()
WHERE id IN (
    SELECT r.id
    FROM release r
    WHERE r.is_deleted = false
      AND NOT EXISTS (
          SELECT 1
          FROM track t
          WHERE t.release_id = r.id
            AND t.is_deleted = false
      )
);");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.Sql("-- no-op: orphaned release soft-deletes are not rolled back");
        }
    }
}
