using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace VigilCareClinicalAPI.Migrations
{
    /// <inheritdoc />
    public partial class AddOrderStatusAndDepartmentEnums : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropIndex(
                name: "IX_orders_status_ordered_at",
                table: "orders");

            migrationBuilder.Sql("""
                UPDATE orders SET status = 'PENDING'     WHERE status = 'pending';
                UPDATE orders SET status = 'IN_PROGRESS' WHERE status = 'in_progress';
                UPDATE orders SET status = 'RESULTED'    WHERE status = 'resulted';
                UPDATE orders SET status = 'CANCELLED'   WHERE status = 'cancelled';
                """);

            migrationBuilder.Sql("""
                UPDATE encounters SET department = 'GENERAL_MEDICINE' WHERE department = 'General Medicine';
                """);

            migrationBuilder.AlterColumn<string>(
                name: "status",
                table: "orders",
                type: "character varying(20)",
                maxLength: 20,
                nullable: false,
                defaultValueSql: "'PENDING'",
                oldClrType: typeof(string),
                oldType: "character varying(20)",
                oldMaxLength: 20,
                oldDefaultValue: "pending");

            migrationBuilder.CreateIndex(
                name: "IX_orders_status_ordered_at",
                table: "orders",
                columns: new[] { "status", "ordered_at" },
                filter: "status IN ('PENDING', 'IN_PROGRESS')");

            migrationBuilder.AddCheckConstraint(
                name: "chk_orders_status",
                table: "orders",
                sql: "status IN ('PENDING', 'IN_PROGRESS', 'RESULTED', 'CANCELLED')");

            migrationBuilder.AddCheckConstraint(
                name: "chk_encounters_department",
                table: "encounters",
                sql: "department IN ('ICU', 'GENERAL_MEDICINE', 'EMERGENCY', 'CARDIOLOGY', 'SURGERY', 'PEDIATRICS')");
        }

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

            migrationBuilder.DropCheckConstraint(
                name: "chk_orders_status",
                table: "orders");

            migrationBuilder.DropCheckConstraint(
                name: "chk_encounters_department",
                table: "encounters");

            migrationBuilder.Sql("""
                UPDATE orders SET status = 'pending'     WHERE status = 'PENDING';
                UPDATE orders SET status = 'in_progress' WHERE status = 'IN_PROGRESS';
                UPDATE orders SET status = 'resulted'    WHERE status = 'RESULTED';
                UPDATE orders SET status = 'cancelled'   WHERE status = 'CANCELLED';
                """);

            migrationBuilder.Sql("""
                UPDATE encounters SET department = 'General Medicine' WHERE department = 'GENERAL_MEDICINE';
                """);

            migrationBuilder.AlterColumn<string>(
                name: "status",
                table: "orders",
                type: "character varying(20)",
                maxLength: 20,
                nullable: false,
                defaultValue: "pending",
                oldClrType: typeof(string),
                oldType: "character varying(20)",
                oldMaxLength: 20,
                oldDefaultValueSql: "'PENDING'");

            migrationBuilder.CreateIndex(
                name: "IX_orders_status_ordered_at",
                table: "orders",
                columns: new[] { "status", "ordered_at" },
                filter: "status IN ('pending', 'in_progress')");
        }
    }
}
