using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Monitoring.Infrastructure.Data.Migrations;

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

		_ = migrationBuilder.DropForeignKey(
			name: "FK_Assets_AssetGroups_GroupId",
			table: "Assets");

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

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

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

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

		_ = migrationBuilder.RenameColumn(
			name: "AssetId",
			table: "Accesses",
			newName: "AssetGroupId");

		_ = migrationBuilder.RenameIndex(
			name: "IX_Accesses_MemberId_AssetId",
			table: "Accesses",
			newName: "IX_Accesses_MemberId_AssetGroupId");

		_ = migrationBuilder.RenameIndex(
			name: "IX_Accesses_AssetId",
			table: "Accesses",
			newName: "IX_Accesses_AssetGroupId");

		_ = migrationBuilder.AlterColumn<string>(
			name: "Note",
			table: "ResponsiblePersons",
			type: "character varying(1000)",
			maxLength: 1000,
			nullable: false,
			oldClrType: typeof(string),
			oldType: "character varying(100)",
			oldMaxLength: 100);

		_ = migrationBuilder.AlterColumn<string>(
			name: "Description",
			table: "Photos",
			type: "character varying(1000)",
			maxLength: 1000,
			nullable: false,
			oldClrType: typeof(string),
			oldType: "character varying(100)",
			oldMaxLength: 100);

		_ = migrationBuilder.AlterColumn<string>(
			name: "Description",
			table: "Assets",
			type: "character varying(1000)",
			maxLength: 1000,
			nullable: false,
			oldClrType: typeof(string),
			oldType: "character varying(100)",
			oldMaxLength: 100);

		_ = migrationBuilder.AddColumn<Guid>(
			name: "ParentId",
			table: "AssetGroups",
			type: "uuid",
			nullable: true);

		_ = migrationBuilder.CreateTable(
			name: "AssetAssetGroup",
			columns: table => new
			{
				AssetsId = table.Column<Guid>(type: "uuid", nullable: false),
				GroupsId = table.Column<Guid>(type: "uuid", nullable: false)
			},
			constraints: table =>
			{
				_ = table.PrimaryKey("PK_AssetAssetGroup", x => new { x.AssetsId, x.GroupsId });
				_ = table.ForeignKey(
					name: "FK_AssetAssetGroup_AssetGroups_GroupsId",
					column: x => x.GroupsId,
					principalTable: "AssetGroups",
					principalColumn: "Id",
					onDelete: ReferentialAction.Cascade);
				_ = table.ForeignKey(
					name: "FK_AssetAssetGroup_Assets_AssetsId",
					column: x => x.AssetsId,
					principalTable: "Assets",
					principalColumn: "Id",
					onDelete: ReferentialAction.Cascade);
			});

		_ = migrationBuilder.CreateTable(
			name: "UserLogs",
			columns: table => new
			{
				Id = table.Column<Guid>(type: "uuid", nullable: false),
				UserId = table.Column<Guid>(type: "uuid", nullable: false),
				CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
				Information = table.Column<string>(type: "character varying(1000)", maxLength: 1000, nullable: false)
			},
			constraints: table =>
			{
				_ = table.PrimaryKey("PK_UserLogs", x => x.Id);
				_ = table.ForeignKey(
					name: "FK_UserLogs_Users_UserId",
					column: x => x.UserId,
					principalTable: "Users",
					principalColumn: "Id");
			});

		_ = migrationBuilder.CreateIndex(
			name: "IX_AssetGroups_ParentId",
			table: "AssetGroups",
			column: "ParentId");

		_ = migrationBuilder.CreateIndex(
			name: "IX_AssetAssetGroup_GroupsId",
			table: "AssetAssetGroup",
			column: "GroupsId");

		_ = migrationBuilder.CreateIndex(
			name: "IX_UserLogs_CreatedAt",
			table: "UserLogs",
			column: "CreatedAt");

		_ = migrationBuilder.CreateIndex(
			name: "IX_UserLogs_UserId",
			table: "UserLogs",
			column: "UserId");

		_ = migrationBuilder.AddForeignKey(
			name: "FK_Accesses_AssetGroups_AssetGroupId",
			table: "Accesses",
			column: "AssetGroupId",
			principalTable: "AssetGroups",
			principalColumn: "Id",
			onDelete: ReferentialAction.Cascade);

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

		_ = migrationBuilder.AddForeignKey(
			name: "FK_Events_Users_UserId",
			table: "Events",
			column: "UserId",
			principalTable: "Users",
			principalColumn: "Id");
	}

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

		_ = migrationBuilder.DropForeignKey(
			name: "FK_AssetGroups_AssetGroups_ParentId",
			table: "AssetGroups");

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

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

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

		_ = migrationBuilder.DropIndex(
			name: "IX_AssetGroups_ParentId",
			table: "AssetGroups");

		_ = migrationBuilder.DropColumn(
			name: "ParentId",
			table: "AssetGroups");

		_ = migrationBuilder.RenameColumn(
			name: "AssetGroupId",
			table: "Accesses",
			newName: "AssetId");

		_ = migrationBuilder.RenameIndex(
			name: "IX_Accesses_MemberId_AssetGroupId",
			table: "Accesses",
			newName: "IX_Accesses_MemberId_AssetId");

		_ = migrationBuilder.RenameIndex(
			name: "IX_Accesses_AssetGroupId",
			table: "Accesses",
			newName: "IX_Accesses_AssetId");

		_ = migrationBuilder.AlterColumn<string>(
			name: "Note",
			table: "ResponsiblePersons",
			type: "character varying(100)",
			maxLength: 100,
			nullable: false,
			oldClrType: typeof(string),
			oldType: "character varying(1000)",
			oldMaxLength: 1000);

		_ = migrationBuilder.AlterColumn<string>(
			name: "Description",
			table: "Photos",
			type: "character varying(100)",
			maxLength: 100,
			nullable: false,
			oldClrType: typeof(string),
			oldType: "character varying(1000)",
			oldMaxLength: 1000);

		_ = migrationBuilder.AddColumn<string>(
			name: "Message",
			table: "Events",
			type: "character varying(100)",
			maxLength: 100,
			nullable: true);

		_ = migrationBuilder.AlterColumn<string>(
			name: "Description",
			table: "Assets",
			type: "character varying(100)",
			maxLength: 100,
			nullable: false,
			oldClrType: typeof(string),
			oldType: "character varying(1000)",
			oldMaxLength: 1000);

		_ = migrationBuilder.AddColumn<Guid>(
			name: "GroupId",
			table: "Assets",
			type: "uuid",
			nullable: true);

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

		_ = migrationBuilder.AddForeignKey(
			name: "FK_Accesses_Assets_AssetId",
			table: "Accesses",
			column: "AssetId",
			principalTable: "Assets",
			principalColumn: "Id",
			onDelete: ReferentialAction.Cascade);

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

		_ = migrationBuilder.AddForeignKey(
			name: "FK_Events_Users_UserId",
			table: "Events",
			column: "UserId",
			principalTable: "Users",
			principalColumn: "Id",
			onDelete: ReferentialAction.SetNull);
	}
}
