using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace VigilCareRecordsAPI.Data.Migrations
{
    /// <inheritdoc />
    public partial class InitialCreate : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "users",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
                    username = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
                    password_hash = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
                    full_name = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
                    role = table.Column<string>(type: "character varying(30)", maxLength: 30, nullable: false),
                    is_active = table.Column<bool>(type: "boolean", nullable: false, defaultValue: true),
                    created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "NOW()")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_users", x => x.id);
                    table.CheckConstraint("chk_users_role", "role IN ('INTAKE_CLERK', 'DATA_ENTRY_CLERK', 'VERIFIER', 'CLINICAL_APPROVER', 'CLINICIAN', 'ADMINISTRATOR')");
                });

            migrationBuilder.CreateTable(
                name: "digitization_batches",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
                    status = table.Column<string>(type: "character varying(30)", maxLength: 30, nullable: false, defaultValueSql: "'UPLOADED'"),
                    batch_type = table.Column<string>(type: "character varying(30)", maxLength: 30, nullable: false),
                    track = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false, defaultValueSql: "'BACKFILL'"),
                    patient_id = table.Column<Guid>(type: "uuid", nullable: true),
                    encounter_draft_id = table.Column<Guid>(type: "uuid", nullable: true),
                    document_ref = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: false),
                    document_sha256 = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false),
                    enable_retroactive_alerts = table.Column<bool>(type: "boolean", nullable: false, defaultValue: false),
                    entered_by_user_id = table.Column<Guid>(type: "uuid", nullable: true),
                    verified_by_user_id = table.Column<Guid>(type: "uuid", nullable: true),
                    approved_by_user_id = table.Column<Guid>(type: "uuid", nullable: true),
                    rejection_reason = table.Column<string>(type: "text", nullable: true),
                    promoted_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
                    promotion_encounter_id = table.Column<Guid>(type: "uuid", nullable: true),
                    supersedes_batch_id = table.Column<Guid>(type: "uuid", nullable: true),
                    clinician_attestation = table.Column<bool>(type: "boolean", nullable: false, defaultValue: false),
                    created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "NOW()"),
                    updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "NOW()")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_digitization_batches", x => x.id);
                    table.CheckConstraint("chk_batches_batch_type", "batch_type IN ('PATIENT_REGISTRATION', 'ENCOUNTER_SUMMARY', 'VITALS_SHEET', 'LAB_RESULTS', 'MEDICATION_LIST', 'ALLERGY_UPDATE', 'MIXED')");
                    table.CheckConstraint("chk_batches_status", "status IN ('UPLOADED', 'IN_ENTRY', 'PENDING_VERIFICATION', 'REJECTED', 'VERIFIED', 'AWAITING_CLINICAL_APPROVAL', 'APPROVED', 'PROMOTED')");
                    table.CheckConstraint("chk_batches_track", "track IN ('BACKFILL', 'LIVE_CAPTURE')");
                    table.ForeignKey(
                        name: "FK_digitization_batches_users_approved_by_user_id",
                        column: x => x.approved_by_user_id,
                        principalTable: "users",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_digitization_batches_users_entered_by_user_id",
                        column: x => x.entered_by_user_id,
                        principalTable: "users",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_digitization_batches_users_verified_by_user_id",
                        column: x => x.verified_by_user_id,
                        principalTable: "users",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "digitization_events",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
                    batch_id = table.Column<Guid>(type: "uuid", nullable: false),
                    event_type = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false),
                    actor_user_id = table.Column<Guid>(type: "uuid", nullable: false),
                    occurred_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "NOW()"),
                    metadata_json = table.Column<string>(type: "jsonb", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_digitization_events", x => x.id);
                    table.CheckConstraint("chk_digitization_events_event_type", "event_type IN ('uploaded', 'entry_started', 'submitted_for_verification', 'rejected', 'verified', 'verified_pending_clinical', 'awaiting_clinical_approval', 'approved', 'promoted', 'live_capture_attested', 'correction_requested', 'verification_failed', 'superseded', 'correction_promoted', 'correction_uploaded', 'promotion_retry_succeeded', 'promotion_retry_failed', 'promotion_retry_exhausted', 'promotion_failed')");
                    table.ForeignKey(
                        name: "FK_digitization_events_digitization_batches_batch_id",
                        column: x => x.batch_id,
                        principalTable: "digitization_batches",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_digitization_events_users_actor_user_id",
                        column: x => x.actor_user_id,
                        principalTable: "users",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "draft_encounters",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
                    batch_id = table.Column<Guid>(type: "uuid", nullable: false),
                    admission_date = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
                    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: true),
                    created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "NOW()"),
                    updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "NOW()")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_draft_encounters", x => x.id);
                    table.CheckConstraint("chk_draft_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_draft_encounters_digitization_batches_batch_id",
                        column: x => x.batch_id,
                        principalTable: "digitization_batches",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "draft_observations",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
                    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()")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_draft_observations", x => x.id);
                    table.ForeignKey(
                        name: "FK_draft_observations_digitization_batches_batch_id",
                        column: x => x.batch_id,
                        principalTable: "digitization_batches",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "draft_patients",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
                    batch_id = table.Column<Guid>(type: "uuid", nullable: false),
                    full_name = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: true),
                    date_of_birth = table.Column<DateOnly>(type: "date", nullable: true),
                    sex = table.Column<string>(type: "character varying(10)", maxLength: 10, nullable: true),
                    blood_type = table.Column<string>(type: "character varying(10)", maxLength: 10, nullable: true),
                    emergency_contact = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: true),
                    allergies_json = table.Column<string>(type: "jsonb", nullable: true),
                    no_known_allergies = table.Column<bool>(type: "boolean", nullable: false, defaultValue: false),
                    medications_json = table.Column<string>(type: "jsonb", nullable: true),
                    no_active_medications = table.Column<bool>(type: "boolean", nullable: false, defaultValue: false),
                    created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "NOW()"),
                    updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "NOW()")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_draft_patients", x => x.id);
                    table.CheckConstraint("chk_draft_patients_blood_type", "blood_type IS NULL OR blood_type IN ('A+', 'A-', 'B+', 'B-', 'AB+', 'AB-', 'O+', 'O-')");
                    table.ForeignKey(
                        name: "FK_draft_patients_digitization_batches_batch_id",
                        column: x => x.batch_id,
                        principalTable: "digitization_batches",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "scanned_documents",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
                    batch_id = table.Column<Guid>(type: "uuid", nullable: false),
                    object_key = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: false),
                    sha256 = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false),
                    content_type = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
                    file_size_bytes = table.Column<long>(type: "bigint", nullable: false),
                    uploaded_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "NOW()")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_scanned_documents", x => x.id);
                    table.ForeignKey(
                        name: "FK_scanned_documents_digitization_batches_batch_id",
                        column: x => x.batch_id,
                        principalTable: "digitization_batches",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateIndex(
                name: "IX_digitization_batches_approved_by_user_id",
                table: "digitization_batches",
                column: "approved_by_user_id");

            migrationBuilder.CreateIndex(
                name: "IX_digitization_batches_document_sha256_patient_id_created_at",
                table: "digitization_batches",
                columns: new[] { "document_sha256", "patient_id", "created_at" });

            migrationBuilder.CreateIndex(
                name: "IX_digitization_batches_entered_by_user_id",
                table: "digitization_batches",
                column: "entered_by_user_id");

            migrationBuilder.CreateIndex(
                name: "IX_digitization_batches_status",
                table: "digitization_batches",
                column: "status");

            migrationBuilder.CreateIndex(
                name: "IX_digitization_batches_supersedes_batch_id",
                table: "digitization_batches",
                column: "supersedes_batch_id",
                filter: "supersedes_batch_id IS NOT NULL");

            migrationBuilder.CreateIndex(
                name: "IX_digitization_batches_verified_by_user_id",
                table: "digitization_batches",
                column: "verified_by_user_id");

            migrationBuilder.CreateIndex(
                name: "IX_digitization_events_actor_user_id",
                table: "digitization_events",
                column: "actor_user_id");

            migrationBuilder.CreateIndex(
                name: "IX_digitization_events_batch_id_occurred_at",
                table: "digitization_events",
                columns: new[] { "batch_id", "occurred_at" });

            migrationBuilder.CreateIndex(
                name: "IX_draft_encounters_batch_id",
                table: "draft_encounters",
                column: "batch_id",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_draft_observations_batch_id_observation_code",
                table: "draft_observations",
                columns: new[] { "batch_id", "observation_code" });

            migrationBuilder.CreateIndex(
                name: "IX_draft_patients_batch_id",
                table: "draft_patients",
                column: "batch_id",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_scanned_documents_batch_id",
                table: "scanned_documents",
                column: "batch_id",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_scanned_documents_sha256",
                table: "scanned_documents",
                column: "sha256");

            migrationBuilder.CreateIndex(
                name: "IX_users_username",
                table: "users",
                column: "username",
                unique: true);
        }

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

            migrationBuilder.DropTable(
                name: "draft_encounters");

            migrationBuilder.DropTable(
                name: "draft_observations");

            migrationBuilder.DropTable(
                name: "draft_patients");

            migrationBuilder.DropTable(
                name: "scanned_documents");

            migrationBuilder.DropTable(
                name: "digitization_batches");

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