using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Migrations
{
    /// <summary>
    /// Task 1.7 — canonicalizes ScadaBridge role VALUES onto the canonical six
    /// (only Administrator/Designer/Deployer/Viewer are used here) and collapses
    /// the former audit roles:
    /// <list type="bullet">
    ///   <item><description>Admin → Administrator</description></item>
    ///   <item><description>Design → Designer</description></item>
    ///   <item><description>Deployment → Deployer</description></item>
    ///   <item><description>Audit → Administrator (COLLAPSE — accepted SoD loss /
    ///   privilege escalation)</description></item>
    ///   <item><description>AuditReadOnly → Viewer (COLLAPSE — keeps audit-read,
    ///   never had export)</description></item>
    /// </list>
    /// The <c>UpdateData</c> calls below canonicalize the four seeded rows
    /// (Id 1-4). The raw <c>Sql</c> catch-alls then canonicalize ANY
    /// operator-added (non-seed) rows whose <c>Role</c> still holds a legacy
    /// value — <c>LdapGroupMappings.Role</c> is free-text nvarchar, so operators
    /// may have created rows with Admin/Design/Deployment/Audit/AuditReadOnly.
    /// The catch-alls are value-keyed (WHERE Role IN (...)), so they are
    /// idempotent and safe whether or not the seed <c>UpdateData</c> already ran
    /// (after the seed update the seed rows no longer match a legacy value, so
    /// they are simply skipped).
    /// </summary>
    /// <remarks>
    /// LOSSY DOWN: the Audit/AuditReadOnly → Administrator/Viewer collapse is not
    /// perfectly reversible. Down is a best-effort that maps Administrator→Admin
    /// and Viewer→AuditReadOnly. It CANNOT recover the original Audit vs Admin
    /// distinction (both became Administrator) nor distinguish a genuine Viewer
    /// from a former AuditReadOnly (both became Viewer). The four seeded rows are
    /// restored exactly by the Id-keyed <c>UpdateData</c> calls; operator rows
    /// are reverted by the value-keyed catch-alls under this caveat.
    /// </remarks>
    public partial class CanonicalizeRoles : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            // 1) Canonicalize the four seeded rows by Id (EF-scaffolded).
            migrationBuilder.UpdateData(
                table: "LdapGroupMappings",
                keyColumn: "Id",
                keyValue: 1,
                column: "Role",
                value: "Administrator");

            migrationBuilder.UpdateData(
                table: "LdapGroupMappings",
                keyColumn: "Id",
                keyValue: 2,
                column: "Role",
                value: "Designer");

            migrationBuilder.UpdateData(
                table: "LdapGroupMappings",
                keyColumn: "Id",
                keyValue: 3,
                column: "Role",
                value: "Deployer");

            migrationBuilder.UpdateData(
                table: "LdapGroupMappings",
                keyColumn: "Id",
                keyValue: 4,
                column: "Role",
                value: "Deployer");

            // 2) Catch-all for operator-added (non-seed) rows. Value-keyed and
            //    idempotent. Audit collapses INTO Administrator; AuditReadOnly
            //    collapses INTO Viewer.
            migrationBuilder.Sql(
                "UPDATE [LdapGroupMappings] SET [Role] = N'Administrator' WHERE [Role] IN (N'Admin', N'Audit');");
            migrationBuilder.Sql(
                "UPDATE [LdapGroupMappings] SET [Role] = N'Designer' WHERE [Role] = N'Design';");
            migrationBuilder.Sql(
                "UPDATE [LdapGroupMappings] SET [Role] = N'Deployer' WHERE [Role] = N'Deployment';");
            migrationBuilder.Sql(
                "UPDATE [LdapGroupMappings] SET [Role] = N'Viewer' WHERE [Role] = N'AuditReadOnly';");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            // Restore the four seeded rows exactly (Id-keyed). Down is lossy for
            // the collapsed audit roles — see the class <remarks>.
            migrationBuilder.UpdateData(
                table: "LdapGroupMappings",
                keyColumn: "Id",
                keyValue: 1,
                column: "Role",
                value: "Admin");

            migrationBuilder.UpdateData(
                table: "LdapGroupMappings",
                keyColumn: "Id",
                keyValue: 2,
                column: "Role",
                value: "Design");

            migrationBuilder.UpdateData(
                table: "LdapGroupMappings",
                keyColumn: "Id",
                keyValue: 3,
                column: "Role",
                value: "Deployment");

            migrationBuilder.UpdateData(
                table: "LdapGroupMappings",
                keyColumn: "Id",
                keyValue: 4,
                column: "Role",
                value: "Deployment");

            // Best-effort reverse for operator-added (non-seed) rows. LOSSY:
            // Administrator→Admin and Viewer→AuditReadOnly cannot recover the
            // original Audit/Admin or Viewer/AuditReadOnly distinction (the
            // forward collapse merged those). Value-keyed and idempotent.
            migrationBuilder.Sql(
                "UPDATE [LdapGroupMappings] SET [Role] = N'Admin' WHERE [Role] = N'Administrator';");
            migrationBuilder.Sql(
                "UPDATE [LdapGroupMappings] SET [Role] = N'Design' WHERE [Role] = N'Designer';");
            migrationBuilder.Sql(
                "UPDATE [LdapGroupMappings] SET [Role] = N'Deployment' WHERE [Role] = N'Deployer';");
            migrationBuilder.Sql(
                "UPDATE [LdapGroupMappings] SET [Role] = N'AuditReadOnly' WHERE [Role] = N'Viewer';");
        }
    }
}
