-- Migration: schema_update
-- Generated by: rem db diff --generate
-- Changes detected: 139
--
-- Review this file before applying!
-- Apply with: rem db migrate
--

CREATE TABLE IF NOT EXISTS patient_profiles (
    id UUID NOT NULL,
    tenant_id VARCHAR(100) NOT NULL,
    user_id VARCHAR(256),
    patient_ref VARCHAR(256) NOT NULL,
    clinician_ref VARCHAR(256),
    evaluation_type TEXT,
    evaluation_date DATE,
    session_ref VARCHAR(256),
    risk_level TEXT,
    suicidality TEXT,
    suicidality_details VARCHAR(256),
    homicidal_ideation BOOLEAN,
    homicidal_details VARCHAR(256),
    self_harm_current BOOLEAN,
    self_harm_history BOOLEAN,
    suicide_attempts_lifetime INTEGER,
    suicide_attempt_most_recent VARCHAR(256),
    safety_plan_in_place BOOLEAN,
    safety_plan_date DATE,
    appearance VARCHAR(256),
    behavior VARCHAR(256),
    speech VARCHAR(256),
    mood_states JSONB,
    mood_description TEXT,
    affect_quality TEXT,
    affect_congruent BOOLEAN,
    thought_process TEXT,
    delusions_present BOOLEAN,
    delusion_types TEXT[],
    hallucinations_present BOOLEAN,
    hallucination_types TEXT[],
    command_hallucinations BOOLEAN,
    oriented BOOLEAN,
    orientation_deficits TEXT[],
    memory_intact BOOLEAN,
    attention_intact BOOLEAN,
    insight TEXT,
    judgment TEXT,
    symptoms JSONB,
    chief_complaint VARCHAR(256),
    symptom_duration VARCHAR(256),
    symptom_trajectory VARCHAR(256),
    precipitating_factors TEXT[],
    substance_use_profiles JSONB,
    substance_use_summary TEXT,
    in_recovery BOOLEAN,
    recovery_duration VARCHAR(256),
    mat_current BOOLEAN,
    diagnoses_current TEXT[],
    diagnoses_historical TEXT[],
    first_psychiatric_contact_age INTEGER,
    hospitalizations_psychiatric INTEGER,
    last_hospitalization VARCHAR(256),
    ect_history BOOLEAN,
    therapy_history VARCHAR(256),
    family_psychiatric_history VARCHAR(256),
    current_medications JSONB,
    other_medications TEXT[],
    allergies TEXT[],
    current_therapist BOOLEAN,
    therapy_type VARCHAR(256),
    therapy_frequency VARCHAR(256),
    other_providers TEXT[],
    functioning JSONB,
    social_determinants JSONB,
    cgi_severity TEXT,
    cgi_improvement TEXT,
    gaf_equivalent INTEGER,
    clinical_impression VARCHAR(256),
    differential_diagnoses TEXT[],
    treatment_plan VARCHAR(256),
    treatment_goals TEXT[],
    barriers_to_treatment TEXT[],
    strengths TEXT[],
    follow_up_recommended BOOLEAN,
    follow_up_urgency VARCHAR(256),
    follow_up_interval VARCHAR(256),
    referrals TEXT[],
    labs_ordered TEXT[],
    data_source VARCHAR(256),
    confidence_score FLOAT,
    reviewed_by_clinician BOOLEAN,
    notes VARCHAR(256),
    created_at TIMESTAMP WITHOUT TIME ZONE,
    updated_at TIMESTAMP WITHOUT TIME ZONE,
    deleted_at TIMESTAMP WITHOUT TIME ZONE,
    graph_edges JSONB,
    metadata JSONB,
    tags TEXT[]
);
-- Changes to table: patient_profiles
CREATE INDEX IF NOT EXISTS idx_patient_profiles_graph_edges ON patient_profiles (graph_edges);
CREATE INDEX IF NOT EXISTS idx_patient_profiles_metadata ON patient_profiles (metadata);
CREATE INDEX IF NOT EXISTS idx_patient_profiles_tags ON patient_profiles (tags);
CREATE INDEX IF NOT EXISTS idx_patient_profiles_tenant ON patient_profiles (tenant_id);
CREATE INDEX IF NOT EXISTS idx_patient_profiles_user ON patient_profiles (user_id);
CREATE TABLE IF NOT EXISTS subscribers (
    id UUID NOT NULL,
    tenant_id VARCHAR(100) NOT NULL,
    user_id VARCHAR(256),
    email TEXT NOT NULL,
    name VARCHAR(256),
    comment TEXT,
    status TEXT,
    origin TEXT,
    origin_detail VARCHAR(256),
    subscribed_at TIMESTAMP WITHOUT TIME ZONE,
    unsubscribed_at TIMESTAMP WITHOUT TIME ZONE,
    ip_address VARCHAR(256),
    user_agent VARCHAR(256),
    created_at TIMESTAMP WITHOUT TIME ZONE,
    updated_at TIMESTAMP WITHOUT TIME ZONE,
    deleted_at TIMESTAMP WITHOUT TIME ZONE,
    graph_edges JSONB,
    metadata JSONB,
    tags TEXT[]
);
-- Changes to table: subscribers
CREATE INDEX IF NOT EXISTS idx_subscribers_graph_edges ON subscribers (graph_edges);
CREATE INDEX IF NOT EXISTS idx_subscribers_metadata ON subscribers (metadata);
CREATE INDEX IF NOT EXISTS idx_subscribers_tags ON subscribers (tags);
CREATE INDEX IF NOT EXISTS idx_subscribers_tenant ON subscribers (tenant_id);
CREATE INDEX IF NOT EXISTS idx_subscribers_user ON subscribers (user_id);
CREATE TABLE IF NOT EXISTS embeddings_patient_profiles (
    id UUID NOT NULL,
    entity_id UUID NOT NULL,
    field_name VARCHAR(100) NOT NULL,
    provider VARCHAR(50) NOT NULL,
    model VARCHAR(100) NOT NULL,
    embedding FLOAT[] NOT NULL,
    created_at TIMESTAMP WITHOUT TIME ZONE,
    updated_at TIMESTAMP WITHOUT TIME ZONE
);
-- Changes to table: embeddings_patient_profiles
CREATE INDEX IF NOT EXISTS idx_embeddings_patient_profiles_entity ON embeddings_patient_profiles (entity_id);
CREATE INDEX IF NOT EXISTS idx_embeddings_patient_profiles_field_provider ON embeddings_patient_profiles (field_name, provider);
