using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Monitoring.Infrastructure.Data.Migrations
{
	/// <inheritdoc />
	public partial class AddedAlarmScope : Migration
	{
		/// <inheritdoc />
		protected override void Up(MigrationBuilder migrationBuilder)
		{
			migrationBuilder.DropForeignKey(
				name: "FK_CheckLists_Alarms_AlarmId",
				table: "CheckLists"
			);

			migrationBuilder.DropTable(name: "AlarmStatusHistory");

			migrationBuilder.RenameColumn(
				name: "AlarmId",
				table: "CheckLists",
				newName: "AlarmScopeId"
			);

			migrationBuilder.RenameIndex(
				name: "IX_CheckLists_AlarmId",
				table: "CheckLists",
				newName: "IX_CheckLists_AlarmScopeId"
			);

			migrationBuilder.AddColumn<Guid>(
				name: "ScopeId",
				table: "Alarms",
				type: "uuid",
				nullable: true
			);

			migrationBuilder.CreateTable(
				name: "AlarmScopes",
				columns: table => new
				{
					Id = table.Column<Guid>(type: "uuid", nullable: false),
					AssetId = table.Column<Guid>(type: "uuid", nullable: false),
				},
				constraints: table =>
				{
					table.PrimaryKey("PK_AlarmScopes", x => x.Id);
					table.ForeignKey(
						name: "FK_AlarmScopes_Assets_AssetId",
						column: x => x.AssetId,
						principalTable: "Assets",
						principalColumn: "Id",
						onDelete: ReferentialAction.Cascade
					);
				}
			);

			migrationBuilder.CreateTable(
				name: "AlarmScopeStatusHistory",
				columns: table => new
				{
					Id = table.Column<Guid>(type: "uuid", nullable: false),
					AlarmScopeId = table.Column<Guid>(type: "uuid", nullable: false),
					UserId = table.Column<Guid>(type: "uuid", nullable: true),
					CreatedAt = table.Column<DateTime>(
						type: "timestamp with time zone",
						nullable: false
					),
					Status = table.Column<int>(type: "integer", nullable: false),
				},
				constraints: table =>
				{
					table.PrimaryKey("PK_AlarmScopeStatusHistory", x => x.Id);
					table.ForeignKey(
						name: "FK_AlarmScopeStatusHistory_AlarmScopes_AlarmScopeId",
						column: x => x.AlarmScopeId,
						principalTable: "AlarmScopes",
						principalColumn: "Id"
					);
					table.ForeignKey(
						name: "FK_AlarmScopeStatusHistory_Users_UserId",
						column: x => x.UserId,
						principalTable: "Users",
						principalColumn: "Id"
					);
				}
			);

			migrationBuilder.CreateIndex(
				name: "IX_Alarms_ScopeId",
				table: "Alarms",
				column: "ScopeId"
			);

			migrationBuilder.CreateIndex(
				name: "IX_AlarmScopes_AssetId",
				table: "AlarmScopes",
				column: "AssetId"
			);

			migrationBuilder.CreateIndex(
				name: "IX_AlarmScopeStatusHistory_AlarmScopeId",
				table: "AlarmScopeStatusHistory",
				column: "AlarmScopeId"
			);

			migrationBuilder.CreateIndex(
				name: "IX_AlarmScopeStatusHistory_UserId",
				table: "AlarmScopeStatusHistory",
				column: "UserId"
			);

			migrationBuilder.AddForeignKey(
				name: "FK_Alarms_AlarmScopes_ScopeId",
				table: "Alarms",
				column: "ScopeId",
				principalTable: "AlarmScopes",
				principalColumn: "Id"
			);

			migrationBuilder.AddForeignKey(
				name: "FK_CheckLists_AlarmScopes_AlarmScopeId",
				table: "CheckLists",
				column: "AlarmScopeId",
				principalTable: "AlarmScopes",
				principalColumn: "Id"
			);
		}

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

			migrationBuilder.DropForeignKey(
				name: "FK_CheckLists_AlarmScopes_AlarmScopeId",
				table: "CheckLists"
			);

			migrationBuilder.DropTable(name: "AlarmScopeStatusHistory");

			migrationBuilder.DropTable(name: "AlarmScopes");

			migrationBuilder.DropIndex(name: "IX_Alarms_ScopeId", table: "Alarms");

			migrationBuilder.DropColumn(name: "ScopeId", table: "Alarms");

			migrationBuilder.RenameColumn(
				name: "AlarmScopeId",
				table: "CheckLists",
				newName: "AlarmId"
			);

			migrationBuilder.RenameIndex(
				name: "IX_CheckLists_AlarmScopeId",
				table: "CheckLists",
				newName: "IX_CheckLists_AlarmId"
			);

			migrationBuilder.CreateTable(
				name: "AlarmStatusHistory",
				columns: table => new
				{
					Id = table.Column<Guid>(type: "uuid", nullable: false),
					AlarmId = table.Column<Guid>(type: "uuid", nullable: false),
					UserId = table.Column<Guid>(type: "uuid", nullable: true),
					CreatedAt = table.Column<DateTime>(
						type: "timestamp with time zone",
						nullable: false
					),
					Status = table.Column<int>(type: "integer", nullable: false),
				},
				constraints: table =>
				{
					table.PrimaryKey("PK_AlarmStatusHistory", x => x.Id);
					table.ForeignKey(
						name: "FK_AlarmStatusHistory_Alarms_AlarmId",
						column: x => x.AlarmId,
						principalTable: "Alarms",
						principalColumn: "Id"
					);
					table.ForeignKey(
						name: "FK_AlarmStatusHistory_Users_UserId",
						column: x => x.UserId,
						principalTable: "Users",
						principalColumn: "Id"
					);
				}
			);

			migrationBuilder.CreateIndex(
				name: "IX_AlarmStatusHistory_AlarmId",
				table: "AlarmStatusHistory",
				column: "AlarmId"
			);

			migrationBuilder.CreateIndex(
				name: "IX_AlarmStatusHistory_UserId",
				table: "AlarmStatusHistory",
				column: "UserId"
			);

			migrationBuilder.AddForeignKey(
				name: "FK_CheckLists_Alarms_AlarmId",
				table: "CheckLists",
				column: "AlarmId",
				principalTable: "Alarms",
				principalColumn: "Id"
			);
		}
	}
}
