using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace PARR.DAL.Migrations
{
    /// <inheritdoc />
    public partial class TblsEsppWorksAndAppsAndTEmpl : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<Guid>(
                name: "ApplicationInWorkId",
                table: "Templates",
                type: "uuid",
                nullable: false,
                defaultValue: new Guid("00000000-0000-0000-0000-000000000000"));

            migrationBuilder.CreateTable(
                name: "Processes",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    DateCreated = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
                    DateModified = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
                    Name = table.Column<string>(type: "text", nullable: false),
                    EsppId = table.Column<int>(type: "integer", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Processes", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "Subprocesses",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    DateCreated = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
                    DateModified = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
                    Name = table.Column<string>(type: "text", nullable: false),
                    EsppId = table.Column<int>(type: "integer", nullable: false),
                    ProcessId = table.Column<Guid>(type: "uuid", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Subprocesses", x => x.Id);
                    table.ForeignKey(
                        name: "FK_Subprocesses_Processes_ProcessId",
                        column: x => x.ProcessId,
                        principalTable: "Processes",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "Tnks",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    DateCreated = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
                    DateModified = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
                    Name = table.Column<string>(type: "text", nullable: false),
                    EsppId = table.Column<int>(type: "integer", nullable: false),
                    SubprocessId = table.Column<Guid>(type: "uuid", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Tnks", x => x.Id);
                    table.ForeignKey(
                        name: "FK_Tnks_Subprocesses_SubprocessId",
                        column: x => x.SubprocessId,
                        principalTable: "Subprocesses",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "Works",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    DateCreated = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
                    DateModified = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
                    Name = table.Column<string>(type: "text", nullable: false),
                    EsppId = table.Column<int>(type: "integer", nullable: false),
                    TnkId = table.Column<Guid>(type: "uuid", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Works", x => x.Id);
                    table.ForeignKey(
                        name: "FK_Works_Tnks_TnkId",
                        column: x => x.TnkId,
                        principalTable: "Tnks",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "ApplicationsInWorks",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    DateCreated = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
                    DateModified = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
                    WorkId = table.Column<Guid>(type: "uuid", nullable: false),
                    ApplicationId = table.Column<Guid>(type: "uuid", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ApplicationsInWorks", x => x.Id);
                    table.ForeignKey(
                        name: "FK_ApplicationsInWorks_Applications_ApplicationId",
                        column: x => x.ApplicationId,
                        principalTable: "Applications",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_ApplicationsInWorks_Works_WorkId",
                        column: x => x.WorkId,
                        principalTable: "Works",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateIndex(
                name: "IX_Templates_ApplicationInWorkId",
                table: "Templates",
                column: "ApplicationInWorkId");

            migrationBuilder.CreateIndex(
                name: "IX_Templates_Name",
                table: "Templates",
                column: "Name",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_ApplicationsInWorks_ApplicationId",
                table: "ApplicationsInWorks",
                column: "ApplicationId");

            migrationBuilder.CreateIndex(
                name: "IX_ApplicationsInWorks_WorkId_ApplicationId",
                table: "ApplicationsInWorks",
                columns: new[] { "WorkId", "ApplicationId" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_Subprocesses_ProcessId",
                table: "Subprocesses",
                column: "ProcessId");

            migrationBuilder.CreateIndex(
                name: "IX_Tnks_SubprocessId",
                table: "Tnks",
                column: "SubprocessId");

            migrationBuilder.CreateIndex(
                name: "IX_Works_TnkId",
                table: "Works",
                column: "TnkId");

            migrationBuilder.AddForeignKey(
                name: "FK_Templates_ApplicationsInWorks_ApplicationInWorkId",
                table: "Templates",
                column: "ApplicationInWorkId",
                principalTable: "ApplicationsInWorks",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);
        }

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

            migrationBuilder.DropTable(
                name: "ApplicationsInWorks");

            migrationBuilder.DropTable(
                name: "Works");

            migrationBuilder.DropTable(
                name: "Tnks");

            migrationBuilder.DropTable(
                name: "Subprocesses");

            migrationBuilder.DropTable(
                name: "Processes");

            migrationBuilder.DropIndex(
                name: "IX_Templates_ApplicationInWorkId",
                table: "Templates");

            migrationBuilder.DropIndex(
                name: "IX_Templates_Name",
                table: "Templates");

            migrationBuilder.DropColumn(
                name: "ApplicationInWorkId",
                table: "Templates");
        }
    }
}
