using System;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Predictalytics.Infrastructure.Migrations
{
    /// <inheritdoc />
    public partial class AddTradeContextAndSubcategory : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<DateTime>(
                name: "LastAnalyzedAt",
                table: "Traders",
                type: "datetime(6)",
                nullable: true);

            migrationBuilder.AddColumn<string>(
                name: "Subcategory",
                table: "TraderCategoryPerformances",
                type: "varchar(128)",
                maxLength: 128,
                nullable: false,
                defaultValue: "")
                .Annotation("MySql:CharSet", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "TradeContexts",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                    TradeId = table.Column<long>(type: "bigint", nullable: false),
                    PriceBefore1m = table.Column<decimal>(type: "decimal(18,4)", precision: 18, scale: 4, nullable: true),
                    PriceAfter1m = table.Column<decimal>(type: "decimal(18,4)", precision: 18, scale: 4, nullable: true),
                    EstimatedSlippage = table.Column<decimal>(type: "decimal(18,4)", precision: 18, scale: 4, nullable: true),
                    EstimatedOrderType = table.Column<string>(type: "varchar(32)", maxLength: 32, nullable: false)
                        .Annotation("MySql:CharSet", "utf8mb4")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_TradeContexts", x => x.Id);
                    table.ForeignKey(
                        name: "FK_TradeContexts_Trades_TradeId",
                        column: x => x.TradeId,
                        principalTable: "Trades",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                })
                .Annotation("MySql:CharSet", "utf8mb4");

            migrationBuilder.CreateIndex(
                name: "IX_TraderCategoryPerformances_TraderId_Category_Subcategory",
                table: "TraderCategoryPerformances",
                columns: new[] { "TraderId", "Category", "Subcategory" },
                unique: true);

            migrationBuilder.DropIndex(
                name: "IX_TraderCategoryPerformances_TraderId_Category",
                table: "TraderCategoryPerformances");

            migrationBuilder.CreateIndex(
                name: "IX_TradeContexts_TradeId",
                table: "TradeContexts",
                column: "TradeId",
                unique: true);
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "TradeContexts");

            migrationBuilder.DropColumn(
                name: "LastAnalyzedAt",
                table: "Traders");

            migrationBuilder.DropColumn(
                name: "Subcategory",
                table: "TraderCategoryPerformances");

            migrationBuilder.CreateIndex(
                name: "IX_TraderCategoryPerformances_TraderId_Category",
                table: "TraderCategoryPerformances",
                columns: new[] { "TraderId", "Category" },
                unique: true);

            migrationBuilder.DropIndex(
                name: "IX_TraderCategoryPerformances_TraderId_Category_Subcategory",
                table: "TraderCategoryPerformances");
        }
    }
}
