using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace CMSMicroservice.Infrastructure.Persistence.Migrations
{
    /// <inheritdoc />
    public partial class AddNetworkClubSystemV2 : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<long>(
                name: "DiscountBalance",
                schema: "CMS",
                table: "UserWallets",
                type: "bigint",
                nullable: false,
                defaultValue: 0L);

            migrationBuilder.AddColumn<int>(
                name: "LegPosition",
                schema: "CMS",
                table: "Users",
                type: "int",
                nullable: true);

            migrationBuilder.AddColumn<long>(
                name: "NetworkParentId",
                schema: "CMS",
                table: "Users",
                type: "bigint",
                nullable: true);

            migrationBuilder.AddColumn<int>(
                name: "ClubDiscountPercent",
                schema: "CMS",
                table: "Productss",
                type: "int",
                nullable: false,
                defaultValue: 0);

            migrationBuilder.AddColumn<bool>(
                name: "IsClubExclusive",
                schema: "CMS",
                table: "Productss",
                type: "bit",
                nullable: false,
                defaultValue: false);

            migrationBuilder.CreateTable(
                name: "ClubFeatures",
                schema: "CMS",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    Title = table.Column<string>(type: "nvarchar(200)", maxLength: 200, nullable: false),
                    Description = table.Column<string>(type: "nvarchar(1000)", maxLength: 1000, nullable: true),
                    IsActive = table.Column<bool>(type: "bit", nullable: false),
                    RequiredPoints = table.Column<int>(type: "int", nullable: true),
                    SortOrder = table.Column<int>(type: "int", nullable: false),
                    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_ClubFeatures", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "ClubMemberships",
                schema: "CMS",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    UserId = table.Column<long>(type: "bigint", nullable: false),
                    IsActive = table.Column<bool>(type: "bit", nullable: false),
                    ActivatedAt = table.Column<DateTime>(type: "datetime2", nullable: true),
                    InitialContribution = table.Column<long>(type: "bigint", nullable: false),
                    TotalEarned = table.Column<long>(type: "bigint", nullable: false),
                    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_ClubMemberships", x => x.Id);
                    table.ForeignKey(
                        name: "FK_ClubMemberships_Users_UserId",
                        column: x => x.UserId,
                        principalSchema: "CMS",
                        principalTable: "Users",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "NetworkMembershipHistories",
                schema: "CMS",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    UserId = table.Column<long>(type: "bigint", nullable: false),
                    OldParentId = table.Column<long>(type: "bigint", nullable: true),
                    NewParentId = table.Column<long>(type: "bigint", nullable: true),
                    OldLegPosition = table.Column<int>(type: "int", nullable: true),
                    NewLegPosition = table.Column<int>(type: "int", nullable: true),
                    Action = table.Column<int>(type: "int", nullable: false),
                    Reason = table.Column<string>(type: "nvarchar(500)", maxLength: 500, nullable: true),
                    PerformedBy = table.Column<string>(type: "nvarchar(100)", maxLength: 100, 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_NetworkMembershipHistories", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "NetworkWeeklyBalances",
                schema: "CMS",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    UserId = table.Column<long>(type: "bigint", nullable: false),
                    WeekNumber = table.Column<string>(type: "nvarchar(20)", maxLength: 20, nullable: false),
                    LeftLegBalances = table.Column<int>(type: "int", nullable: false),
                    RightLegBalances = table.Column<int>(type: "int", nullable: false),
                    TotalBalances = table.Column<int>(type: "int", nullable: false),
                    WeeklyPoolContribution = table.Column<long>(type: "bigint", nullable: false),
                    CalculatedAt = table.Column<DateTime>(type: "datetime2", nullable: true),
                    IsExpired = table.Column<bool>(type: "bit", nullable: false),
                    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_NetworkWeeklyBalances", x => x.Id);
                    table.ForeignKey(
                        name: "FK_NetworkWeeklyBalances_Users_UserId",
                        column: x => x.UserId,
                        principalSchema: "CMS",
                        principalTable: "Users",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "SystemConfigurations",
                schema: "CMS",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    Scope = table.Column<int>(type: "int", nullable: false),
                    Key = table.Column<string>(type: "nvarchar(200)", maxLength: 200, nullable: false),
                    Value = table.Column<string>(type: "nvarchar(1000)", maxLength: 1000, nullable: false),
                    DataType = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: true),
                    Description = table.Column<string>(type: "nvarchar(500)", maxLength: 500, nullable: true),
                    IsActive = table.Column<bool>(type: "bit", nullable: false),
                    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_SystemConfigurations", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "WeeklyCommissionPools",
                schema: "CMS",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    WeekNumber = table.Column<string>(type: "nvarchar(20)", maxLength: 20, nullable: false),
                    TotalPoolAmount = table.Column<long>(type: "bigint", nullable: false),
                    TotalBalances = table.Column<int>(type: "int", nullable: false),
                    ValuePerBalance = table.Column<long>(type: "bigint", nullable: false),
                    IsCalculated = table.Column<bool>(type: "bit", nullable: false),
                    CalculatedAt = table.Column<DateTime>(type: "datetime2", 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_WeeklyCommissionPools", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "ClubMembershipHistories",
                schema: "CMS",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    ClubMembershipId = table.Column<long>(type: "bigint", nullable: false),
                    UserId = table.Column<long>(type: "bigint", nullable: false),
                    OldIsActive = table.Column<bool>(type: "bit", nullable: false),
                    NewIsActive = table.Column<bool>(type: "bit", nullable: false),
                    OldInitialContribution = table.Column<long>(type: "bigint", nullable: true),
                    NewInitialContribution = table.Column<long>(type: "bigint", nullable: true),
                    Action = table.Column<int>(type: "int", nullable: false),
                    Reason = table.Column<string>(type: "nvarchar(500)", maxLength: 500, nullable: true),
                    PerformedBy = table.Column<string>(type: "nvarchar(100)", maxLength: 100, 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_ClubMembershipHistories", x => x.Id);
                    table.ForeignKey(
                        name: "FK_ClubMembershipHistories_ClubMemberships_ClubMembershipId",
                        column: x => x.ClubMembershipId,
                        principalSchema: "CMS",
                        principalTable: "ClubMemberships",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "UserClubFeatures",
                schema: "CMS",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    UserId = table.Column<long>(type: "bigint", nullable: false),
                    ClubMembershipId = table.Column<long>(type: "bigint", nullable: false),
                    ClubFeatureId = table.Column<long>(type: "bigint", nullable: false),
                    GrantedAt = table.Column<DateTime>(type: "datetime2", nullable: false),
                    Notes = 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_UserClubFeatures", x => x.Id);
                    table.ForeignKey(
                        name: "FK_UserClubFeatures_ClubFeatures_ClubFeatureId",
                        column: x => x.ClubFeatureId,
                        principalSchema: "CMS",
                        principalTable: "ClubFeatures",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_UserClubFeatures_ClubMemberships_ClubMembershipId",
                        column: x => x.ClubMembershipId,
                        principalSchema: "CMS",
                        principalTable: "ClubMemberships",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_UserClubFeatures_Users_UserId",
                        column: x => x.UserId,
                        principalSchema: "CMS",
                        principalTable: "Users",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "SystemConfigurationHistories",
                schema: "CMS",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    ConfigurationId = table.Column<long>(type: "bigint", nullable: false),
                    Scope = table.Column<int>(type: "int", nullable: false),
                    Key = table.Column<string>(type: "nvarchar(200)", maxLength: 200, nullable: false),
                    OldValue = table.Column<string>(type: "nvarchar(1000)", maxLength: 1000, nullable: false),
                    NewValue = table.Column<string>(type: "nvarchar(1000)", maxLength: 1000, nullable: false),
                    Reason = table.Column<string>(type: "nvarchar(500)", maxLength: 500, nullable: true),
                    PerformedBy = table.Column<string>(type: "nvarchar(100)", maxLength: 100, 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_SystemConfigurationHistories", x => x.Id);
                    table.ForeignKey(
                        name: "FK_SystemConfigurationHistories_SystemConfigurations_ConfigurationId",
                        column: x => x.ConfigurationId,
                        principalSchema: "CMS",
                        principalTable: "SystemConfigurations",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "UserCommissionPayouts",
                schema: "CMS",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    UserId = table.Column<long>(type: "bigint", nullable: false),
                    WeekNumber = table.Column<string>(type: "nvarchar(20)", maxLength: 20, nullable: false),
                    WeeklyPoolId = table.Column<long>(type: "bigint", nullable: false),
                    BalancesEarned = table.Column<int>(type: "int", nullable: false),
                    ValuePerBalance = table.Column<long>(type: "bigint", nullable: false),
                    TotalAmount = table.Column<long>(type: "bigint", nullable: false),
                    Status = table.Column<int>(type: "int", nullable: false),
                    PaidAt = table.Column<DateTime>(type: "datetime2", nullable: true),
                    WithdrawalMethod = table.Column<int>(type: "int", nullable: true),
                    IbanNumber = table.Column<string>(type: "nvarchar(26)", maxLength: 26, nullable: true),
                    WithdrawnAt = table.Column<DateTime>(type: "datetime2", 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_UserCommissionPayouts", x => x.Id);
                    table.ForeignKey(
                        name: "FK_UserCommissionPayouts_Users_UserId",
                        column: x => x.UserId,
                        principalSchema: "CMS",
                        principalTable: "Users",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_UserCommissionPayouts_WeeklyCommissionPools_WeeklyPoolId",
                        column: x => x.WeeklyPoolId,
                        principalSchema: "CMS",
                        principalTable: "WeeklyCommissionPools",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "CommissionPayoutHistories",
                schema: "CMS",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    UserCommissionPayoutId = table.Column<long>(type: "bigint", nullable: false),
                    UserId = table.Column<long>(type: "bigint", nullable: false),
                    WeekNumber = table.Column<string>(type: "nvarchar(20)", maxLength: 20, nullable: false),
                    AmountBefore = table.Column<long>(type: "bigint", nullable: false),
                    AmountAfter = table.Column<long>(type: "bigint", nullable: false),
                    OldStatus = table.Column<int>(type: "int", nullable: false),
                    NewStatus = table.Column<int>(type: "int", nullable: false),
                    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_CommissionPayoutHistories", x => x.Id);
                    table.ForeignKey(
                        name: "FK_CommissionPayoutHistories_UserCommissionPayouts_UserCommissionPayoutId",
                        column: x => x.UserCommissionPayoutId,
                        principalSchema: "CMS",
                        principalTable: "UserCommissionPayouts",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateIndex(
                name: "IX_User_LegPosition",
                schema: "CMS",
                table: "Users",
                column: "LegPosition");

            migrationBuilder.CreateIndex(
                name: "IX_User_NetworkParentId",
                schema: "CMS",
                table: "Users",
                column: "NetworkParentId");

            migrationBuilder.CreateIndex(
                name: "IX_Products_IsClubExclusive",
                schema: "CMS",
                table: "Productss",
                column: "IsClubExclusive");

            migrationBuilder.CreateIndex(
                name: "IX_ClubFeature_IsActive_SortOrder",
                schema: "CMS",
                table: "ClubFeatures",
                columns: new[] { "IsActive", "SortOrder" });

            migrationBuilder.CreateIndex(
                name: "IX_ClubMembershipHistory_Action",
                schema: "CMS",
                table: "ClubMembershipHistories",
                column: "Action");

            migrationBuilder.CreateIndex(
                name: "IX_ClubMembershipHistory_ClubMembershipId",
                schema: "CMS",
                table: "ClubMembershipHistories",
                column: "ClubMembershipId");

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

            migrationBuilder.CreateIndex(
                name: "IX_ClubMembership_IsActive",
                schema: "CMS",
                table: "ClubMemberships",
                column: "IsActive");

            migrationBuilder.CreateIndex(
                name: "IX_ClubMembership_UserId",
                schema: "CMS",
                table: "ClubMemberships",
                column: "UserId",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_CommissionPayoutHistory_Action",
                schema: "CMS",
                table: "CommissionPayoutHistories",
                column: "Action");

            migrationBuilder.CreateIndex(
                name: "IX_CommissionPayoutHistory_PayoutId",
                schema: "CMS",
                table: "CommissionPayoutHistories",
                column: "UserCommissionPayoutId");

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

            migrationBuilder.CreateIndex(
                name: "IX_CommissionPayoutHistory_WeekNumber",
                schema: "CMS",
                table: "CommissionPayoutHistories",
                column: "WeekNumber");

            migrationBuilder.CreateIndex(
                name: "IX_NetworkMembershipHistory_Action",
                schema: "CMS",
                table: "NetworkMembershipHistories",
                column: "Action");

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

            migrationBuilder.CreateIndex(
                name: "IX_NetworkWeeklyBalance_IsExpired",
                schema: "CMS",
                table: "NetworkWeeklyBalances",
                column: "IsExpired");

            migrationBuilder.CreateIndex(
                name: "IX_NetworkWeeklyBalance_UserId_WeekNumber",
                schema: "CMS",
                table: "NetworkWeeklyBalances",
                columns: new[] { "UserId", "WeekNumber" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_NetworkWeeklyBalance_WeekNumber",
                schema: "CMS",
                table: "NetworkWeeklyBalances",
                column: "WeekNumber");

            migrationBuilder.CreateIndex(
                name: "IX_SystemConfigurationHistory_ConfigId_Created",
                schema: "CMS",
                table: "SystemConfigurationHistories",
                columns: new[] { "ConfigurationId", "Created" });

            migrationBuilder.CreateIndex(
                name: "IX_SystemConfigurationHistory_Scope_Key",
                schema: "CMS",
                table: "SystemConfigurationHistories",
                columns: new[] { "Scope", "Key" });

            migrationBuilder.CreateIndex(
                name: "IX_SystemConfiguration_IsActive",
                schema: "CMS",
                table: "SystemConfigurations",
                column: "IsActive");

            migrationBuilder.CreateIndex(
                name: "IX_SystemConfiguration_Scope_Key",
                schema: "CMS",
                table: "SystemConfigurations",
                columns: new[] { "Scope", "Key" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_UserClubFeature_ClubMembershipId",
                schema: "CMS",
                table: "UserClubFeatures",
                column: "ClubMembershipId");

            migrationBuilder.CreateIndex(
                name: "IX_UserClubFeature_UserId_ClubFeatureId",
                schema: "CMS",
                table: "UserClubFeatures",
                columns: new[] { "UserId", "ClubFeatureId" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_UserClubFeatures_ClubFeatureId",
                schema: "CMS",
                table: "UserClubFeatures",
                column: "ClubFeatureId");

            migrationBuilder.CreateIndex(
                name: "IX_UserCommissionPayout_Status",
                schema: "CMS",
                table: "UserCommissionPayouts",
                column: "Status");

            migrationBuilder.CreateIndex(
                name: "IX_UserCommissionPayout_UserId_WeekNumber",
                schema: "CMS",
                table: "UserCommissionPayouts",
                columns: new[] { "UserId", "WeekNumber" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_UserCommissionPayout_WeeklyPoolId",
                schema: "CMS",
                table: "UserCommissionPayouts",
                column: "WeeklyPoolId");

            migrationBuilder.CreateIndex(
                name: "IX_UserCommissionPayout_WeekNumber",
                schema: "CMS",
                table: "UserCommissionPayouts",
                column: "WeekNumber");

            migrationBuilder.CreateIndex(
                name: "IX_WeeklyCommissionPool_IsCalculated",
                schema: "CMS",
                table: "WeeklyCommissionPools",
                column: "IsCalculated");

            migrationBuilder.CreateIndex(
                name: "IX_WeeklyCommissionPool_WeekNumber",
                schema: "CMS",
                table: "WeeklyCommissionPools",
                column: "WeekNumber",
                unique: true);

            migrationBuilder.AddForeignKey(
                name: "FK_Users_Users_NetworkParentId",
                schema: "CMS",
                table: "Users",
                column: "NetworkParentId",
                principalSchema: "CMS",
                principalTable: "Users",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_Users_Users_NetworkParentId",
                schema: "CMS",
                table: "Users");

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

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

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

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

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

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

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

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

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

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

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

            migrationBuilder.DropIndex(
                name: "IX_User_LegPosition",
                schema: "CMS",
                table: "Users");

            migrationBuilder.DropIndex(
                name: "IX_User_NetworkParentId",
                schema: "CMS",
                table: "Users");

            migrationBuilder.DropIndex(
                name: "IX_Products_IsClubExclusive",
                schema: "CMS",
                table: "Productss");

            migrationBuilder.DropColumn(
                name: "DiscountBalance",
                schema: "CMS",
                table: "UserWallets");

            migrationBuilder.DropColumn(
                name: "LegPosition",
                schema: "CMS",
                table: "Users");

            migrationBuilder.DropColumn(
                name: "NetworkParentId",
                schema: "CMS",
                table: "Users");

            migrationBuilder.DropColumn(
                name: "ClubDiscountPercent",
                schema: "CMS",
                table: "Productss");

            migrationBuilder.DropColumn(
                name: "IsClubExclusive",
                schema: "CMS",
                table: "Productss");
        }
    }
}
