using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Managing.Infrastructure.Databases.Migrations
{
    /// <inheritdoc />
    public partial class UserKeys : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropIndex(
                name: "IX_Signals_Identifier_Date_UserName",
                table: "Signals");

            migrationBuilder.DropIndex(
                name: "IX_Signals_UserName",
                table: "Signals");

            migrationBuilder.DropIndex(
                name: "IX_Signals_UserName_Date",
                table: "Signals");

            migrationBuilder.DropIndex(
                name: "IX_Scenarios_UserName",
                table: "Scenarios");

            migrationBuilder.DropIndex(
                name: "IX_Scenarios_UserName_Name",
                table: "Scenarios");

            migrationBuilder.DropIndex(
                name: "IX_Positions_UserName",
                table: "Positions");

            migrationBuilder.DropIndex(
                name: "IX_Positions_UserName_Identifier",
                table: "Positions");

            migrationBuilder.DropIndex(
                name: "IX_Indicators_UserName",
                table: "Indicators");

            migrationBuilder.DropIndex(
                name: "IX_Indicators_UserName_Name",
                table: "Indicators");

            migrationBuilder.DropIndex(
                name: "IX_BundleBacktestRequests_UserName",
                table: "BundleBacktestRequests");

            migrationBuilder.DropIndex(
                name: "IX_BundleBacktestRequests_UserName_CreatedAt",
                table: "BundleBacktestRequests");

            migrationBuilder.DropIndex(
                name: "IX_Backtests_UserName",
                table: "Backtests");

            migrationBuilder.DropIndex(
                name: "IX_Backtests_UserName_Score",
                table: "Backtests");

            migrationBuilder.DropColumn(
                name: "UserName",
                table: "Signals");

            migrationBuilder.DropColumn(
                name: "UserName",
                table: "Scenarios");

            migrationBuilder.DropColumn(
                name: "UserName",
                table: "Positions");

            migrationBuilder.DropColumn(
                name: "UserName",
                table: "Indicators");

            migrationBuilder.DropColumn(
                name: "UserName",
                table: "BundleBacktestRequests");

            migrationBuilder.DropColumn(
                name: "UserName",
                table: "Backtests");

            migrationBuilder.AddColumn<int>(
                name: "UserId",
                table: "Signals",
                type: "integer",
                nullable: true);

            migrationBuilder.AddColumn<int>(
                name: "UserId",
                table: "Scenarios",
                type: "integer",
                nullable: false,
                defaultValue: 0);

            migrationBuilder.AddColumn<int>(
                name: "UserId",
                table: "Positions",
                type: "integer",
                nullable: true);

            migrationBuilder.AddColumn<int>(
                name: "UserId",
                table: "Indicators",
                type: "integer",
                nullable: true);

            migrationBuilder.AddColumn<int>(
                name: "UserId",
                table: "Backtests",
                type: "integer",
                nullable: false,
                defaultValue: 0);

            migrationBuilder.CreateIndex(
                name: "IX_Signals_Identifier_Date_UserId",
                table: "Signals",
                columns: new[] { "Identifier", "Date", "UserId" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_Signals_UserId",
                table: "Signals",
                column: "UserId");

            migrationBuilder.CreateIndex(
                name: "IX_Signals_UserId_Date",
                table: "Signals",
                columns: new[] { "UserId", "Date" });

            migrationBuilder.CreateIndex(
                name: "IX_Scenarios_UserId",
                table: "Scenarios",
                column: "UserId");

            migrationBuilder.CreateIndex(
                name: "IX_Scenarios_UserId_Name",
                table: "Scenarios",
                columns: new[] { "UserId", "Name" });

            migrationBuilder.CreateIndex(
                name: "IX_Positions_UserId",
                table: "Positions",
                column: "UserId");

            migrationBuilder.CreateIndex(
                name: "IX_Positions_UserId_Identifier",
                table: "Positions",
                columns: new[] { "UserId", "Identifier" });

            migrationBuilder.CreateIndex(
                name: "IX_Indicators_UserId",
                table: "Indicators",
                column: "UserId");

            migrationBuilder.CreateIndex(
                name: "IX_Indicators_UserId_Name",
                table: "Indicators",
                columns: new[] { "UserId", "Name" });

            migrationBuilder.CreateIndex(
                name: "IX_BundleBacktestRequests_UserId_CreatedAt",
                table: "BundleBacktestRequests",
                columns: new[] { "UserId", "CreatedAt" });

            migrationBuilder.CreateIndex(
                name: "IX_Backtests_UserId",
                table: "Backtests",
                column: "UserId");

            migrationBuilder.CreateIndex(
                name: "IX_Backtests_UserId_Score",
                table: "Backtests",
                columns: new[] { "UserId", "Score" });

            migrationBuilder.AddForeignKey(
                name: "FK_Backtests_Users_UserId",
                table: "Backtests",
                column: "UserId",
                principalTable: "Users",
                principalColumn: "Id",
                onDelete: ReferentialAction.SetNull);

            migrationBuilder.AddForeignKey(
                name: "FK_Indicators_Users_UserId",
                table: "Indicators",
                column: "UserId",
                principalTable: "Users",
                principalColumn: "Id",
                onDelete: ReferentialAction.SetNull);

            migrationBuilder.AddForeignKey(
                name: "FK_Positions_Users_UserId",
                table: "Positions",
                column: "UserId",
                principalTable: "Users",
                principalColumn: "Id",
                onDelete: ReferentialAction.SetNull);

            migrationBuilder.AddForeignKey(
                name: "FK_Scenarios_Users_UserId",
                table: "Scenarios",
                column: "UserId",
                principalTable: "Users",
                principalColumn: "Id",
                onDelete: ReferentialAction.SetNull);

            migrationBuilder.AddForeignKey(
                name: "FK_Signals_Users_UserId",
                table: "Signals",
                column: "UserId",
                principalTable: "Users",
                principalColumn: "Id",
                onDelete: ReferentialAction.SetNull);
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_Backtests_Users_UserId",
                table: "Backtests");

            migrationBuilder.DropForeignKey(
                name: "FK_Indicators_Users_UserId",
                table: "Indicators");

            migrationBuilder.DropForeignKey(
                name: "FK_Positions_Users_UserId",
                table: "Positions");

            migrationBuilder.DropForeignKey(
                name: "FK_Scenarios_Users_UserId",
                table: "Scenarios");

            migrationBuilder.DropForeignKey(
                name: "FK_Signals_Users_UserId",
                table: "Signals");

            migrationBuilder.DropIndex(
                name: "IX_Signals_Identifier_Date_UserId",
                table: "Signals");

            migrationBuilder.DropIndex(
                name: "IX_Signals_UserId",
                table: "Signals");

            migrationBuilder.DropIndex(
                name: "IX_Signals_UserId_Date",
                table: "Signals");

            migrationBuilder.DropIndex(
                name: "IX_Scenarios_UserId",
                table: "Scenarios");

            migrationBuilder.DropIndex(
                name: "IX_Scenarios_UserId_Name",
                table: "Scenarios");

            migrationBuilder.DropIndex(
                name: "IX_Positions_UserId",
                table: "Positions");

            migrationBuilder.DropIndex(
                name: "IX_Positions_UserId_Identifier",
                table: "Positions");

            migrationBuilder.DropIndex(
                name: "IX_Indicators_UserId",
                table: "Indicators");

            migrationBuilder.DropIndex(
                name: "IX_Indicators_UserId_Name",
                table: "Indicators");

            migrationBuilder.DropIndex(
                name: "IX_BundleBacktestRequests_UserId_CreatedAt",
                table: "BundleBacktestRequests");

            migrationBuilder.DropIndex(
                name: "IX_Backtests_UserId",
                table: "Backtests");

            migrationBuilder.DropIndex(
                name: "IX_Backtests_UserId_Score",
                table: "Backtests");

            migrationBuilder.DropColumn(
                name: "UserId",
                table: "Signals");

            migrationBuilder.DropColumn(
                name: "UserId",
                table: "Scenarios");

            migrationBuilder.DropColumn(
                name: "UserId",
                table: "Positions");

            migrationBuilder.DropColumn(
                name: "UserId",
                table: "Indicators");

            migrationBuilder.DropColumn(
                name: "UserId",
                table: "Backtests");

            migrationBuilder.AddColumn<string>(
                name: "UserName",
                table: "Signals",
                type: "character varying(255)",
                maxLength: 255,
                nullable: true);

            migrationBuilder.AddColumn<string>(
                name: "UserName",
                table: "Scenarios",
                type: "character varying(255)",
                maxLength: 255,
                nullable: true);

            migrationBuilder.AddColumn<string>(
                name: "UserName",
                table: "Positions",
                type: "character varying(255)",
                maxLength: 255,
                nullable: true);

            migrationBuilder.AddColumn<string>(
                name: "UserName",
                table: "Indicators",
                type: "character varying(255)",
                maxLength: 255,
                nullable: true);

            migrationBuilder.AddColumn<string>(
                name: "UserName",
                table: "BundleBacktestRequests",
                type: "character varying(255)",
                maxLength: 255,
                nullable: false,
                defaultValue: "");

            migrationBuilder.AddColumn<string>(
                name: "UserName",
                table: "Backtests",
                type: "character varying(255)",
                maxLength: 255,
                nullable: false,
                defaultValue: "");

            migrationBuilder.CreateIndex(
                name: "IX_Signals_Identifier_Date_UserName",
                table: "Signals",
                columns: new[] { "Identifier", "Date", "UserName" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_Signals_UserName",
                table: "Signals",
                column: "UserName");

            migrationBuilder.CreateIndex(
                name: "IX_Signals_UserName_Date",
                table: "Signals",
                columns: new[] { "UserName", "Date" });

            migrationBuilder.CreateIndex(
                name: "IX_Scenarios_UserName",
                table: "Scenarios",
                column: "UserName");

            migrationBuilder.CreateIndex(
                name: "IX_Scenarios_UserName_Name",
                table: "Scenarios",
                columns: new[] { "UserName", "Name" });

            migrationBuilder.CreateIndex(
                name: "IX_Positions_UserName",
                table: "Positions",
                column: "UserName");

            migrationBuilder.CreateIndex(
                name: "IX_Positions_UserName_Identifier",
                table: "Positions",
                columns: new[] { "UserName", "Identifier" });

            migrationBuilder.CreateIndex(
                name: "IX_Indicators_UserName",
                table: "Indicators",
                column: "UserName");

            migrationBuilder.CreateIndex(
                name: "IX_Indicators_UserName_Name",
                table: "Indicators",
                columns: new[] { "UserName", "Name" });

            migrationBuilder.CreateIndex(
                name: "IX_BundleBacktestRequests_UserName",
                table: "BundleBacktestRequests",
                column: "UserName");

            migrationBuilder.CreateIndex(
                name: "IX_BundleBacktestRequests_UserName_CreatedAt",
                table: "BundleBacktestRequests",
                columns: new[] { "UserName", "CreatedAt" });

            migrationBuilder.CreateIndex(
                name: "IX_Backtests_UserName",
                table: "Backtests",
                column: "UserName");

            migrationBuilder.CreateIndex(
                name: "IX_Backtests_UserName_Score",
                table: "Backtests",
                columns: new[] { "UserName", "Score" });
        }
    }
}
