using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace esuite.Database.SqlServer.Migrations
{
    /// <inheritdoc />
    public partial class ExternalKeySupport : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.EnsureSchema(
                name: "Miscellaneous");

            migrationBuilder.CreateTable(
                name: "ExternalSystems",
                schema: "Miscellaneous",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    SystemName = table.Column<string>(type: "nvarchar(200)", maxLength: 200, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ExternalSystems", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "ExternalKeys",
                schema: "Miscellaneous",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    EntityName = table.Column<string>(type: "nvarchar(450)", nullable: false),
                    PrimaryKey = table.Column<string>(type: "nvarchar(450)", nullable: false),
                    ExternalSystemId = table.Column<long>(type: "bigint", nullable: false),
                    ExternalId = table.Column<string>(type: "nvarchar(450)", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ExternalKeys", x => x.Id);
                    table.ForeignKey(
                        name: "FK_ExternalKeys_ExternalSystems_ExternalSystemId",
                        column: x => x.ExternalSystemId,
                        principalSchema: "Miscellaneous",
                        principalTable: "ExternalSystems",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.InsertData(
                schema: "Miscellaneous",
                table: "ExternalSystems",
                columns: ["Id", "SystemName"],
                values: [1L, "e-flow"]);

            migrationBuilder.CreateIndex(
                name: "IX_ExternalKeys_EntityName_PrimaryKey",
                schema: "Miscellaneous",
                table: "ExternalKeys",
                columns: ["EntityName", "PrimaryKey"],
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_ExternalKeys_ExternalSystemId_ExternalId",
                schema: "Miscellaneous",
                table: "ExternalKeys",
                columns: ["ExternalSystemId", "ExternalId"],
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_ExternalSystems_SystemName",
                schema: "Miscellaneous",
                table: "ExternalSystems",
                column: "SystemName",
                unique: true);
        }

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

            migrationBuilder.DropTable(
                name: "ExternalSystems",
                schema: "Miscellaneous");
        }
    }
}
