using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace VigilCareRecordsAPI.Data.Migrations
{
    /// <inheritdoc />
    public partial class AddLiveEncountersAndObservations : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "live_encounters",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
                    patient_id = table.Column<Guid>(type: "uuid", nullable: false),
                    admission_date = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
                    department = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: true),
                    room_bed = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: true),
                    admission_reason = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: true),
                    discharge_diagnosis = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: true),
                    status = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false),
                    created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "NOW()")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_live_encounters", x => x.id);
                    table.CheckConstraint("chk_live_encounters_department", "department IS NULL OR department IN ('Emergency Department', 'Internal Medicine', 'General Medicine', 'Surgery', 'ICU', 'NICU', 'Medical-Surgical', 'Outpatient Clinic', 'Pediatrics', 'Obstetrics & Gynecology', 'Labor & Delivery', 'Cardiology', 'Orthopedics', 'Neurology', 'Oncology', 'Radiology', 'Laboratory', 'Psychiatry', 'Physical Therapy', 'Anesthesiology')");
                    table.ForeignKey(
                        name: "FK_live_encounters_patients_patient_id",
                        column: x => x.patient_id,
                        principalSchema: "clinical",
                        principalTable: "patients",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "live_observations",
                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: true),
                    source_batch_id = table.Column<Guid>(type: "uuid", nullable: false),
                    observation_code = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false),
                    value = table.Column<decimal>(type: "numeric(10,3)", nullable: false),
                    unit = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false),
                    recorded_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
                    note = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: true),
                    created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "NOW()"),
                    is_superseded = table.Column<bool>(type: "boolean", nullable: false, defaultValue: false),
                    superseded_by_batch_id = table.Column<Guid>(type: "uuid", nullable: true),
                    superseded_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_live_observations", x => x.id);
                    table.ForeignKey(
                        name: "FK_live_observations_live_encounters_encounter_id",
                        column: x => x.encounter_id,
                        principalTable: "live_encounters",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateIndex(
                name: "IX_live_encounters_patient_id_status",
                table: "live_encounters",
                columns: new[] { "patient_id", "status" });

            migrationBuilder.CreateIndex(
                name: "IX_live_observations_encounter_id_observation_code",
                table: "live_observations",
                columns: new[] { "encounter_id", "observation_code" });

            migrationBuilder.CreateIndex(
                name: "IX_live_observations_is_superseded",
                table: "live_observations",
                column: "is_superseded",
                filter: "is_superseded = true");

            migrationBuilder.CreateIndex(
                name: "IX_live_observations_source_batch_id",
                table: "live_observations",
                column: "source_batch_id");
        }

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

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