using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Monitoring.Infrastructure.Data.Migrations
{
	/// <inheritdoc />
	public partial class AddCheckListsAndAlarmStatuses : Migration
	{
		/// <inheritdoc />
		protected override void Up(MigrationBuilder migrationBuilder)
		{
			migrationBuilder.DropTable(name: "AlarmAcceptances");

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

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

			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.CreateTable(
				name: "CheckListTemplates",
				columns: table => new
				{
					Id = table.Column<Guid>(type: "uuid", nullable: false),
					Type = table.Column<int>(type: "integer", nullable: false),
				},
				constraints: table =>
				{
					table.PrimaryKey("PK_CheckListTemplates", x => x.Id);
				}
			);

			migrationBuilder.CreateTable(
				name: "CheckLists",
				columns: table => new
				{
					Id = table.Column<Guid>(type: "uuid", nullable: false),
					TemplateId = table.Column<Guid>(type: "uuid", nullable: false),
					AlarmId = table.Column<Guid>(type: "uuid", nullable: false),
				},
				constraints: table =>
				{
					table.PrimaryKey("PK_CheckLists", x => x.Id);
					table.ForeignKey(
						name: "FK_CheckLists_Alarms_AlarmId",
						column: x => x.AlarmId,
						principalTable: "Alarms",
						principalColumn: "Id"
					);
					table.ForeignKey(
						name: "FK_CheckLists_CheckListTemplates_TemplateId",
						column: x => x.TemplateId,
						principalTable: "CheckListTemplates",
						principalColumn: "Id"
					);
				}
			);

			migrationBuilder.CreateTable(
				name: "CheckListTemplateItems",
				columns: table => new
				{
					Id = table.Column<Guid>(type: "uuid", nullable: false),
					CheckListTemplateId = table.Column<Guid>(type: "uuid", nullable: false),
					Index = table.Column<int>(type: "integer", nullable: false),
				},
				constraints: table =>
				{
					table.PrimaryKey("PK_CheckListTemplateItems", x => x.Id);
					table.ForeignKey(
						name: "FK_CheckListTemplateItems_CheckListTemplates_CheckListTemplate~",
						column: x => x.CheckListTemplateId,
						principalTable: "CheckListTemplates",
						principalColumn: "Id"
					);
				}
			);

			migrationBuilder.CreateTable(
				name: "CheckListItems",
				columns: table => new
				{
					Id = table.Column<Guid>(type: "uuid", nullable: false),
					TemplateId = table.Column<Guid>(type: "uuid", nullable: false),
					CheckListId = table.Column<Guid>(type: "uuid", nullable: false),
					IsChecked = table.Column<bool>(type: "boolean", nullable: false),
				},
				constraints: table =>
				{
					table.PrimaryKey("PK_CheckListItems", x => x.Id);
					table.ForeignKey(
						name: "FK_CheckListItems_CheckListTemplateItems_TemplateId",
						column: x => x.TemplateId,
						principalTable: "CheckListTemplateItems",
						principalColumn: "Id"
					);
					table.ForeignKey(
						name: "FK_CheckListItems_CheckLists_CheckListId",
						column: x => x.CheckListId,
						principalTable: "CheckLists",
						principalColumn: "Id"
					);
				}
			);

			migrationBuilder.CreateTable(
				name: "CheckListTemplateItemTranslations",
				columns: table => new
				{
					Id = table.Column<Guid>(type: "uuid", nullable: false),
					ItemId = table.Column<Guid>(type: "uuid", nullable: false),
					Name = table.Column<string>(type: "text", nullable: false),
					Description = table.Column<string>(type: "text", nullable: false),
				},
				constraints: table =>
				{
					table.PrimaryKey("PK_CheckListTemplateItemTranslations", x => x.Id);
					table.ForeignKey(
						name: "FK_CheckListTemplateItemTranslations_CheckListTemplateItems_It~",
						column: x => x.ItemId,
						principalTable: "CheckListTemplateItems",
						principalColumn: "Id",
						onDelete: ReferentialAction.Cascade
					);
				}
			);

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

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

			migrationBuilder.CreateIndex(
				name: "IX_CheckListItems_CheckListId",
				table: "CheckListItems",
				column: "CheckListId"
			);

			migrationBuilder.CreateIndex(
				name: "IX_CheckListItems_TemplateId",
				table: "CheckListItems",
				column: "TemplateId"
			);

			migrationBuilder.CreateIndex(
				name: "IX_CheckLists_AlarmId",
				table: "CheckLists",
				column: "AlarmId"
			);

			migrationBuilder.CreateIndex(
				name: "IX_CheckLists_TemplateId",
				table: "CheckLists",
				column: "TemplateId"
			);

			migrationBuilder.CreateIndex(
				name: "IX_CheckListTemplateItems_CheckListTemplateId",
				table: "CheckListTemplateItems",
				column: "CheckListTemplateId"
			);

			migrationBuilder.CreateIndex(
				name: "IX_CheckListTemplateItemTranslations_ItemId",
				table: "CheckListTemplateItemTranslations",
				column: "ItemId"
			);
		}

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

			migrationBuilder.DropTable(name: "CheckListItems");

			migrationBuilder.DropTable(name: "CheckListTemplateItemTranslations");

			migrationBuilder.DropTable(name: "CheckLists");

			migrationBuilder.DropTable(name: "CheckListTemplateItems");

			migrationBuilder.DropTable(name: "CheckListTemplates");

			migrationBuilder.AddColumn<bool>(
				name: "IsAccepted",
				table: "Alarms",
				type: "boolean",
				nullable: false,
				defaultValue: false
			);

			migrationBuilder.AddColumn<bool>(
				name: "IsResolved",
				table: "Alarms",
				type: "boolean",
				nullable: false,
				defaultValue: false
			);

			migrationBuilder.CreateTable(
				name: "AlarmAcceptances",
				columns: table => new
				{
					Id = table.Column<Guid>(type: "uuid", nullable: false),
					AcceptedById = table.Column<Guid>(type: "uuid", nullable: false),
					AlarmId = table.Column<Guid>(type: "uuid", nullable: false),
					TransferredToId = table.Column<Guid>(type: "uuid", nullable: true),
					AcceptedAt = table.Column<DateTime>(
						type: "timestamp with time zone",
						nullable: false
					),
					TransferredAt = table.Column<DateTime>(
						type: "timestamp with time zone",
						nullable: true
					),
				},
				constraints: table =>
				{
					table.PrimaryKey("PK_AlarmAcceptances", x => x.Id);
					table.ForeignKey(
						name: "FK_AlarmAcceptances_Alarms_AlarmId",
						column: x => x.AlarmId,
						principalTable: "Alarms",
						principalColumn: "Id"
					);
					table.ForeignKey(
						name: "FK_AlarmAcceptances_Users_AcceptedById",
						column: x => x.AcceptedById,
						principalTable: "Users",
						principalColumn: "Id"
					);
					table.ForeignKey(
						name: "FK_AlarmAcceptances_Users_TransferredToId",
						column: x => x.TransferredToId,
						principalTable: "Users",
						principalColumn: "Id"
					);
				}
			);

			migrationBuilder.CreateIndex(
				name: "IX_AlarmAcceptances_AcceptedById",
				table: "AlarmAcceptances",
				column: "AcceptedById"
			);

			migrationBuilder.CreateIndex(
				name: "IX_AlarmAcceptances_AlarmId",
				table: "AlarmAcceptances",
				column: "AlarmId"
			);

			migrationBuilder.CreateIndex(
				name: "IX_AlarmAcceptances_TransferredToId",
				table: "AlarmAcceptances",
				column: "TransferredToId"
			);
		}
	}
}
