using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Monitoring.Infrastructure.Data.Migrations
{
	/// <inheritdoc />
	public partial class AddServiceOrganizations : Migration
	{
		/// <inheritdoc />
		protected override void Up(MigrationBuilder migrationBuilder)
		{
			migrationBuilder.AddColumn<Guid>(
				name: "ServiceOrganizationId",
				table: "Assets",
				type: "uuid",
				nullable: true
			);

			migrationBuilder.CreateTable(
				name: "ServiceOrganizations",
				columns: table => new
				{
					Id = table.Column<Guid>(type: "uuid", nullable: false),
					Name = table.Column<string>(
						type: "character varying(100)",
						maxLength: 100,
						nullable: false
					),
					CreatedAt = table.Column<DateTime>(
						type: "timestamp with time zone",
						nullable: false
					),
					UpdatedAt = table.Column<DateTime>(
						type: "timestamp with time zone",
						nullable: false
					),
					TimeStamp = table.Column<byte[]>(
						type: "bytea",
						rowVersion: true,
						nullable: false
					),
				},
				constraints: table =>
				{
					table.PrimaryKey("PK_ServiceOrganizations", x => x.Id);
				}
			);

			migrationBuilder.CreateIndex(
				name: "IX_Assets_ServiceOrganizationId",
				table: "Assets",
				column: "ServiceOrganizationId"
			);

			migrationBuilder.CreateIndex(
				name: "IX_ServiceOrganizations_Name",
				table: "ServiceOrganizations",
				column: "Name",
				unique: true
			);

			migrationBuilder.AddForeignKey(
				name: "FK_Assets_ServiceOrganizations_ServiceOrganizationId",
				table: "Assets",
				column: "ServiceOrganizationId",
				principalTable: "ServiceOrganizations",
				principalColumn: "Id",
				onDelete: ReferentialAction.SetNull
			);
		}

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

			migrationBuilder.DropTable(name: "ServiceOrganizations");

			migrationBuilder.DropIndex(name: "IX_Assets_ServiceOrganizationId", table: "Assets");

			migrationBuilder.DropColumn(name: "ServiceOrganizationId", table: "Assets");
		}
	}
}
