using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Migrations
{
    /// <summary>
    /// SMS code-review (CentralUI-NNN / UI-Med-2): makes <c>SmsConfigurations.FromNumber</c>
    /// optional. A Twilio Messaging-Service-only config supplies a MessagingServiceSid instead
    /// of a From number; the design doc, the delivery adapter, and now the UI/CLI treat the two
    /// as either-or, so the column must allow NULL.
    ///
    /// ALTER COLUMN to NULL is naturally idempotent (re-running re-applies the same shape).
    /// </summary>
    public partial class SmsFromNumberOptional : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.Sql("ALTER TABLE [SmsConfigurations] ALTER COLUMN [FromNumber] nvarchar(32) NULL;");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            // Reversing to NOT NULL requires backfilling NULLs first. Unlike the recipient
            // EmailAddress backfill, '' is a harmless reversal here: a Messaging-Service-only
            // config keeps its MessagingServiceSid and stays deliverable, so no data is lost.
            migrationBuilder.Sql("UPDATE [SmsConfigurations] SET [FromNumber] = '' WHERE [FromNumber] IS NULL;");
            migrationBuilder.Sql("ALTER TABLE [SmsConfigurations] ALTER COLUMN [FromNumber] nvarchar(32) NOT NULL;");
        }
    }
}
