using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace VigilCareClinicalAPI.Migrations
{
    /// <inheritdoc />
    public partial class AddGcsScores : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "gcs_scores",
                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),
                    eye_score = table.Column<int>(type: "integer", nullable: false),
                    verbal_score = table.Column<int>(type: "integer", nullable: false),
                    motor_score = table.Column<int>(type: "integer", nullable: false),
                    total_score = table.Column<int>(type: "integer", nullable: false),
                    classification = table.Column<string>(type: "character varying(16)", maxLength: 16, nullable: false),
                    calculated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
                    created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_gcs_scores", x => x.id);
                    table.CheckConstraint("chk_gcs_scores_classification", "classification IN ('MILD', 'MODERATE', 'SEVERE')");
                    table.ForeignKey(
                        name: "FK_gcs_scores_encounters_encounter_id",
                        column: x => x.encounter_id,
                        principalTable: "encounters",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateIndex(
                name: "IX_gcs_scores_encounter_id_calculated_at",
                table: "gcs_scores",
                columns: new[] { "encounter_id", "calculated_at" });

            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',
                        'GCS_CRITICAL', 'GCS_WARNING'
                    ));
                """);
        }

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