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

#nullable disable

namespace MyOffice.Migrations.Postgres.Migrations
{
    /// <inheritdoc />
    public partial class Motion : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_AccountMotions_Users_UserId",
                table: "AccountMotions");

            migrationBuilder.RenameColumn(
                name: "AmountOut",
                table: "AccountMotions",
                newName: "AmountPlus");

            migrationBuilder.RenameColumn(
                name: "AmountIn",
                table: "AccountMotions",
                newName: "AmountMinus");

            migrationBuilder.AlterColumn<Guid>(
                name: "UserId",
                table: "AccountMotions",
                type: "uuid",
                nullable: true,
                oldClrType: typeof(Guid),
                oldType: "uuid");

            migrationBuilder.AddColumn<string>(
                name: "Description",
                table: "AccountMotions",
                type: "text",
                nullable: true);

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

            migrationBuilder.CreateTable(
                name: "GlobalMotions",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    Name = table.Column<string>(type: "text", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_GlobalMotions", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "MotionCategories",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    UserId = table.Column<Guid>(type: "uuid", nullable: false),
                    Name = table.Column<string>(type: "text", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_MotionCategories", x => x.Id);
                    table.ForeignKey(
                        name: "FK_MotionCategories_Users_UserId",
                        column: x => x.UserId,
                        principalTable: "Users",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "Motions",
                columns: table => new
                {
                    Id = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    CategoryId = table.Column<Guid>(type: "uuid", nullable: false),
                    MotionGlobalId = table.Column<Guid>(type: "uuid", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Motions", x => x.Id);
                    table.ForeignKey(
                        name: "FK_Motions_GlobalMotions_MotionGlobalId",
                        column: x => x.MotionGlobalId,
                        principalTable: "GlobalMotions",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_Motions_MotionCategories_CategoryId",
                        column: x => x.CategoryId,
                        principalTable: "MotionCategories",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateIndex(
                name: "IX_AccountMotions_MotionId",
                table: "AccountMotions",
                column: "MotionId");

            migrationBuilder.CreateIndex(
                name: "IX_MotionCategories_UserId",
                table: "MotionCategories",
                column: "UserId");

            migrationBuilder.CreateIndex(
                name: "IX_Motions_CategoryId",
                table: "Motions",
                column: "CategoryId");

            migrationBuilder.CreateIndex(
                name: "IX_Motions_MotionGlobalId",
                table: "Motions",
                column: "MotionGlobalId");

            migrationBuilder.AddForeignKey(
                name: "FK_AccountMotions_Motions_MotionId",
                table: "AccountMotions",
                column: "MotionId",
                principalTable: "Motions",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_AccountMotions_Users_UserId",
                table: "AccountMotions",
                column: "UserId",
                principalTable: "Users",
                principalColumn: "Id");
        }

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

            migrationBuilder.DropForeignKey(
                name: "FK_AccountMotions_Users_UserId",
                table: "AccountMotions");

            migrationBuilder.DropTable(
                name: "Motions");

            migrationBuilder.DropTable(
                name: "GlobalMotions");

            migrationBuilder.DropTable(
                name: "MotionCategories");

            migrationBuilder.DropIndex(
                name: "IX_AccountMotions_MotionId",
                table: "AccountMotions");

            migrationBuilder.DropColumn(
                name: "Description",
                table: "AccountMotions");

            migrationBuilder.DropColumn(
                name: "MotionId",
                table: "AccountMotions");

            migrationBuilder.RenameColumn(
                name: "AmountPlus",
                table: "AccountMotions",
                newName: "AmountOut");

            migrationBuilder.RenameColumn(
                name: "AmountMinus",
                table: "AccountMotions",
                newName: "AmountIn");

            migrationBuilder.AlterColumn<Guid>(
                name: "UserId",
                table: "AccountMotions",
                type: "uuid",
                nullable: false,
                defaultValue: new Guid("00000000-0000-0000-0000-000000000000"),
                oldClrType: typeof(Guid),
                oldType: "uuid",
                oldNullable: true);

            migrationBuilder.AlterColumn<long>(
                name: "Id",
                table: "AccountMotions",
                type: "bigint",
                nullable: false,
                oldClrType: typeof(Guid),
                oldType: "uuid")
                .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);

            migrationBuilder.AddForeignKey(
                name: "FK_AccountMotions_Users_UserId",
                table: "AccountMotions",
                column: "UserId",
                principalTable: "Users",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);
        }
    }
}
