using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace OmsorgCore.Infrastructure.Persistence.Migrations
{
    /// <inheritdoc />
    public partial class AddOrderDetailsAndConfigurableStatus : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.RenameColumn(
                name: "Status",
                table: "orders",
                newName: "Priority");

            migrationBuilder.AlterColumn<string>(
                name: "RequiredQualification",
                table: "orders",
                type: "character varying(200)",
                maxLength: 200,
                nullable: true,
                oldClrType: typeof(string),
                oldType: "text",
                oldNullable: true);

            migrationBuilder.AddColumn<string>(
                name: "Conditions",
                table: "orders",
                type: "character varying(500)",
                maxLength: 500,
                nullable: true);

            migrationBuilder.AddColumn<Guid>(
                name: "FacilityContactId",
                table: "orders",
                type: "uuid",
                nullable: true);

            migrationBuilder.AddColumn<int>(
                name: "RequiredHeadcount",
                table: "orders",
                type: "integer",
                nullable: false,
                defaultValue: 0);

            migrationBuilder.AddColumn<string>(
                name: "ShiftType",
                table: "orders",
                type: "character varying(100)",
                maxLength: 100,
                nullable: true);

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

            migrationBuilder.CreateTable(
                name: "order_status_definitions",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    Name = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false),
                    SortOrder = table.Column<int>(type: "integer", nullable: false),
                    IsInitial = table.Column<bool>(type: "boolean", nullable: false),
                    IsTerminal = table.Column<bool>(type: "boolean", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_order_status_definitions", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "order_status_transitions",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    FromStatusId = table.Column<Guid>(type: "uuid", nullable: false),
                    ToStatusId = table.Column<Guid>(type: "uuid", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_order_status_transitions", x => x.Id);
                    table.ForeignKey(
                        name: "FK_order_status_transitions_order_status_definitions_FromStatu~",
                        column: x => x.FromStatusId,
                        principalTable: "order_status_definitions",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_order_status_transitions_order_status_definitions_ToStatusId",
                        column: x => x.ToStatusId,
                        principalTable: "order_status_definitions",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateIndex(
                name: "IX_orders_FacilityContactId",
                table: "orders",
                column: "FacilityContactId");

            migrationBuilder.CreateIndex(
                name: "IX_orders_StatusId",
                table: "orders",
                column: "StatusId");

            migrationBuilder.CreateIndex(
                name: "IX_order_status_definitions_Name",
                table: "order_status_definitions",
                column: "Name",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_order_status_transitions_FromStatusId_ToStatusId",
                table: "order_status_transitions",
                columns: new[] { "FromStatusId", "ToStatusId" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_order_status_transitions_ToStatusId",
                table: "order_status_transitions",
                column: "ToStatusId");

            migrationBuilder.AddForeignKey(
                name: "FK_orders_facility_contacts_FacilityContactId",
                table: "orders",
                column: "FacilityContactId",
                principalTable: "facility_contacts",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);

            migrationBuilder.AddForeignKey(
                name: "FK_orders_order_status_definitions_StatusId",
                table: "orders",
                column: "StatusId",
                principalTable: "order_status_definitions",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);
        }

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

            migrationBuilder.DropForeignKey(
                name: "FK_orders_order_status_definitions_StatusId",
                table: "orders");

            migrationBuilder.DropTable(
                name: "order_status_transitions");

            migrationBuilder.DropTable(
                name: "order_status_definitions");

            migrationBuilder.DropIndex(
                name: "IX_orders_FacilityContactId",
                table: "orders");

            migrationBuilder.DropIndex(
                name: "IX_orders_StatusId",
                table: "orders");

            migrationBuilder.DropColumn(
                name: "Conditions",
                table: "orders");

            migrationBuilder.DropColumn(
                name: "FacilityContactId",
                table: "orders");

            migrationBuilder.DropColumn(
                name: "RequiredHeadcount",
                table: "orders");

            migrationBuilder.DropColumn(
                name: "ShiftType",
                table: "orders");

            migrationBuilder.DropColumn(
                name: "StatusId",
                table: "orders");

            migrationBuilder.RenameColumn(
                name: "Priority",
                table: "orders",
                newName: "Status");

            migrationBuilder.AlterColumn<string>(
                name: "RequiredQualification",
                table: "orders",
                type: "text",
                nullable: true,
                oldClrType: typeof(string),
                oldType: "character varying(200)",
                oldMaxLength: 200,
                oldNullable: true);
        }
    }
}
