using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Monitoring.Infrastructure.Data.Migrations;

/// <inheritdoc />
public partial class FixObjectStates : Migration
{
	/// <inheritdoc />
	protected override void Up(MigrationBuilder migrationBuilder)
	{
		_ = migrationBuilder.DropForeignKey(
			name: "FK_Assets_AssetGroups_GroupId",
			table: "Assets");

		_ = migrationBuilder.DropForeignKey(
			name: "FK_Events_Organizations_OrganizationId",
			table: "Events");

		_ = migrationBuilder.DropColumn(
			name: "HasAlarms",
			table: "Zones");

		_ = migrationBuilder.DropColumn(
			name: "HasFaults",
			table: "Zones");

		_ = migrationBuilder.DropColumn(
			name: "Code",
			table: "Events");

		_ = migrationBuilder.DropColumn(
			name: "IsAlarm",
			table: "Events");

		_ = migrationBuilder.DropColumn(
			name: "HasAlarms",
			table: "Assets");

		_ = migrationBuilder.DropColumn(
			name: "HasFaults",
			table: "Assets");

		_ = migrationBuilder.DropColumn(
			name: "IsOnline",
			table: "Assets");

		_ = migrationBuilder.DropColumn(
			name: "HasAlarms",
			table: "Areas");

		_ = migrationBuilder.DropColumn(
			name: "HasFaults",
			table: "Areas");

		_ = migrationBuilder.DropIndex(
			name: "IX_Events_OrganizationId",
			table: "Events");

		_ = migrationBuilder.DropColumn(
			name: "OrganizationId",
			table: "Events");

		_ = migrationBuilder.AddColumn<Guid>(
			name: "CodeId",
			table: "Events",
			type: "uuid",
			nullable: false,
			defaultValue: new Guid("00000000-0000-0000-0000-000000000000"));

		_ = migrationBuilder.CreateIndex(
			name: "IX_Events_CodeId",
			table: "Events",
			column: "CodeId");

		_ = migrationBuilder.AlterColumn<string>(
			name: "PasswordHash",
			table: "Users",
			type: "character varying(100)",
			maxLength: 100,
			nullable: false,
			oldClrType: typeof(string),
			oldType: "text");

		_ = migrationBuilder.AddColumn<Guid>(
			name: "ZoneId",
			table: "Faults",
			type: "uuid",
			nullable: false,
			defaultValue: new Guid("00000000-0000-0000-0000-000000000000"));

		_ = migrationBuilder.AlterColumn<int>(
			name: "ZoneIndex",
			table: "Events",
			type: "integer",
			nullable: false,
			defaultValue: 0,
			oldClrType: typeof(int),
			oldType: "integer",
			oldNullable: true);

		_ = migrationBuilder.AlterColumn<int>(
			name: "AreaIndex",
			table: "Events",
			type: "integer",
			nullable: false,
			defaultValue: 0,
			oldClrType: typeof(int),
			oldType: "integer",
			oldNullable: true);

		_ = migrationBuilder.AddColumn<bool>(
			name: "IsGuarded",
			table: "Areas",
			type: "boolean",
			nullable: false,
			defaultValue: false);

		_ = migrationBuilder.CreateTable(
			name: "Alarms",
			columns: table => new
			{
				Id = table.Column<Guid>(type: "uuid", nullable: false),
				EventId = table.Column<Guid>(type: "uuid", nullable: false),
				ZoneId = table.Column<Guid>(type: "uuid", nullable: false)
			},
			constraints: table =>
			{
				_ = table.PrimaryKey("PK_Alarms", x => x.Id);
				_ = table.ForeignKey(
					name: "FK_Alarms_Events_EventId",
					column: x => x.EventId,
					principalTable: "Events",
					principalColumn: "Id");
				_ = table.ForeignKey(
					name: "FK_Alarms_Zones_ZoneId",
					column: x => x.ZoneId,
					principalTable: "Zones",
					principalColumn: "Id");
			});

		_ = migrationBuilder.CreateTable(
			name: "EventCodes",
			columns: table => new
			{
				Id = table.Column<Guid>(type: "uuid", nullable: false),
				Value = table.Column<int>(type: "integer", nullable: false),
				Group = table.Column<int>(type: "integer", nullable: false),
				IsActive = table.Column<bool>(type: "boolean", nullable: false)
			},
			constraints: table =>
			{
				_ = table.PrimaryKey("PK_EventCodes", x => x.Id);
			});

		_ = migrationBuilder.InsertData(
			table: "Roles",
			columns: new[] { "Id", "Name", "UpperCaseName" },
			values: new object[] { new Guid("6e7c3d9f-7d6a-4d0c-8c9b-7c0d0e8f9f8f"), "Visitor", "VISITOR" });

		_ = migrationBuilder.CreateIndex(
			name: "IX_Faults_ZoneId",
			table: "Faults",
			column: "ZoneId");

		_ = migrationBuilder.CreateIndex(
			name: "IX_Assets_Id",
			table: "Assets",
			column: "Id",
			unique: true);

		_ = migrationBuilder.CreateIndex(
			name: "IX_Alarms_EventId",
			table: "Alarms",
			column: "EventId");

		_ = migrationBuilder.CreateIndex(
			name: "IX_Alarms_ZoneId",
			table: "Alarms",
			column: "ZoneId");

		_ = migrationBuilder.CreateIndex(
			name: "IX_EventCodes_Group",
			table: "EventCodes",
			column: "Group");

		_ = migrationBuilder.CreateIndex(
			name: "IX_EventCodes_Value",
			table: "EventCodes",
			column: "Value",
			unique: true);

		_ = migrationBuilder.AddForeignKey(
			name: "FK_Assets_AssetGroups_GroupId",
			table: "Assets",
			column: "GroupId",
			principalTable: "AssetGroups",
			principalColumn: "Id");

		_ = migrationBuilder.AddForeignKey(
			name: "FK_Events_EventCodes_CodeId",
			table: "Events",
			column: "CodeId",
			principalTable: "EventCodes",
			principalColumn: "Id");

		_ = migrationBuilder.AddForeignKey(
			name: "FK_Faults_Zones_ZoneId",
			table: "Faults",
			column: "ZoneId",
			principalTable: "Zones",
			principalColumn: "Id",
			onDelete: ReferentialAction.NoAction);
	}

	/// <inheritdoc />
	protected override void Down(MigrationBuilder migrationBuilder)
	{
		_ = migrationBuilder.DropForeignKey(
			name: "FK_Assets_AssetGroups_GroupId",
			table: "Assets");

		_ = migrationBuilder.DropForeignKey(
			name: "FK_Events_EventCodes_CodeId",
			table: "Events");

		_ = migrationBuilder.DropForeignKey(
			name: "FK_Faults_Zones_ZoneId",
			table: "Faults");

		_ = migrationBuilder.DropTable(
			name: "Alarms");

		_ = migrationBuilder.DropTable(
			name: "EventCodes");

		_ = migrationBuilder.DropIndex(
			name: "IX_Faults_ZoneId",
			table: "Faults");

		_ = migrationBuilder.DropIndex(
			name: "IX_Assets_Id",
			table: "Assets");

		_ = migrationBuilder.DeleteData(
			table: "Roles",
			keyColumn: "Id",
			keyValue: new Guid("6e7c3d9f-7d6a-4d0c-8c9b-7c0d0e8f9f8f"));

		_ = migrationBuilder.DropColumn(
			name: "ZoneId",
			table: "Faults");

		_ = migrationBuilder.DropColumn(
			name: "IsGuarded",
			table: "Areas");

		_ = migrationBuilder.RenameColumn(
			name: "CodeId",
			table: "Events",
			newName: "OrganizationId");

		_ = migrationBuilder.RenameIndex(
			name: "IX_Events_CodeId",
			table: "Events",
			newName: "IX_Events_OrganizationId");

		_ = migrationBuilder.AddColumn<bool>(
			name: "HasAlarms",
			table: "Zones",
			type: "boolean",
			nullable: false,
			defaultValue: false);

		_ = migrationBuilder.AddColumn<bool>(
			name: "HasFaults",
			table: "Zones",
			type: "boolean",
			nullable: false,
			defaultValue: false);

		_ = migrationBuilder.AlterColumn<string>(
			name: "PasswordHash",
			table: "Users",
			type: "text",
			nullable: false,
			oldClrType: typeof(string),
			oldType: "character varying(100)",
			oldMaxLength: 100);

		_ = migrationBuilder.AlterColumn<int>(
			name: "ZoneIndex",
			table: "Events",
			type: "integer",
			nullable: true,
			oldClrType: typeof(int),
			oldType: "integer");

		_ = migrationBuilder.AlterColumn<int>(
			name: "AreaIndex",
			table: "Events",
			type: "integer",
			nullable: true,
			oldClrType: typeof(int),
			oldType: "integer");

		_ = migrationBuilder.AddColumn<int>(
			name: "Code",
			table: "Events",
			type: "integer",
			nullable: false,
			defaultValue: 0);

		_ = migrationBuilder.AddColumn<bool>(
			name: "IsAlarm",
			table: "Events",
			type: "boolean",
			nullable: true);

		_ = migrationBuilder.AddColumn<bool>(
			name: "HasAlarms",
			table: "Assets",
			type: "boolean",
			nullable: false,
			defaultValue: false);

		_ = migrationBuilder.AddColumn<bool>(
			name: "HasFaults",
			table: "Assets",
			type: "boolean",
			nullable: false,
			defaultValue: false);

		_ = migrationBuilder.AddColumn<bool>(
			name: "IsOnline",
			table: "Assets",
			type: "boolean",
			nullable: false,
			defaultValue: false);

		_ = migrationBuilder.AddColumn<bool>(
			name: "HasAlarms",
			table: "Areas",
			type: "boolean",
			nullable: false,
			defaultValue: false);

		_ = migrationBuilder.AddColumn<bool>(
			name: "HasFaults",
			table: "Areas",
			type: "boolean",
			nullable: false,
			defaultValue: false);

		_ = migrationBuilder.AddForeignKey(
			name: "FK_Assets_AssetGroups_GroupId",
			table: "Assets",
			column: "GroupId",
			principalTable: "AssetGroups",
			principalColumn: "Id",
			onDelete: ReferentialAction.SetNull);

		_ = migrationBuilder.AddForeignKey(
			name: "FK_Events_Organizations_OrganizationId",
			table: "Events",
			column: "OrganizationId",
			principalTable: "Organizations",
			principalColumn: "Id",
			onDelete: ReferentialAction.Cascade);
	}
}
