#!/usr/bin/env python3
"""Convert the Wheel of Wellness Question Bank to a polished DOCX."""

from docx import Document
from docx.shared import Pt, Cm, RGBColor
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.enum.table import WD_TABLE_ALIGNMENT
from docx.oxml.ns import qn
from docx.oxml import OxmlElement

# ── Colors ──────────────────────────────────────────────────
TEAL       = '2E7D6F'
CORAL      = 'E8835C'
DARK       = '2D3436'
ALT_ROW    = 'F7F7F7'

# ── Helpers (same pattern as build_proposal_docx.py) ────────
def set_cell_shading(cell, color):
    shading = OxmlElement('w:shd')
    shading.set(qn('w:fill'), color)
    shading.set(qn('w:val'), 'clear')
    cell._tc.get_or_add_tcPr().append(shading)

def styled_table(doc, headers, rows, col_widths=None):
    t = doc.add_table(rows=1 + len(rows), cols=len(headers))
    t.alignment = WD_TABLE_ALIGNMENT.CENTER
    t.style = 'Table Grid'
    for i, h in enumerate(headers):
        cell = t.rows[0].cells[i]
        cell.text = ''
        run = cell.paragraphs[0].add_run(h)
        run.bold = True
        run.font.size = Pt(9.5)
        run.font.color.rgb = RGBColor(255, 255, 255)
        set_cell_shading(cell, TEAL)
    for ri, row_data in enumerate(rows):
        for ci, val in enumerate(row_data):
            cell = t.rows[ri + 1].cells[ci]
            cell.text = ''
            run = cell.paragraphs[0].add_run(str(val))
            run.font.size = Pt(9.5)
            run.font.color.rgb = RGBColor(*bytes.fromhex(DARK))
            if ri % 2 == 1:
                set_cell_shading(cell, ALT_ROW)
    if col_widths:
        for i, w in enumerate(col_widths):
            for row in t.rows:
                row.cells[i].width = Cm(w)
    doc.add_paragraph()  # spacer
    return t

def heading(doc, text, level=1):
    h = doc.add_heading(text, level=level)
    for run in h.runs:
        run.font.color.rgb = RGBColor(*bytes.fromhex(TEAL))
    return h

def body(doc, text, bold_prefix=None):
    p = doc.add_paragraph()
    if bold_prefix:
        r = p.add_run(bold_prefix)
        r.bold = True
        r.font.size = Pt(11)
        r.font.color.rgb = RGBColor(*bytes.fromhex(DARK))
    r = p.add_run(text)
    r.font.size = Pt(11)
    r.font.color.rgb = RGBColor(*bytes.fromhex(DARK))
    return p

# ── Question data ───────────────────────────────────────────
AUDIENCES = [
    {
        'title': '1. Generalized (All Audiences)',
        'sources': 'WHO-5 Well-Being Index (WHO, 1998); PROMIS Global Health-10 (NIH, 2009)',
        'scale': 'Almost all the time · More than half the time · Some of the time · Once in a while',
        'dims': [
            ('Physical Wellness', [
                ('G1', 'In the past month, how often have you felt tired or worn out?', 'PROMIS Global08', 'Fatigue (reverse-scored)'),
                ('G2', 'How would you rate your overall physical health right now?', 'PROMIS Global01', 'General health rating'),
                ('G3', 'In a typical week, how often do you move your body in a way that feels good?', 'PROMIS Global06', 'Everyday physical activities'),
            ]),
            ('Emotional Wellness', [
                ('G4', 'In the past two weeks, how often have you felt calm and at ease?', 'WHO-5 Item 2', 'Calm and relaxed'),
                ('G5', 'In the past two weeks, how often have you felt cheerful or in good spirits?', 'WHO-5 Item 1', 'Cheerful and in good spirits'),
                ('G6', 'How often have you felt worried, sad, or upset in a way that was hard to shake?', 'PROMIS Global10', 'Emotional problems (reverse-scored)'),
            ]),
            ('Social Wellness', [
                ('G7', 'Do you have people in your life you can count on when things get hard?', 'PROMIS Global05', 'Social activities and relationships'),
                ('G8', 'How satisfied are you with how you get along with the people closest to you?', 'PROMIS Global05', 'Social relationships'),
                ('G9', 'In the past month, how often have you felt alone even when other people were around?', 'WHO-5 (adapted)', 'Social connectedness (reverse-scored)'),
            ]),
            ('Daily Life Wellness', [
                ('G10', 'How confident are you that you can handle everyday tasks like bills, forms, and schedules?', 'PROMIS Global06', 'Everyday activities capacity'),
                ('G11', 'In the past two weeks, how often has your sleep been restful?', 'PROMIS Global09', 'Sleep quality'),
                ('G12', 'How safe do you feel in your home and neighborhood?', 'PROMIS (adapted)', 'Personal safety'),
            ]),
            ('Purpose Wellness', [
                ('G13', 'In the past two weeks, how often has your daily life been filled with things that interest you?', 'WHO-5 Item 5', 'Daily life filled with interest'),
                ('G14', 'How would you rate your quality of life right now?', 'PROMIS Global02', 'Quality of life'),
                ('G15', 'Do you feel like your life has meaning or direction?', 'WHO-5 (adapted)', 'Purpose and meaning'),
            ]),
            ('Growth Wellness', [
                ('G16', 'In the past two weeks, how often have you felt active and full of energy?', 'WHO-5 Item 3', 'Active and vigorous'),
                ('G17', 'Are you learning or trying anything new right now?', 'WHO-5 (adapted)', 'Interest and engagement'),
                ('G18', 'How often do you make time for things that help you grow (reading, classes, hobbies)?', 'PROMIS (adapted)', 'Personal development'),
            ]),
        ],
    },
    {
        'title': '2. Parent / Caregiver',
        'sources': 'CWBS-SF (Tebb & Berg-Weger, 2016); CAREPAL-8 (Ullrich et al., 2024); Preparedness for Caregiving Scale (Archbold et al., 1990)',
        'scale': 'Almost every day · More days than not · Once in a while · Rarely',
        'dims': [
            ('Physical Wellness', [
                ('P1', 'When did you last see a doctor for YOUR own health check?', 'CWBS-SF', 'Basic needs: self-care'),
                ('P2', 'In the past month, how often have you felt tired or worn out from caregiving?', 'CAREPAL-8 Item 2', 'PHQ-9 fatigue item'),
                ('P3', 'In a typical week, how often do you get enough sleep?', 'CWBS-SF', 'Physical well-being'),
            ]),
            ('Emotional Wellness', [
                ('P4', 'How often do you feel worn out or weighed down by all you have to do?', 'CAREPAL-8 Item 2', 'PHQ-9 fatigue / burden'),
                ('P5', 'In the past two weeks, how often have you been bothered by not being able to stop or control worrying?', 'CAREPAL-8 Item 1', 'GAD-7 worry item'),
                ('P6', 'How often do you feel like you are doing a good job as a caregiver?', 'Preparedness Scale', 'Perceived competence'),
            ]),
            ('Social Wellness', [
                ('P7', 'Do you have someone who can watch your kids so you can take a break?', 'CWBS-SF', 'Respite and support'),
                ('P8', 'Is there someone you can talk to honestly about how hard caregiving can be?', 'CAREPAL-8 (adapted)', 'Emotional support'),
                ('P9', 'How often do you spend time with friends or do something fun for yourself?', 'CWBS-SF', 'Personal time'),
            ]),
            ('Daily Life Wellness', [
                ('P10', 'How confident are you about handling school meetings, doctor visits, and paperwork for your family?', 'Preparedness Scale', 'System navigation readiness'),
                ('P11', 'Does your family have the money and resources to cover your basic needs?', 'CAREPAL-8 Items 5-8', 'Financial / material needs'),
                ('P12', 'Are you able to get the services and supports your family member with a disability needs?', 'CAREPAL-8 Items 5-7', 'Information and service needs'),
            ]),
            ('Purpose Wellness', [
                ('P13', 'Do you feel accepted and respected by the professionals who work with your family?', 'CAREPAL-8 Item 8', 'Professional acceptance'),
                ('P14', 'How often do you feel like your role as a caregiver gives your life meaning?', 'Preparedness Scale (adapted)', 'Role meaning'),
                ('P15', 'Is there anything outside of caregiving that is important to who you are?', 'CWBS-SF', 'Identity beyond caregiving'),
            ]),
            ('Growth Wellness', [
                ('P16', 'Are you learning new things about how to support your family member?', 'Preparedness Scale', 'Knowledge growth'),
                ('P17', 'How often do you feel stuck or like nothing you do makes a difference?', 'CAREPAL-8 (adapted)', 'Helplessness / stagnation (reverse-scored)'),
                ('P18', 'In the past month, have you done anything just for you (a class, a hobby, time with a friend)?', 'CWBS-SF', 'Personal growth activity'),
            ]),
        ],
    },
    {
        'title': '3. Family Member / Household',
        'sources': 'Beach Center FQOL (Hoffman et al., 2006); FQoLS (Brown et al., 2006)',
        'scale': 'Very satisfied · Satisfied · Neither satisfied nor dissatisfied · Dissatisfied',
        'dims': [
            ('Physical Wellness', [
                ('F1', 'Do members of your family get the medical care they need when they need it?', 'FQOL Item 19', 'Physical/Material: medical care'),
                ('F2', 'Does your family feel safe at home, work, school, and in the neighborhood?', 'FQOL Item 21', 'Physical/Material: safety'),
                ('F3', 'Does your family have transportation to get to the places you need to be?', 'FQOL Item 17', 'Physical/Material: transportation'),
            ]),
            ('Emotional Wellness', [
                ('F4', 'Does your family have the support you need to handle stress?', 'FQOL Item 13', 'Emotional Well-Being: stress support'),
                ('F5', 'Do family members have some time to pursue their own interests?', 'FQOL Item 15', 'Emotional Well-Being: personal time'),
                ('F6', 'Do family members have outside help available for special needs?', 'FQOL Item 16', 'Emotional Well-Being: outside help'),
            ]),
            ('Social Wellness', [
                ('F7', 'Do family members have friends or others outside the home they can lean on?', 'FQOL Item 14', 'Emotional Well-Being: external support'),
                ('F8', 'Does your family enjoy spending time together?', 'FQOL Item 1', 'Family Interaction: enjoyment'),
                ('F9', 'Do family members show that they love and care for each other?', 'FQOL Item 5', 'Family Interaction: affection'),
            ]),
            ('Daily Life Wellness', [
                ('F10', 'Are you able to get the services and supports your family member with a disability needs?', 'FQOL Items 22-25', 'Disability-Related Support'),
                ('F11', 'Does your family have a way to take care of expenses?', 'FQOL Item 20', 'Physical/Material: finances'),
                ('F12', 'Does your family have a good relationship with the service providers who work with your family member?', 'FQOL Item 25', 'Disability-Related Support: providers'),
            ]),
            ('Purpose Wellness', [
                ('F13', 'Does your family solve problems together?', 'FQOL Item 3', 'Family Interaction: problem-solving'),
                ('F14', 'Do family members support each other to accomplish goals?', 'FQOL Item 4', 'Family Interaction: goal support'),
                ('F15', 'Does your family feel like it is handling life\'s ups and downs well?', 'FQOL Item 6', 'Family Interaction: resilience'),
            ]),
            ('Growth Wellness', [
                ('F16', 'Do family members help the children learn to be independent?', 'FQOL Item 7', 'Parenting: independence'),
                ('F17', 'Do adults in the family know other people in the children\'s lives (friends, teachers)?', 'FQOL Item 11', 'Parenting: involvement'),
                ('F18', 'Do family members talk openly with each other?', 'FQOL Item 2', 'Family Interaction: communication'),
            ]),
        ],
    },
    {
        'title': '4. Professional (Therapists, Counselors, Coaches, Educators, Healthcare Workers)',
        'sources': 'ProQOL-5 (Stamm, 2010); OLBI (Demerouti et al., 2003)',
        'scale': 'Almost every day · Several days a week · Once in a while · Rarely',
        'dims': [
            ('Physical Wellness', [
                ('PR1', 'How often do you feel physically drained after your workday?', 'ProQOL-5 Item 19', 'Burnout: worn out'),
                ('PR2', 'In the past month, how often have you lost sleep over the experiences of people you help?', 'ProQOL-5 Item 8', 'Burnout: sleep disruption'),
                ('PR3', 'How often do you feel on edge or jumpy because of things you hear at work?', 'ProQOL-5 Item 5', 'STS: startle response'),
            ]),
            ('Emotional Wellness', [
                ('PR4', 'I find my work as a helper to be deeply satisfying.', 'ProQOL-5 Item 3', 'CS: satisfaction from helping'),
                ('PR5', 'How often do you feel depressed because of the experiences of people you help?', 'ProQOL-5 Item 13', 'STS: depression from trauma'),
                ('PR6', 'How often do you feel overwhelmed because your workload seems endless?', 'ProQOL-5 Item 21', 'Burnout: overwhelm'),
            ]),
            ('Social Wellness', [
                ('PR7', 'Do you have colleagues or peers you can talk with honestly about difficult cases?', 'ProQOL-5 (adapted)', 'Peer consultation / support'),
                ('PR8', 'How often do you feel connected to the people around you at work?', 'ProQOL-5 Item 4', 'Burnout-R: connectedness'),
                ('PR9', 'How often do you feel trapped by your job?', 'ProQOL-5 Item 10', 'Burnout: trapped'),
            ]),
            ('Daily Life Wellness', [
                ('PR10', 'How well do you maintain a boundary between your work and your personal life?', 'OLBI (adapted)', 'Disengagement: work-life boundary'),
                ('PR11', 'I am pleased with how I keep up with professional techniques and protocols.', 'ProQOL-5 Item 16', 'CS: professional competence'),
                ('PR12', 'How often do you feel bogged down by the systems and rules at work?', 'ProQOL-5 Item 26', 'Burnout: systemic frustration'),
            ]),
            ('Purpose Wellness', [
                ('PR13', 'I believe I can make a difference through my work.', 'ProQOL-5 Item 22', 'CS: impact'),
                ('PR14', 'How often do you have happy thoughts about the people you help?', 'ProQOL-5 Item 20', 'CS: positive regard'),
                ('PR15', 'Are you happy that you chose this line of work?', 'ProQOL-5 Item 30', 'CS: career satisfaction'),
            ]),
            ('Growth Wellness', [
                ('PR16', 'I am pleased with how I am able to keep up with helping techniques and protocols.', 'ProQOL-5 Item 16', 'CS: professional growth'),
                ('PR17', 'How often do you avoid situations that remind you of frightening things people you help have told you?', 'ProQOL-5 Item 23', 'STS: avoidance (reverse-scored)'),
                ('PR18', 'Are you the person you always wanted to be in this work?', 'ProQOL-5 Item 17', 'Burnout-R: self-concept'),
            ]),
        ],
    },
    {
        'title': '5. Special Needs / Accessible Format (Person with Disability)',
        'sources': 'PWI-ID (Cummins & Lau, 2005); adapted from WHO-5',
        'scale': 'Emoji scale: Not Good · Okay · Pretty Good · Good · Very Good (support person may assist)',
        'dims': [
            ('My Body', [
                ('SN1', 'How do you feel about your health?', 'PWI-ID: Health', 'Health domain'),
                ('SN2', 'How do you feel about how safe you feel?', 'PWI-ID: Personal Safety', 'Safety domain'),
            ]),
            ('My Feelings', [
                ('SN3', 'In the past two weeks, how often have you felt happy?', 'WHO-5 Item 1 (simplified)', 'Cheerful and in good spirits'),
                ('SN4', 'In the past two weeks, how often have you felt calm?', 'WHO-5 Item 2 (simplified)', 'Calm and relaxed'),
            ]),
            ('My People', [
                ('SN5', 'How do you feel about how you get along with people? Like your family and friends.', 'PWI-ID: Personal Relationships', 'Relationships domain'),
                ('SN6', 'How do you feel about doing things outside your home? Like in your community or neighborhood.', 'PWI-ID: Community Connectedness', 'Community domain'),
            ]),
            ('My Day', [
                ('SN7', 'How do you feel about the things you have? Like your money and the things you own.', 'PWI-ID: Standard of Living', 'Material well-being'),
                ('SN8', 'How do you feel about how your day goes? Like your routine and what you do.', 'WHO-5 Item 5 (simplified)', 'Daily life filled with interest'),
            ]),
            ('What Matters', [
                ('SN9', 'How do you feel about the things you do or make?', 'PWI-ID: Life Achievement', 'Achievement domain'),
                ('SN10', 'How do you feel about your life as a whole?', 'PWI-ID: Global', 'Life as a whole (validity check)'),
            ]),
            ('Growing', [
                ('SN11', 'How do you feel about what will happen to you later in your life?', 'PWI-ID: Future Security', 'Future security domain'),
                ('SN12', 'In the past two weeks, how often have you felt like doing things?', 'WHO-5 Item 3 (simplified)', 'Active and vigorous'),
            ]),
        ],
    },
]

# ── Build document ──────────────────────────────────────────
doc = Document()

for section in doc.sections:
    section.top_margin = Cm(2.54)
    section.bottom_margin = Cm(2.54)
    section.left_margin = Cm(2.54)
    section.right_margin = Cm(2.54)

# Title
p = doc.add_paragraph()
p.alignment = WD_ALIGN_PARAGRAPH.CENTER
pf = p.paragraph_format
pf.space_before = Pt(48)
r = p.add_run('Wheel of Wellness\nFull Question Bank')
r.bold = True
r.font.size = Pt(28)
r.font.color.rgb = RGBColor(*bytes.fromhex(TEAL))

p = doc.add_paragraph()
p.alignment = WD_ALIGN_PARAGRAPH.CENTER
r = p.add_run('HELPipedia / SpecialNeeds.help')
r.font.size = Pt(14)
r.font.color.rgb = RGBColor(*bytes.fromhex(CORAL))

p = doc.add_paragraph()
p.alignment = WD_ALIGN_PARAGRAPH.CENTER
pf = p.paragraph_format
pf.space_before = Pt(24)
r = p.add_run(
    'Each audience variant contains 18 questions (3 per dimension).\n'
    'Items are mapped to their source instrument and adapted at or below a 4th-grade reading level.\n'
    'Response options use a consistent 4-point scale per audience.\n\n'
    'Special Needs variant: 12 questions (2 per dimension) with emoji response scale.'
)
r.font.size = Pt(11)
r.font.color.rgb = RGBColor(*bytes.fromhex(DARK))

doc.add_page_break()

# Audience sections
for ai, aud in enumerate(AUDIENCES):
    heading(doc, aud['title'], level=1)
    body(doc, aud['sources'], bold_prefix='Source instruments: ')
    body(doc, aud['scale'], bold_prefix='Response scale: ')
    if 'Special Needs' in aud['title']:
        body(doc, '12 items (2 per dimension). Support person may assist with reading.', bold_prefix='Note: ')

    for dim_name, items in aud['dims']:
        heading(doc, dim_name, level=2)
        styled_table(doc,
            ['#', 'Question', 'Source', 'Adapted From'],
            [[q, text, src, adapt] for q, text, src, adapt in items],
            col_widths=[1.5, 9, 3.5, 4],
        )

    if ai < len(AUDIENCES) - 1:
        doc.add_page_break()

# Scoring notes
doc.add_page_break()
heading(doc, 'Scoring Notes', level=1)

body(doc, 'Generalized, Parent/Caregiver, Family, Professional: 18 items. Score per dimension = mean of 3 items x 25 (range: 0-100). Overall score = mean of all 6 dimension scores.',
     bold_prefix='Scoring: ')
body(doc, 'Special Needs: 12 items. Score per dimension = mean of 2 items x 20 (range: 0-100). Overall score = mean of all 6 dimension scores.')
body(doc, '0-39 = Needs Attention · 40-59 = Moderate · 60-100 = Strong',
     bold_prefix='Thresholds: ')
body(doc, 'All instruments are screening tools for self-reflection. Not diagnostic. Clinical interpretation should involve a qualified professional.',
     bold_prefix='Important: ')

# Footer
p = doc.add_paragraph()
p.alignment = WD_ALIGN_PARAGRAPH.CENTER
pf = p.paragraph_format
pf.space_before = Pt(24)
r = p.add_run('Last updated: July 2026\nCompiled for HELPipedia Wheel of Wellness / SpecialNeeds.help')
r.italic = True
r.font.size = Pt(10)
r.font.color.rgb = RGBColor(*bytes.fromhex('888888'))

# Save
out = '/home/newkirk/Documents/Academic/Career/WheelOfWellnessQuestionBank.docx'
doc.save(out)
print(f'Saved: {out}')
