using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Migrations
{
    /// <inheritdoc />
    public partial class AddNativeAlarmSources : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "InstanceNativeAlarmSourceOverrides",
                columns: table => new
                {
                    Id = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    InstanceId = table.Column<int>(type: "int", nullable: false),
                    SourceCanonicalName = table.Column<string>(type: "nvarchar(400)", maxLength: 400, nullable: false),
                    ConnectionNameOverride = table.Column<string>(type: "nvarchar(200)", maxLength: 200, nullable: true),
                    SourceReferenceOverride = table.Column<string>(type: "nvarchar(1000)", maxLength: 1000, nullable: true),
                    ConditionFilterOverride = table.Column<string>(type: "nvarchar(1000)", maxLength: 1000, nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_InstanceNativeAlarmSourceOverrides", x => x.Id);
                    table.ForeignKey(
                        name: "FK_InstanceNativeAlarmSourceOverrides_Instances_InstanceId",
                        column: x => x.InstanceId,
                        principalTable: "Instances",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "TemplateNativeAlarmSources",
                columns: table => new
                {
                    Id = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    TemplateId = table.Column<int>(type: "int", nullable: false),
                    Name = table.Column<string>(type: "nvarchar(200)", maxLength: 200, nullable: false),
                    Description = table.Column<string>(type: "nvarchar(2000)", maxLength: 2000, nullable: true),
                    ConnectionName = table.Column<string>(type: "nvarchar(200)", maxLength: 200, nullable: false),
                    SourceReference = table.Column<string>(type: "nvarchar(1000)", maxLength: 1000, nullable: false),
                    ConditionFilter = table.Column<string>(type: "nvarchar(1000)", maxLength: 1000, nullable: true),
                    IsLocked = table.Column<bool>(type: "bit", nullable: false),
                    IsInherited = table.Column<bool>(type: "bit", nullable: false),
                    LockedInDerived = table.Column<bool>(type: "bit", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_TemplateNativeAlarmSources", x => x.Id);
                    table.ForeignKey(
                        name: "FK_TemplateNativeAlarmSources_Templates_TemplateId",
                        column: x => x.TemplateId,
                        principalTable: "Templates",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateIndex(
                name: "IX_InstanceNativeAlarmSourceOverrides_InstanceId_SourceCanonicalName",
                table: "InstanceNativeAlarmSourceOverrides",
                columns: new[] { "InstanceId", "SourceCanonicalName" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_TemplateNativeAlarmSources_TemplateId_Name",
                table: "TemplateNativeAlarmSources",
                columns: new[] { "TemplateId", "Name" },
                unique: true);
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "InstanceNativeAlarmSourceOverrides");

            migrationBuilder.DropTable(
                name: "TemplateNativeAlarmSources");
        }
    }
}
