using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Monitoring.Monolith.Data.Migrations;

/// <inheritdoc />
public partial class Initialize : Migration
{
	/// <inheritdoc />
	protected override void Up(MigrationBuilder migrationBuilder)
	{
		_ = migrationBuilder.CreateTable(
			name: "Organizations",
			columns: table => new
			{
				Id = table.Column<Guid>(type: "uuid", nullable: false),
				Name = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
				Description = table.Column<string>(type: "character varying(1000)", maxLength: 1000, nullable: false)
			},
			constraints: table =>
			{
				_ = table.PrimaryKey("PK_Organizations", x => x.Id);
			});

		_ = migrationBuilder.CreateTable(
			name: "Roles",
			columns: table => new
			{
				Id = table.Column<Guid>(type: "uuid", nullable: false),
				Name = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
				UpperCaseName = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false)
			},
			constraints: table =>
			{
				_ = table.PrimaryKey("PK_Roles", x => x.Id);
			});

		_ = migrationBuilder.CreateTable(
			name: "AssetGroups",
			columns: table => new
			{
				Id = table.Column<Guid>(type: "uuid", nullable: false),
				Name = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
				OrganizationId = table.Column<Guid>(type: "uuid", nullable: false)
			},
			constraints: table =>
			{
				_ = table.PrimaryKey("PK_AssetGroups", x => x.Id);
				_ = table.ForeignKey(
					name: "FK_AssetGroups_Organizations_OrganizationId",
					column: x => x.OrganizationId,
					principalTable: "Organizations",
					principalColumn: "Id",
					onDelete: ReferentialAction.Cascade);
			});

		_ = migrationBuilder.CreateTable(
			name: "Tokens",
			columns: table => new
			{
				Id = table.Column<Guid>(type: "uuid", nullable: false),
				Name = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
				Code = table.Column<string>(type: "character varying(1000)", maxLength: 1000, nullable: false),
				OrganizationId = table.Column<Guid>(type: "uuid", nullable: false)
			},
			constraints: table =>
			{
				_ = table.PrimaryKey("PK_Tokens", x => x.Id);
				_ = table.ForeignKey(
					name: "FK_Tokens_Organizations_OrganizationId",
					column: x => x.OrganizationId,
					principalTable: "Organizations",
					principalColumn: "Id",
					onDelete: ReferentialAction.Cascade);
			});

		_ = migrationBuilder.CreateTable(
			name: "Users",
			columns: table => new
			{
				Id = table.Column<Guid>(type: "uuid", nullable: false),
				Username = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
				UpperCaseUsername = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
				PasswordHash = table.Column<string>(type: "text", nullable: false),
				RoleId = table.Column<Guid>(type: "uuid", nullable: false)
			},
			constraints: table =>
			{
				_ = table.PrimaryKey("PK_Users", x => x.Id);
				_ = table.ForeignKey(
					name: "FK_Users_Roles_RoleId",
					column: x => x.RoleId,
					principalTable: "Roles",
					principalColumn: "Id");
			});

		_ = migrationBuilder.CreateTable(
			name: "Assets",
			columns: table => new
			{
				Id = table.Column<Guid>(type: "uuid", nullable: false),
				Name = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
				Description = table.Column<string>(type: "character varying(1000)", maxLength: 1000, nullable: false),
				Latitude = table.Column<float>(type: "real", nullable: true),
				Longitude = table.Column<float>(type: "real", nullable: true),
				OrganizationId = table.Column<Guid>(type: "uuid", nullable: false),
				GroupId = table.Column<Guid>(type: "uuid", nullable: true),
				Country = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
				Region = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
				City = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
				Street = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
				House = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
				Building = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
				Apartment = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
				GateCode = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
				IntercomCode = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
				OwnerName = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
				OwnerPassword = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
				OwnerPhone1 = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
				OwnerPhone2 = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
				IdentificationPassword = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
				DuressPassword = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
				IsOnline = table.Column<bool>(type: "boolean", nullable: false)
			},
			constraints: table =>
			{
				_ = table.PrimaryKey("PK_Assets", x => x.Id);
				_ = table.ForeignKey(
					name: "FK_Assets_AssetGroups_GroupId",
					column: x => x.GroupId,
					principalTable: "AssetGroups",
					principalColumn: "Id",
					onDelete: ReferentialAction.SetNull);
				_ = table.ForeignKey(
					name: "FK_Assets_Organizations_OrganizationId",
					column: x => x.OrganizationId,
					principalTable: "Organizations",
					principalColumn: "Id",
					onDelete: ReferentialAction.Cascade);
			});

		_ = migrationBuilder.CreateTable(
			name: "Members",
			columns: table => new
			{
				Id = table.Column<Guid>(type: "uuid", nullable: false),
				UserId = table.Column<Guid>(type: "uuid", nullable: false),
				OrganizationId = table.Column<Guid>(type: "uuid", nullable: false)
			},
			constraints: table =>
			{
				_ = table.PrimaryKey("PK_Members", x => x.Id);
				_ = table.ForeignKey(
					name: "FK_Members_Organizations_OrganizationId",
					column: x => x.OrganizationId,
					principalTable: "Organizations",
					principalColumn: "Id",
					onDelete: ReferentialAction.Cascade);
				_ = table.ForeignKey(
					name: "FK_Members_Users_UserId",
					column: x => x.UserId,
					principalTable: "Users",
					principalColumn: "Id",
					onDelete: ReferentialAction.Cascade);
			});

		_ = migrationBuilder.CreateTable(
			name: "Areas",
			columns: table => new
			{
				Id = table.Column<Guid>(type: "uuid", nullable: false),
				Index = table.Column<int>(type: "integer", nullable: false, defaultValue: 0),
				Name = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
				Description = table.Column<string>(type: "character varying(1000)", maxLength: 1000, nullable: false),
				AssetId = table.Column<Guid>(type: "uuid", nullable: false)
			},
			constraints: table =>
			{
				_ = table.PrimaryKey("PK_Areas", x => x.Id);
				_ = table.ForeignKey(
					name: "FK_Areas_Assets_AssetId",
					column: x => x.AssetId,
					principalTable: "Assets",
					principalColumn: "Id",
					onDelete: ReferentialAction.Cascade);
			});

		_ = migrationBuilder.CreateTable(
			name: "Events",
			columns: table => new
			{
				Id = table.Column<Guid>(type: "uuid", nullable: false),
				Code = table.Column<int>(type: "integer", nullable: false),
				Message = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
				CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
				IsAlarm = table.Column<bool>(type: "boolean", nullable: true),
				AreaIndex = table.Column<int>(type: "integer", nullable: true),
				ZoneIndex = table.Column<int>(type: "integer", nullable: true),
				OrganizationId = table.Column<Guid>(type: "uuid", nullable: false),
				AssetId = table.Column<Guid>(type: "uuid", nullable: false),
				UserId = table.Column<Guid>(type: "uuid", nullable: true)
			},
			constraints: table =>
			{
				_ = table.PrimaryKey("PK_Events", x => x.Id);
				_ = table.ForeignKey(
					name: "FK_Events_Assets_AssetId",
					column: x => x.AssetId,
					principalTable: "Assets",
					principalColumn: "Id",
					onDelete: ReferentialAction.Cascade);
				_ = table.ForeignKey(
					name: "FK_Events_Organizations_OrganizationId",
					column: x => x.OrganizationId,
					principalTable: "Organizations",
					principalColumn: "Id",
					onDelete: ReferentialAction.Cascade);
				_ = table.ForeignKey(
					name: "FK_Events_Users_UserId",
					column: x => x.UserId,
					principalTable: "Users",
					principalColumn: "Id",
					onDelete: ReferentialAction.SetNull);
			});

		_ = migrationBuilder.CreateTable(
			name: "Photos",
			columns: table => new
			{
				Id = table.Column<Guid>(type: "uuid", nullable: false),
				Name = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
				Description = table.Column<string>(type: "character varying(1000)", maxLength: 1000, nullable: false),
				Base64 = table.Column<string>(type: "text", nullable: false),
				AssetId = table.Column<Guid>(type: "uuid", nullable: false)
			},
			constraints: table =>
			{
				_ = table.PrimaryKey("PK_Photos", x => x.Id);
				_ = table.ForeignKey(
					name: "FK_Photos_Assets_AssetId",
					column: x => x.AssetId,
					principalTable: "Assets",
					principalColumn: "Id",
					onDelete: ReferentialAction.Cascade);
			});

		_ = migrationBuilder.CreateTable(
			name: "ResponsiblePersons",
			columns: table => new
			{
				Id = table.Column<Guid>(type: "uuid", nullable: false),
				Name = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
				Key = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
				Phone1 = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
				Phone2 = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
				Note = table.Column<string>(type: "character varying(1000)", maxLength: 1000, nullable: false),
				AssetId = table.Column<Guid>(type: "uuid", nullable: false)
			},
			constraints: table =>
			{
				_ = table.PrimaryKey("PK_ResponsiblePersons", x => x.Id);
				_ = table.ForeignKey(
					name: "FK_ResponsiblePersons_Assets_AssetId",
					column: x => x.AssetId,
					principalTable: "Assets",
					principalColumn: "Id",
					onDelete: ReferentialAction.Cascade);
			});

		_ = migrationBuilder.CreateTable(
			name: "Zones",
			columns: table => new
			{
				Id = table.Column<Guid>(type: "uuid", nullable: false),
				Index = table.Column<int>(type: "integer", nullable: false, defaultValue: 0),
				Name = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
				Description = table.Column<string>(type: "character varying(1000)", maxLength: 1000, nullable: false),
				AssetId = table.Column<Guid>(type: "uuid", nullable: false),
				AreaId = table.Column<Guid>(type: "uuid", nullable: true)
			},
			constraints: table =>
			{
				_ = table.PrimaryKey("PK_Zones", x => x.Id);
				_ = table.ForeignKey(
					name: "FK_Zones_Areas_AreaId",
					column: x => x.AreaId,
					principalTable: "Areas",
					principalColumn: "Id",
					onDelete: ReferentialAction.SetNull);
				_ = table.ForeignKey(
					name: "FK_Zones_Assets_AssetId",
					column: x => x.AssetId,
					principalTable: "Assets",
					principalColumn: "Id",
					onDelete: ReferentialAction.Cascade);
			});

		_ = migrationBuilder.CreateTable(
			name: "Faults",
			columns: table => new
			{
				Id = table.Column<Guid>(type: "uuid", nullable: false),
				EventId = table.Column<Guid>(type: "uuid", nullable: false)
			},
			constraints: table =>
			{
				_ = table.PrimaryKey("PK_Faults", x => x.Id);
				_ = table.ForeignKey(
					name: "FK_Faults_Events_EventId",
					column: x => x.EventId,
					principalTable: "Events",
					principalColumn: "Id",
					onDelete: ReferentialAction.Cascade);
			});

		_ = migrationBuilder.InsertData(
			table: "Roles",
			columns: new[] { "Id", "Name", "UpperCaseName" },
			values: new object[,]
			{
					{ new Guid("06e83f0a-28f4-470e-be61-b732029406db"), "Manager", "MANAGER" },
					{ new Guid("23b97497-89fa-4778-bbfe-692b5a82eaec"), "Watcher", "WATCHER" },
					{ new Guid("60782519-2571-4153-9010-312463165300"), "Administrator", "ADMINISTRATOR" }
			});

		_ = migrationBuilder.InsertData(
			table: "Users",
			columns: new[] { "Id", "PasswordHash", "RoleId", "UpperCaseUsername", "Username" },
			values: new object[] { new Guid("e9bf7e86-fbe3-4c28-b6cc-b27dc62684df"), "$2a$11$KqE5dEUpwM1gV6l3UBT3euaWU9DWR/4GPtuaddS/7rO/JMU9raFbO", new Guid("60782519-2571-4153-9010-312463165300"), "ADMIN", "admin" });

		_ = migrationBuilder.CreateIndex(
			name: "IX_Areas_AssetId_Index",
			table: "Areas",
			columns: new[] { "AssetId", "Index" },
			unique: true);

		_ = migrationBuilder.CreateIndex(
			name: "IX_AssetGroups_OrganizationId",
			table: "AssetGroups",
			column: "OrganizationId");

		_ = migrationBuilder.CreateIndex(
			name: "IX_Assets_GroupId",
			table: "Assets",
			column: "GroupId");

		_ = migrationBuilder.CreateIndex(
			name: "IX_Assets_OrganizationId",
			table: "Assets",
			column: "OrganizationId");

		_ = migrationBuilder.CreateIndex(
			name: "IX_Events_AssetId",
			table: "Events",
			column: "AssetId");

		_ = migrationBuilder.CreateIndex(
			name: "IX_Events_OrganizationId",
			table: "Events",
			column: "OrganizationId");

		_ = migrationBuilder.CreateIndex(
			name: "IX_Events_UserId",
			table: "Events",
			column: "UserId");

		_ = migrationBuilder.CreateIndex(
			name: "IX_Faults_EventId",
			table: "Faults",
			column: "EventId");

		_ = migrationBuilder.CreateIndex(
			name: "IX_Members_OrganizationId_UserId",
			table: "Members",
			columns: new[] { "OrganizationId", "UserId" },
			unique: true);

		_ = migrationBuilder.CreateIndex(
			name: "IX_Members_UserId",
			table: "Members",
			column: "UserId");

		_ = migrationBuilder.CreateIndex(
			name: "IX_Organizations_Name",
			table: "Organizations",
			column: "Name",
			unique: true);

		_ = migrationBuilder.CreateIndex(
			name: "IX_Photos_AssetId",
			table: "Photos",
			column: "AssetId");

		_ = migrationBuilder.CreateIndex(
			name: "IX_ResponsiblePersons_AssetId",
			table: "ResponsiblePersons",
			column: "AssetId");

		_ = migrationBuilder.CreateIndex(
			name: "IX_Tokens_OrganizationId",
			table: "Tokens",
			column: "OrganizationId");

		_ = migrationBuilder.CreateIndex(
			name: "IX_Users_RoleId",
			table: "Users",
			column: "RoleId");

		_ = migrationBuilder.CreateIndex(
			name: "IX_Zones_AreaId",
			table: "Zones",
			column: "AreaId");

		_ = migrationBuilder.CreateIndex(
			name: "IX_Zones_AssetId_Index",
			table: "Zones",
			columns: new[] { "AssetId", "Index" },
			unique: true);
	}

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

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

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

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

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

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

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

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

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

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

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

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

		_ = migrationBuilder.DropTable(
			name: "Organizations");
	}
}
