using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace VigilCareRecordsAPI.Data.Migrations
{
    /// <inheritdoc />
    public partial class AddAlertThresholdsAndClinicalAlerts : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "alert_thresholds",
                schema: "clinical",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
                    observation_code = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false),
                    display_name = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
                    unit = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false),
                    critical_low = table.Column<decimal>(type: "numeric(10,3)", nullable: true),
                    warning_low = table.Column<decimal>(type: "numeric(10,3)", nullable: true),
                    warning_high = table.Column<decimal>(type: "numeric(10,3)", nullable: true),
                    critical_high = table.Column<decimal>(type: "numeric(10,3)", nullable: true),
                    suppression_window_minutes = table.Column<int>(type: "integer", nullable: true),
                    created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "NOW()")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_alert_thresholds", x => x.id);
                });

            migrationBuilder.CreateTable(
                name: "clinical_alerts",
                schema: "clinical",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
                    encounter_id = table.Column<Guid>(type: "uuid", nullable: false),
                    patient_id = table.Column<Guid>(type: "uuid", nullable: false),
                    observation_id = table.Column<Guid>(type: "uuid", nullable: true),
                    alert_type = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false),
                    severity = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false),
                    details = table.Column<string>(type: "text", nullable: false),
                    observation_code = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: true),
                    status = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false, defaultValueSql: "'OPEN'"),
                    acknowledged_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
                    acknowledged_by = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: true),
                    resolved_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
                    triggered_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "NOW()"),
                    client_alert_id = table.Column<Guid>(type: "uuid", nullable: true),
                    synced_from_gateway = table.Column<bool>(type: "boolean", nullable: false, defaultValue: false),
                    feedback_received = table.Column<bool>(type: "boolean", nullable: false, defaultValue: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_clinical_alerts", x => x.id);
                    table.CheckConstraint("chk_clinical_alerts_alert_type", "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', 'CRITICAL_PAO2_MMHG', 'CRITICAL_PLATELET_K_UL', 'CRITICAL_BILIRUBIN_MG_DL', 'CRITICAL_CREATININE_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', 'WARNING_PAO2_MMHG', 'WARNING_PLATELET_K_UL', 'WARNING_BILIRUBIN_MG_DL', 'WARNING_CREATININE_MG_DL', 'NEWS2_WARNING', 'NEWS2_EMERGENCY', 'RAPID_DETERIORATION', 'QSOFA_WARNING', 'QSOFA_SCREEN', 'GCS_CRITICAL', 'GCS_WARNING', 'SOFA_SEPSIS', 'SOFA_WARNING')");
                    table.CheckConstraint("chk_clinical_alerts_severity", "severity IN ('WARNING', 'CRITICAL')");
                    table.CheckConstraint("chk_clinical_alerts_status", "status IN ('OPEN', 'ACKNOWLEDGED', 'RESOLVED', 'ESCALATED')");
                });

            migrationBuilder.CreateIndex(
                name: "IX_alert_thresholds_observation_code",
                schema: "clinical",
                table: "alert_thresholds",
                column: "observation_code",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_clinical_alerts_client_alert_id",
                schema: "clinical",
                table: "clinical_alerts",
                column: "client_alert_id",
                unique: true,
                filter: "client_alert_id IS NOT NULL");

            migrationBuilder.CreateIndex(
                name: "IX_clinical_alerts_encounter_id_alert_type_observation_code",
                schema: "clinical",
                table: "clinical_alerts",
                columns: new[] { "encounter_id", "alert_type", "observation_code" },
                filter: "status IN ('OPEN', 'ESCALATED')");

            migrationBuilder.CreateIndex(
                name: "IX_clinical_alerts_encounter_id_triggered_at",
                schema: "clinical",
                table: "clinical_alerts",
                columns: new[] { "encounter_id", "triggered_at" });

            migrationBuilder.CreateIndex(
                name: "IX_clinical_alerts_patient_id_triggered_at",
                schema: "clinical",
                table: "clinical_alerts",
                columns: new[] { "patient_id", "triggered_at" });

            migrationBuilder.CreateIndex(
                name: "IX_clinical_alerts_severity_triggered_at",
                schema: "clinical",
                table: "clinical_alerts",
                columns: new[] { "severity", "triggered_at" },
                filter: "status = 'OPEN'");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "alert_thresholds",
                schema: "clinical");

            migrationBuilder.DropTable(
                name: "clinical_alerts",
                schema: "clinical");
        }
    }
}
