using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace PARR.DAL.Migrations
{
    /// <inheritdoc />
    public partial class JobsUpdateAddJobGroupTbl : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_FieldFilters_JobDetails_JobDetailsId",
                schema: "job",
                table: "FieldFilters");

            migrationBuilder.DropTable(
                name: "JobDetails",
                schema: "job");

            migrationBuilder.DropColumn(
                name: "IsUmbrella",
                schema: "job",
                table: "Jobs");

            migrationBuilder.RenameColumn(
                name: "JobDetailsId",
                schema: "job",
                table: "FieldFilters",
                newName: "UnitFilterId");

            migrationBuilder.RenameIndex(
                name: "IX_FieldFilters_JobDetailsId",
                schema: "job",
                table: "FieldFilters",
                newName: "IX_FieldFilters_UnitFilterId");

            migrationBuilder.AddColumn<Guid>(
                name: "JobId",
                table: "Templates",
                type: "uuid",
                nullable: true);

            migrationBuilder.AddColumn<Guid>(
                name: "UnitId",
                table: "Templates",
                type: "uuid",
                nullable: true);

            migrationBuilder.AddColumn<Guid>(
                name: "GroupId",
                schema: "job",
                table: "Jobs",
                type: "uuid",
                nullable: false,
                defaultValue: new Guid("00000000-0000-0000-0000-000000000000"));

            migrationBuilder.AddColumn<string>(
                name: "WorkName",
                schema: "job",
                table: "Jobs",
                type: "text",
                nullable: false,
                defaultValue: "");

            migrationBuilder.CreateTable(
                name: "Groups",
                schema: "job",
                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),
                    GroupName = table.Column<string>(type: "text", nullable: false),
                    IsUmbrella = table.Column<bool>(type: "boolean", nullable: true),
                    ShortDescription = table.Column<string>(type: "text", nullable: false),
                    FullDescription = table.Column<string>(type: "text", nullable: false),
                    Solution = table.Column<string>(type: "text", nullable: false),
                    Duration = table.Column<string>(type: "text", nullable: false),
                    ReferenceDate = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Groups", x => x.Id);
                },
                comment: "Таблица описания групп работ, для реализации зонтиков");

            migrationBuilder.CreateTable(
                name: "JobUnitFilters",
                schema: "job",
                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),
                    UnitFilter = table.Column<string>(type: "text", nullable: false),
                    JobId = table.Column<Guid>(type: "uuid", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_JobUnitFilters", x => x.Id);
                    table.ForeignKey(
                        name: "FK_JobUnitFilters_Jobs_JobId",
                        column: x => x.JobId,
                        principalSchema: "job",
                        principalTable: "Jobs",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                },
                comment: "Таблица описания критериев выборки ЭК, описание полей в АСУ ЕСПП");

            migrationBuilder.CreateIndex(
                name: "IX_Templates_JobId",
                table: "Templates",
                column: "JobId");

            migrationBuilder.CreateIndex(
                name: "IX_Templates_UnitId",
                table: "Templates",
                column: "UnitId");

            migrationBuilder.CreateIndex(
                name: "IX_Jobs_GroupId",
                schema: "job",
                table: "Jobs",
                column: "GroupId");

            migrationBuilder.CreateIndex(
                name: "IX_JobUnitFilters_JobId",
                schema: "job",
                table: "JobUnitFilters",
                column: "JobId");

            migrationBuilder.AddForeignKey(
                name: "FK_FieldFilters_JobUnitFilters_UnitFilterId",
                schema: "job",
                table: "FieldFilters",
                column: "UnitFilterId",
                principalSchema: "job",
                principalTable: "JobUnitFilters",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_Jobs_Groups_GroupId",
                schema: "job",
                table: "Jobs",
                column: "GroupId",
                principalSchema: "job",
                principalTable: "Groups",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_Templates_Jobs_JobId",
                table: "Templates",
                column: "JobId",
                principalSchema: "job",
                principalTable: "Jobs",
                principalColumn: "Id");

            migrationBuilder.AddForeignKey(
                name: "FK_Templates_Units_UnitId",
                table: "Templates",
                column: "UnitId",
                principalSchema: "unit",
                principalTable: "Units",
                principalColumn: "Id");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_FieldFilters_JobUnitFilters_UnitFilterId",
                schema: "job",
                table: "FieldFilters");

            migrationBuilder.DropForeignKey(
                name: "FK_Jobs_Groups_GroupId",
                schema: "job",
                table: "Jobs");

            migrationBuilder.DropForeignKey(
                name: "FK_Templates_Jobs_JobId",
                table: "Templates");

            migrationBuilder.DropForeignKey(
                name: "FK_Templates_Units_UnitId",
                table: "Templates");

            migrationBuilder.DropTable(
                name: "Groups",
                schema: "job");

            migrationBuilder.DropTable(
                name: "JobUnitFilters",
                schema: "job");

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

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

            migrationBuilder.DropIndex(
                name: "IX_Jobs_GroupId",
                schema: "job",
                table: "Jobs");

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

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

            migrationBuilder.DropColumn(
                name: "GroupId",
                schema: "job",
                table: "Jobs");

            migrationBuilder.DropColumn(
                name: "WorkName",
                schema: "job",
                table: "Jobs");

            migrationBuilder.RenameColumn(
                name: "UnitFilterId",
                schema: "job",
                table: "FieldFilters",
                newName: "JobDetailsId");

            migrationBuilder.RenameIndex(
                name: "IX_FieldFilters_UnitFilterId",
                schema: "job",
                table: "FieldFilters",
                newName: "IX_FieldFilters_JobDetailsId");

            migrationBuilder.AddColumn<bool>(
                name: "IsUmbrella",
                schema: "job",
                table: "Jobs",
                type: "boolean",
                nullable: true);

            migrationBuilder.CreateTable(
                name: "JobDetails",
                schema: "job",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    JobId = 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),
                    Duration = table.Column<string>(type: "text", nullable: false),
                    FullDescription = table.Column<string>(type: "text", nullable: false),
                    ReferenceDate = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
                    ShortDescription = table.Column<string>(type: "text", nullable: false),
                    Solution = table.Column<string>(type: "text", nullable: false),
                    UnitFilter = table.Column<string>(type: "text", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_JobDetails", x => x.Id);
                    table.ForeignKey(
                        name: "FK_JobDetails_Jobs_JobId",
                        column: x => x.JobId,
                        principalSchema: "job",
                        principalTable: "Jobs",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                },
                comment: "Таблица описания критериев выборки ЭК, описание полей в АСУ ЕСПП");

            migrationBuilder.CreateIndex(
                name: "IX_JobDetails_JobId",
                schema: "job",
                table: "JobDetails",
                column: "JobId");

            migrationBuilder.AddForeignKey(
                name: "FK_FieldFilters_JobDetails_JobDetailsId",
                schema: "job",
                table: "FieldFilters",
                column: "JobDetailsId",
                principalSchema: "job",
                principalTable: "JobDetails",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);
        }
    }
}
