using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace PARR.DAL.Migrations
{
    /// <inheritdoc />
    public partial class tblsJobGroups : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.EnsureSchema(
                name: "jobGroup");

            migrationBuilder.RenameTable(
                name: "GroupTypes",
                schema: "job",
                newName: "GroupTypes",
                newSchema: "jobGroup");

            migrationBuilder.RenameTable(
                name: "Groups",
                schema: "job",
                newName: "Groups",
                newSchema: "jobGroup");

            migrationBuilder.RenameTable(
                name: "GroupDistributionConfigs",
                schema: "job",
                newName: "GroupDistributionConfigs",
                newSchema: "jobGroup");

            migrationBuilder.AddColumn<bool>(
                name: "IsAllowJobUnitFilter",
                schema: "jobGroup",
                table: "GroupTypes",
                type: "boolean",
                nullable: false,
                defaultValue: false,
                comment: "Разрешить Job фильтры. Влияет только на интерфейс. Мэтчер собирает все фильтры из группы и работ всегда.");

            migrationBuilder.AddColumn<bool>(
                name: "IsJobGroupAutoControl",
                schema: "jobGroup",
                table: "GroupTypes",
                type: "boolean",
                nullable: false,
                defaultValue: false,
                comment: "Автоконтролем управляет JobGroup? true - да JobGroup, false - Job. Влияет на интерфейс и на логику работы автоконтроля.");

            migrationBuilder.CreateTable(
                name: "AutoControls",
                schema: "jobGroup",
                columns: table => new
                {
                    JobGroupId = table.Column<Guid>(type: "uuid", nullable: false),
                    IsEnable = table.Column<bool>(type: "boolean", nullable: false),
                    InitUsedTemplateState = table.Column<bool>(type: "boolean", nullable: false),
                    InitUsedScheduleState = table.Column<bool>(type: "boolean", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_AutoControls", x => x.JobGroupId);
                    table.ForeignKey(
                        name: "FK_AutoControls_Groups_JobGroupId",
                        column: x => x.JobGroupId,
                        principalSchema: "jobGroup",
                        principalTable: "Groups",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                },
                comment: "Таблица управления автоконтролем для группы работ");

            migrationBuilder.CreateTable(
                name: "UnitFilters",
                schema: "jobGroup",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    DateCreated = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
                    UnitFilter = table.Column<string>(type: "text", nullable: false),
                    JobGroupId = table.Column<Guid>(type: "uuid", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_UnitFilters", x => x.Id);
                    table.ForeignKey(
                        name: "FK_UnitFilters_Groups_JobGroupId",
                        column: x => x.JobGroupId,
                        principalSchema: "jobGroup",
                        principalTable: "Groups",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                },
                comment: "Таблица описания критериев выборки ЭК, описание полей в АСУ ЕСПП");

            migrationBuilder.CreateTable(
                name: "FieldFilters",
                schema: "jobGroup",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    DateCreated = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
                    UnitFilterId = table.Column<Guid>(type: "uuid", nullable: false),
                    FieldId = table.Column<Guid>(type: "uuid", nullable: false),
                    ValueMask = table.Column<string>(type: "text", nullable: false),
                    IsInverse = table.Column<bool>(type: "boolean", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_FieldFilters", x => x.Id);
                    table.ForeignKey(
                        name: "FK_FieldFilters_Fields_FieldId",
                        column: x => x.FieldId,
                        principalSchema: "unit",
                        principalTable: "Fields",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_FieldFilters_UnitFilters_UnitFilterId",
                        column: x => x.UnitFilterId,
                        principalSchema: "jobGroup",
                        principalTable: "UnitFilters",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                },
                comment: "Таблица описания критериев выборки аттрибутов ЭК");

            migrationBuilder.CreateTable(
                name: "RelationshipFilters",
                schema: "jobGroup",
                columns: table => new
                {
                    UnitFilterId = table.Column<Guid>(type: "uuid", nullable: false),
                    FieldId = table.Column<Guid>(type: "uuid", nullable: false),
                    IsParent = table.Column<bool>(type: "boolean", nullable: false),
                    ValueMask = table.Column<string>(type: "text", nullable: false),
                    IsFullMatch = table.Column<bool>(type: "boolean", nullable: false),
                    IsInverse = table.Column<bool>(type: "boolean", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_RelationshipFilters", x => new { x.UnitFilterId, x.FieldId });
                    table.ForeignKey(
                        name: "FK_RelationshipFilters_Fields_FieldId",
                        column: x => x.FieldId,
                        principalSchema: "unit",
                        principalTable: "Fields",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_RelationshipFilters_UnitFilters_UnitFilterId",
                        column: x => x.UnitFilterId,
                        principalSchema: "jobGroup",
                        principalTable: "UnitFilters",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                },
                comment: "Таблица фильтров связей ЭК");

            migrationBuilder.UpdateData(
                schema: "jobGroup",
                table: "GroupTypes",
                keyColumn: "Id",
                keyValue: new Guid("318625e5-f833-436a-99df-2a382a1e71c7"),
                columns: new[] { "IsAllowJobUnitFilter", "IsJobGroupAutoControl" },
                values: new object[] { false, true });

            migrationBuilder.UpdateData(
                schema: "jobGroup",
                table: "GroupTypes",
                keyColumn: "Id",
                keyValue: new Guid("4fa62e79-86bb-47c2-be1a-72a716a170fa"),
                columns: new[] { "IsAllowJobUnitFilter", "IsJobGroupAutoControl" },
                values: new object[] { true, false });

            migrationBuilder.UpdateData(
                schema: "jobGroup",
                table: "GroupTypes",
                keyColumn: "Id",
                keyValue: new Guid("6ec58b1a-5c40-47b9-b036-4fa490e8d503"),
                columns: new[] { "IsAllowJobUnitFilter", "IsJobGroupAutoControl" },
                values: new object[] { false, true });

            migrationBuilder.UpdateData(
                table: "Settings",
                keyColumn: "Name",
                keyValue: "Initiator",
                column: "Value",
                value: "АКСЕНОВ АЛЕКСАНДР ЕВГЕНЬЕВИЧ (AKSENOVAE@GVC.OAO.RZD)");

            migrationBuilder.CreateIndex(
                name: "IX_FieldFilters_FieldId1",
                schema: "jobGroup",
                table: "FieldFilters",
                column: "FieldId");

            migrationBuilder.CreateIndex(
                name: "IX_FieldFilters_UnitFilterId1",
                schema: "jobGroup",
                table: "FieldFilters",
                column: "UnitFilterId");

            migrationBuilder.CreateIndex(
                name: "IX_RelationshipFilters_FieldId1",
                schema: "jobGroup",
                table: "RelationshipFilters",
                column: "FieldId");

            migrationBuilder.CreateIndex(
                name: "IX_UnitFilters_JobGroupId",
                schema: "jobGroup",
                table: "UnitFilters",
                column: "JobGroupId");
        }

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

            migrationBuilder.DropForeignKey(
                name: "FK_RelationshipFilters_Fields_FieldId",
                schema: "job",
                table: "RelationshipFilters");

            migrationBuilder.DropTable(
                name: "AutoControls",
                schema: "jobGroup");

            migrationBuilder.DropTable(
                name: "FieldFilters",
                schema: "jobGroup");

            migrationBuilder.DropTable(
                name: "RelationshipFilters",
                schema: "jobGroup");

            migrationBuilder.DropTable(
                name: "UnitFilters",
                schema: "jobGroup");

            migrationBuilder.DropColumn(
                name: "IsAllowJobUnitFilter",
                schema: "jobGroup",
                table: "GroupTypes");

            migrationBuilder.DropColumn(
                name: "IsJobGroupAutoControl",
                schema: "jobGroup",
                table: "GroupTypes");

            migrationBuilder.RenameTable(
                name: "GroupTypes",
                schema: "jobGroup",
                newName: "GroupTypes",
                newSchema: "job");

            migrationBuilder.RenameTable(
                name: "Groups",
                schema: "jobGroup",
                newName: "Groups",
                newSchema: "job");

            migrationBuilder.RenameTable(
                name: "GroupDistributionConfigs",
                schema: "jobGroup",
                newName: "GroupDistributionConfigs",
                newSchema: "job");

            migrationBuilder.UpdateData(
                table: "Settings",
                keyColumn: "Name",
                keyValue: "Initiator",
                column: "Value",
                value: "ОВЧАРЕНКО АЛЕКСЕЙ ВИТАЛЬЕВИЧ (OVCHARENKOAV@GVC.OAO.RZD)");
        }
    }
}
