using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Migrations
{
    /// <inheritdoc />
    public partial class AddTemplateFolders : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<int>(
                name: "FolderId",
                table: "Templates",
                type: "int",
                nullable: true);

            migrationBuilder.CreateTable(
                name: "TemplateFolders",
                columns: table => new
                {
                    Id = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    Name = table.Column<string>(type: "nvarchar(200)", maxLength: 200, nullable: false),
                    ParentFolderId = table.Column<int>(type: "int", nullable: true),
                    SortOrder = table.Column<int>(type: "int", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_TemplateFolders", x => x.Id);
                    table.ForeignKey(
                        name: "FK_TemplateFolders_TemplateFolders_ParentFolderId",
                        column: x => x.ParentFolderId,
                        principalTable: "TemplateFolders",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateIndex(
                name: "IX_Templates_FolderId",
                table: "Templates",
                column: "FolderId");

            migrationBuilder.CreateIndex(
                name: "IX_TemplateFolders_ParentFolderId_Name",
                table: "TemplateFolders",
                columns: new[] { "ParentFolderId", "Name" },
                unique: true,
                filter: "[ParentFolderId] IS NOT NULL");

            migrationBuilder.AddForeignKey(
                name: "FK_Templates_TemplateFolders_FolderId",
                table: "Templates",
                column: "FolderId",
                principalTable: "TemplateFolders",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);
        }

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

            migrationBuilder.DropTable(
                name: "TemplateFolders");

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

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