using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

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

            migrationBuilder.CreateTable(
                name: "Fields",
                schema: "unit",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    DateCreated = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
                    AihitName = table.Column<string>(type: "text", nullable: false),
                    EsppName = table.Column<string>(type: "text", nullable: true),
                    DisplayName = table.Column<string>(type: "text", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Fields", x => x.Id);
                },
                comment: "Справочник полей ЭК");

            migrationBuilder.CreateTable(
                name: "FieldValues",
                schema: "unit",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    DateCreated = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
                    Value = table.Column<string>(type: "text", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_FieldValues", x => x.Id);
                },
                comment: "Значения полей ЭК");

            migrationBuilder.CreateTable(
                name: "Units",
                schema: "unit",
                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)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Units", x => x.Id);
                },
                comment: "Таблица с ЭК");

            migrationBuilder.CreateTable(
                name: "FieldInFieldValues",
                schema: "unit",
                columns: table => new
                {
                    FieldId = table.Column<Guid>(type: "uuid", nullable: false),
                    FieldValueId = table.Column<Guid>(type: "uuid", nullable: false),
                    DateCreated = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_FieldInFieldValues", x => new { x.FieldId, x.FieldValueId });
                    table.ForeignKey(
                        name: "FK_FieldInFieldValues_FieldValues_FieldValueId",
                        column: x => x.FieldValueId,
                        principalSchema: "unit",
                        principalTable: "FieldValues",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_FieldInFieldValues_Fields_FieldId",
                        column: x => x.FieldId,
                        principalSchema: "unit",
                        principalTable: "Fields",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                },
                comment: "Значения полей ЭК");

            migrationBuilder.CreateTable(
                name: "UnitInFields",
                schema: "unit",
                columns: table => new
                {
                    UnitId = table.Column<Guid>(type: "uuid", nullable: false),
                    FieldId = table.Column<Guid>(type: "uuid", nullable: false),
                    DateCreated = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_UnitInFields", x => new { x.UnitId, x.FieldId });
                    table.ForeignKey(
                        name: "FK_UnitInFields_Fields_FieldId",
                        column: x => x.FieldId,
                        principalSchema: "unit",
                        principalTable: "Fields",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_UnitInFields_Units_UnitId",
                        column: x => x.UnitId,
                        principalSchema: "unit",
                        principalTable: "Units",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                },
                comment: "Справочник полей ЭК");

            migrationBuilder.CreateTable(
                name: "UnitInValues",
                schema: "unit",
                columns: table => new
                {
                    UnitId = table.Column<Guid>(type: "uuid", nullable: false),
                    FieldId = table.Column<Guid>(type: "uuid", nullable: false),
                    ValueId = 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)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_UnitInValues", x => new { x.UnitId, x.FieldId });
                    table.ForeignKey(
                        name: "FK_UnitInValues_FieldValues_ValueId",
                        column: x => x.ValueId,
                        principalSchema: "unit",
                        principalTable: "FieldValues",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_UnitInValues_Fields_FieldId",
                        column: x => x.FieldId,
                        principalSchema: "unit",
                        principalTable: "Fields",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_UnitInValues_Units_UnitId",
                        column: x => x.UnitId,
                        principalSchema: "unit",
                        principalTable: "Units",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                },
                comment: "Таблица связи ЭК с полями и со значениями");

            migrationBuilder.CreateIndex(
                name: "IX_FieldInFieldValues_FieldValueId",
                schema: "unit",
                table: "FieldInFieldValues",
                column: "FieldValueId");

            migrationBuilder.CreateIndex(
                name: "IX_Fields_AihitName",
                schema: "unit",
                table: "Fields",
                column: "AihitName");

            migrationBuilder.CreateIndex(
                name: "IX_Fields_EsppName",
                schema: "unit",
                table: "Fields",
                column: "EsppName");

            migrationBuilder.CreateIndex(
                name: "IX_FieldValues_Value",
                schema: "unit",
                table: "FieldValues",
                column: "Value");

            migrationBuilder.CreateIndex(
                name: "IX_UnitInFields_FieldId",
                schema: "unit",
                table: "UnitInFields",
                column: "FieldId");

            migrationBuilder.CreateIndex(
                name: "IX_UnitInValues_FieldId",
                schema: "unit",
                table: "UnitInValues",
                column: "FieldId");

            migrationBuilder.CreateIndex(
                name: "IX_UnitInValues_ValueId",
                schema: "unit",
                table: "UnitInValues",
                column: "ValueId");

            migrationBuilder.CreateIndex(
                name: "IX_Units_Name",
                schema: "unit",
                table: "Units",
                column: "Name",
                unique: true);
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "FieldInFieldValues",
                schema: "unit");

            migrationBuilder.DropTable(
                name: "UnitInFields",
                schema: "unit");

            migrationBuilder.DropTable(
                name: "UnitInValues",
                schema: "unit");

            migrationBuilder.DropTable(
                name: "FieldValues",
                schema: "unit");

            migrationBuilder.DropTable(
                name: "Fields",
                schema: "unit");

            migrationBuilder.DropTable(
                name: "Units",
                schema: "unit");
        }
    }
}
