using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace VigilCareClinicalAPI.Migrations
{
    /// <inheritdoc />
    public partial class AddQsofaWarningAlertType : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.Sql("""
                ALTER TABLE clinical_alerts DROP CONSTRAINT chk_clinical_alerts_alert_type;
                ALTER TABLE clinical_alerts ADD CONSTRAINT chk_clinical_alerts_alert_type
                    CHECK (alert_type IN (
                        'SEPSIS_WARNING',
                        'CRITICAL_HEART_RATE', 'CRITICAL_TEMP_C', 'CRITICAL_POTASSIUM_MEQ_L',
                        'CRITICAL_SPO2', 'CRITICAL_RESP_RATE', 'CRITICAL_WBC_K_UL',
                        'CRITICAL_SYSTOLIC_BP', 'CRITICAL_DIASTOLIC_BP', 'CRITICAL_LACTATE_MMOL_L',
                        'CRITICAL_AVPU', 'CRITICAL_GLUCOSE_MG_DL',
                        'WARNING_HEART_RATE', 'WARNING_TEMP_C', 'WARNING_POTASSIUM_MEQ_L',
                        'WARNING_SPO2', 'WARNING_RESP_RATE', 'WARNING_WBC_K_UL',
                        'WARNING_SYSTOLIC_BP', 'WARNING_DIASTOLIC_BP', 'WARNING_LACTATE_MMOL_L',
                        'WARNING_GLUCOSE_MG_DL', 'NEWS2_WARNING', 'NEWS2_EMERGENCY',
                        'RAPID_DETERIORATION', 'QSOFA_WARNING'
                    ));
                """);

            migrationBuilder.CreateTable(
                name: "sepsis_bundles",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
                    encounter_id = table.Column<Guid>(type: "uuid", nullable: false),
                    triggering_alert_id = table.Column<Guid>(type: "uuid", nullable: false),
                    triggering_alert_type = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false),
                    recognized_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
                    deadline_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
                    compliance_status = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false, defaultValueSql: "'IN_PROGRESS'"),
                    completed_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_sepsis_bundles", x => x.id);
                    table.CheckConstraint("chk_sepsis_bundles_compliance_status", "compliance_status IN ('IN_PROGRESS', 'COMPLIANT', 'NON_COMPLIANT')");
                    table.ForeignKey(
                        name: "FK_sepsis_bundles_clinical_alerts_triggering_alert_id",
                        column: x => x.triggering_alert_id,
                        principalTable: "clinical_alerts",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_sepsis_bundles_encounters_encounter_id",
                        column: x => x.encounter_id,
                        principalTable: "encounters",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "sepsis_bundle_elements",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
                    bundle_id = table.Column<Guid>(type: "uuid", nullable: false),
                    element_type = table.Column<string>(type: "character varying(40)", maxLength: 40, nullable: false),
                    status = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false, defaultValueSql: "'PENDING'"),
                    order_id = table.Column<Guid>(type: "uuid", nullable: true),
                    completed_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_sepsis_bundle_elements", x => x.id);
                    table.CheckConstraint("chk_sepsis_bundle_elements_element_type", "element_type IN ('BLOOD_CULTURES', 'SERUM_LACTATE', 'BROAD_SPECTRUM_ANTIBIOTICS', 'IV_FLUID_RESUSCITATION')");
                    table.CheckConstraint("chk_sepsis_bundle_elements_status", "status IN ('PENDING', 'COMPLETED')");
                    table.ForeignKey(
                        name: "FK_sepsis_bundle_elements_orders_order_id",
                        column: x => x.order_id,
                        principalTable: "orders",
                        principalColumn: "id",
                        onDelete: ReferentialAction.SetNull);
                    table.ForeignKey(
                        name: "FK_sepsis_bundle_elements_sepsis_bundles_bundle_id",
                        column: x => x.bundle_id,
                        principalTable: "sepsis_bundles",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateIndex(
                name: "IX_sepsis_bundle_elements_bundle_id_element_type",
                table: "sepsis_bundle_elements",
                columns: new[] { "bundle_id", "element_type" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_sepsis_bundle_elements_order_id",
                table: "sepsis_bundle_elements",
                column: "order_id");

            migrationBuilder.CreateIndex(
                name: "IX_sepsis_bundles_compliance_status",
                table: "sepsis_bundles",
                column: "compliance_status");

            migrationBuilder.CreateIndex(
                name: "IX_sepsis_bundles_encounter_id_recognized_at",
                table: "sepsis_bundles",
                columns: new[] { "encounter_id", "recognized_at" });

            migrationBuilder.CreateIndex(
                name: "IX_sepsis_bundles_triggering_alert_id",
                table: "sepsis_bundles",
                column: "triggering_alert_id");
        }

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

            migrationBuilder.DropTable(
                name: "sepsis_bundles");
        }
    }
}
