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

namespace YGNT.QuestionLibrary.Migrations
{
    public partial class addQuestionOrigin : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "Stems");

            migrationBuilder.DropColumn(
                name: "DeleterUserId",
                table: "ChapterTreePaths");

            migrationBuilder.DropColumn(
                name: "DeletionTime",
                table: "ChapterTreePaths");

            migrationBuilder.DropColumn(
                name: "IsDeleted",
                table: "ChapterTreePaths");

            migrationBuilder.AddColumn<long>(
                name: "QuestionOriginId",
                table: "Questions",
                nullable: false,
                defaultValue: 0L);

            migrationBuilder.AddColumn<long>(
                name: "Depth",
                table: "ChapterTreePaths",
                nullable: false,
                defaultValue: 0L);

            migrationBuilder.CreateTable(
                name: "QuestionOrigins",
                columns: table => new
                {
                    Id = table.Column<long>(nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                    QuestionTypeName = table.Column<string>(nullable: false),
                    Difficulty = table.Column<int>(nullable: false),
                    ShortName = table.Column<string>(nullable: false),
                    Html = table.Column<string>(nullable: false),
                    Answer = table.Column<string>(nullable: true),
                    Analysis = table.Column<string>(nullable: true),
                    CreationTime = table.Column<DateTime>(nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_QuestionOrigins", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "QuestionResolves",
                columns: table => new
                {
                    Id = table.Column<long>(nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                    QuestionOriginId = table.Column<long>(nullable: false),
                    Html = table.Column<string>(nullable: true),
                    XmlPath = table.Column<string>(nullable: true),
                    ResolveType = table.Column<int>(nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_QuestionResolves", x => x.Id);
                    table.ForeignKey(
                        name: "FK_QuestionResolves_QuestionOrigins_QuestionOriginId",
                        column: x => x.QuestionOriginId,
                        principalTable: "QuestionOrigins",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateIndex(
                name: "IX_Questions_QuestionOriginId",
                table: "Questions",
                column: "QuestionOriginId");

            migrationBuilder.CreateIndex(
                name: "IX_QuestionResolves_QuestionOriginId",
                table: "QuestionResolves",
                column: "QuestionOriginId");

            migrationBuilder.AddForeignKey(
                name: "FK_Questions_QuestionOrigins_QuestionOriginId",
                table: "Questions",
                column: "QuestionOriginId",
                principalTable: "QuestionOrigins",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_Questions_QuestionOrigins_QuestionOriginId",
                table: "Questions");

            migrationBuilder.DropTable(
                name: "QuestionResolves");

            migrationBuilder.DropTable(
                name: "QuestionOrigins");

            migrationBuilder.DropIndex(
                name: "IX_Questions_QuestionOriginId",
                table: "Questions");

            migrationBuilder.DropColumn(
                name: "QuestionOriginId",
                table: "Questions");

            migrationBuilder.DropColumn(
                name: "Depth",
                table: "ChapterTreePaths");

            migrationBuilder.AddColumn<long>(
                name: "DeleterUserId",
                table: "ChapterTreePaths",
                type: "bigint",
                nullable: true);

            migrationBuilder.AddColumn<DateTime>(
                name: "DeletionTime",
                table: "ChapterTreePaths",
                type: "datetime(6)",
                nullable: true);

            migrationBuilder.AddColumn<bool>(
                name: "IsDeleted",
                table: "ChapterTreePaths",
                type: "tinyint(1)",
                nullable: false,
                defaultValue: false);

            migrationBuilder.CreateTable(
                name: "Stems",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                    CreationTime = table.Column<DateTime>(type: "datetime(6)", nullable: false),
                    CreatorUserId = table.Column<long>(type: "bigint", nullable: true),
                    DeleterUserId = table.Column<long>(type: "bigint", nullable: true),
                    DeletionTime = table.Column<DateTime>(type: "datetime(6)", nullable: true),
                    IsDeleted = table.Column<bool>(type: "tinyint(1)", nullable: false),
                    LastModificationTime = table.Column<DateTime>(type: "datetime(6)", nullable: true),
                    LastModifierUserId = table.Column<long>(type: "bigint", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Stems", x => x.Id);
                });
        }
    }
}
