using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace CMSMicroservice.Infrastructure.Persistence.Migrations
{
    /// <inheritdoc />
    public partial class Q27_HistoryTables_And_RenameWalletHistory : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.RenameTable(
                name: "UserWalletChangeLogs",
                schema: "CMS",
                newName: "UserWalletHistories",
                newSchema: "CMS");

            migrationBuilder.RenameIndex(
                name: "IX_UserWalletChangeLogs_PackageId",
                schema: "CMS",
                table: "UserWalletHistories",
                newName: "IX_UserWalletHistories_PackageId");

            migrationBuilder.RenameIndex(
                name: "IX_UserWalletChangeLogs_WalletId",
                schema: "CMS",
                table: "UserWalletHistories",
                newName: "IX_UserWalletHistories_WalletId");

            migrationBuilder.Sql(
                "EXEC sp_rename N'CMS.PK_UserWalletChangeLogs', N'PK_UserWalletHistories', N'OBJECT'");

            migrationBuilder.Sql(
                "EXEC sp_rename N'CMS.FK_UserWalletChangeLogs_Packages_PackageId', N'FK_UserWalletHistories_Packages_PackageId', N'OBJECT'");

            migrationBuilder.Sql(
                "EXEC sp_rename N'CMS.FK_UserWalletChangeLogs_UserWallets_WalletId', N'FK_UserWalletHistories_UserWallets_WalletId', N'OBJECT'");

            migrationBuilder.CreateTable(
                name: "ClubMembershipCycleHistories",
                schema: "CMS",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    ClubMembershipCycleId = table.Column<long>(type: "bigint", nullable: false),
                    UserId = table.Column<long>(type: "bigint", nullable: false),
                    CycleNumber = table.Column<int>(type: "int", nullable: false),
                    OldIsCurrentCycle = table.Column<bool>(type: "bit", nullable: false),
                    NewIsCurrentCycle = table.Column<bool>(type: "bit", nullable: false),
                    OldMagicStartedAt = table.Column<DateTime>(type: "datetime2", nullable: true),
                    NewMagicStartedAt = table.Column<DateTime>(type: "datetime2", nullable: true),
                    OldMagicCompletedAt = table.Column<DateTime>(type: "datetime2", nullable: true),
                    NewMagicCompletedAt = table.Column<DateTime>(type: "datetime2", nullable: true),
                    Action = table.Column<int>(type: "int", nullable: false),
                    PerformedBy = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: true),
                    Reason = table.Column<string>(type: "nvarchar(500)", maxLength: 500, nullable: true),
                    Created = table.Column<DateTime>(type: "datetime2", nullable: false),
                    CreatedBy = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    LastModified = table.Column<DateTime>(type: "datetime2", nullable: true),
                    LastModifiedBy = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    IsDeleted = table.Column<bool>(type: "bit", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ClubMembershipCycleHistories", x => x.Id);
                    table.ForeignKey(
                        name: "FK_ClubMembershipCycleHistories_ClubMembershipCycles_ClubMembershipCycleId",
                        column: x => x.ClubMembershipCycleId,
                        principalSchema: "CMS",
                        principalTable: "ClubMembershipCycles",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "PackageHistories",
                schema: "CMS",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    PackageId = table.Column<long>(type: "bigint", nullable: false),
                    OldPrice = table.Column<long>(type: "bigint", nullable: true),
                    NewPrice = table.Column<long>(type: "bigint", nullable: true),
                    OldActivationFee = table.Column<long>(type: "bigint", nullable: true),
                    NewActivationFee = table.Column<long>(type: "bigint", nullable: true),
                    OldMagicMultiplier = table.Column<decimal>(type: "decimal(18,4)", precision: 18, scale: 4, nullable: true),
                    NewMagicMultiplier = table.Column<decimal>(type: "decimal(18,4)", precision: 18, scale: 4, nullable: true),
                    OldMagicMaxDeposit = table.Column<long>(type: "bigint", nullable: true),
                    NewMagicMaxDeposit = table.Column<long>(type: "bigint", nullable: true),
                    OldMaxBalancesPerLeg = table.Column<int>(type: "int", nullable: true),
                    NewMaxBalancesPerLeg = table.Column<int>(type: "int", nullable: true),
                    OldIsActive = table.Column<bool>(type: "bit", nullable: true),
                    NewIsActive = table.Column<bool>(type: "bit", nullable: true),
                    Action = table.Column<int>(type: "int", nullable: false),
                    PerformedBy = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: true),
                    Reason = table.Column<string>(type: "nvarchar(500)", maxLength: 500, nullable: true),
                    Created = table.Column<DateTime>(type: "datetime2", nullable: false),
                    CreatedBy = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    LastModified = table.Column<DateTime>(type: "datetime2", nullable: true),
                    LastModifiedBy = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    IsDeleted = table.Column<bool>(type: "bit", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_PackageHistories", x => x.Id);
                    table.ForeignKey(
                        name: "FK_PackageHistories_Packages_PackageId",
                        column: x => x.PackageId,
                        principalSchema: "CMS",
                        principalTable: "Packages",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateIndex(
                name: "IX_ClubMembershipCycleHistory_Action",
                schema: "CMS",
                table: "ClubMembershipCycleHistories",
                column: "Action");

            migrationBuilder.CreateIndex(
                name: "IX_ClubMembershipCycleHistory_CycleId",
                schema: "CMS",
                table: "ClubMembershipCycleHistories",
                column: "ClubMembershipCycleId");

            migrationBuilder.CreateIndex(
                name: "IX_ClubMembershipCycleHistory_UserId_Created",
                schema: "CMS",
                table: "ClubMembershipCycleHistories",
                columns: new[] { "UserId", "Created" });

            migrationBuilder.CreateIndex(
                name: "IX_PackageHistory_Action",
                schema: "CMS",
                table: "PackageHistories",
                column: "Action");

            migrationBuilder.CreateIndex(
                name: "IX_PackageHistory_PackageId_Created",
                schema: "CMS",
                table: "PackageHistories",
                columns: new[] { "PackageId", "Created" });

        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "ClubMembershipCycleHistories",
                schema: "CMS");

            migrationBuilder.DropTable(
                name: "PackageHistories",
                schema: "CMS");

            migrationBuilder.RenameTable(
                name: "UserWalletHistories",
                schema: "CMS",
                newName: "UserWalletChangeLogs",
                newSchema: "CMS");

            migrationBuilder.RenameIndex(
                name: "IX_UserWalletHistories_PackageId",
                schema: "CMS",
                table: "UserWalletChangeLogs",
                newName: "IX_UserWalletChangeLogs_PackageId");

            migrationBuilder.RenameIndex(
                name: "IX_UserWalletHistories_WalletId",
                schema: "CMS",
                table: "UserWalletChangeLogs",
                newName: "IX_UserWalletChangeLogs_WalletId");

            migrationBuilder.Sql(
                "EXEC sp_rename N'CMS.PK_UserWalletHistories', N'PK_UserWalletChangeLogs', N'OBJECT'");

            migrationBuilder.Sql(
                "EXEC sp_rename N'CMS.FK_UserWalletHistories_Packages_PackageId', N'FK_UserWalletChangeLogs_Packages_PackageId', N'OBJECT'");

            migrationBuilder.Sql(
                "EXEC sp_rename N'CMS.FK_UserWalletHistories_UserWallets_WalletId', N'FK_UserWalletChangeLogs_UserWallets_WalletId', N'OBJECT'");
        }
    }
}
