CREATE TABLE mtbs_assessments (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    medical_record_id UUID NOT NULL REFERENCES medical_records(id) ON DELETE CASCADE,
    visit_id UUID NOT NULL REFERENCES visits(id) ON DELETE CASCADE,
    
    -- Tanda Bahaya Umum
    tidak_bisa_minum BOOLEAN DEFAULT FALSE,
    memuntahkan_semuanya BOOLEAN DEFAULT FALSE,
    kejang BOOLEAN DEFAULT FALSE,
    gelisah_letargis BOOLEAN DEFAULT FALSE,
    
    -- Gejala Utama
    batuk BOOLEAN DEFAULT FALSE,
    diare BOOLEAN DEFAULT FALSE,
    demam BOOLEAN DEFAULT FALSE,
    masalah_telinga BOOLEAN DEFAULT FALSE,
    
    -- Klasifikasi & Tindakan
    klasifikasi VARCHAR(255),
    tindakan TEXT,
    
    -- Detail Keluhan (Stored as JSONB to be flexible for the complex form)
    keluhan_detail JSONB,
    
    -- Kunjungan Ulang
    kunjungan_ulang_hari INT,
    
    -- Metadata
    created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
    created_by UUID REFERENCES users(id),
    updated_by UUID REFERENCES users(id)
);

CREATE INDEX idx_mtbs_assessments_medical_record ON mtbs_assessments(medical_record_id);
CREATE INDEX idx_mtbs_assessments_visit ON mtbs_assessments(visit_id);

CREATE TRIGGER update_mtbs_assessments_updated_at
    BEFORE UPDATE ON mtbs_assessments
    FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
