18 year old male patient with pain in left inguinal region

Reading File
Finding Sources
Finding Sources
Searching PubMed

inguinal pain young male differential diagnosis

Reading File
Reading File
Finding Sources
Finding Sources
Reading File
Reading File
Reading File
Reading File
Reading File
I now have comprehensive information to provide a thorough clinical response. Let me also quickly fetch the anatomy diagram from Bailey & Love which shows hernia relationships.
Here is a detailed clinical approach to an 18-year-old male presenting with left inguinal pain.

Left Inguinal Pain in an 18-Year-Old Male: Clinical Approach


Differential Diagnosis

In a young male this age, the inguinal region is anatomically complex (inguinal canal, spermatic cord, testis, ilioinguinal nerve, lymph nodes, hip joint nearby), and the differential is broad. Organized by priority:

1. Inguinal Hernia (most common surgical cause)

The most common hernia in men, ~10x more frequent than in women. At age 18, an indirect inguinal hernia is most likely - caused by a patent processus vaginalis (congenital predisposition) that manifests with exertion.
  • Indirect: hernia sac passes through the deep inguinal ring, travels the inguinal canal, may reach the scrotum. The defect lies lateral to the inferior epigastric vessels.
  • Direct: passes through Hesselbach's triangle, medial to inferior epigastric vessels - less common at this age.
Clinical features:
  • Groin bulge that appears on standing/straining, reduces when lying
  • Dull aching pain, often worse with lifting or exercise
  • A cough impulse on palpation
  • Cord may feel thicker on the affected side
  • Red flags: incarcerated hernia = firm, irreducible, painful bulge + bowel obstruction signs; strangulated hernia = tender, erythematous, systemic toxicity - surgical emergency
Bailey and Love's Short Practice of Surgery, 28th Ed.

2. Testicular Torsion (URGENT - must exclude)

Peak incidence in early adolescence. This is a urologic emergency.
  • Presentation: sudden, severe unilateral testicular/scrotal pain often with nausea and vomiting. Pain may radiate to the inguinal region or lower abdomen. Can occur without direct scrotal trauma.
  • Signs: high-riding testis, transverse lie, absent cremasteric reflex, tender and firm testis, anterior epididymis
  • Intravaginal torsion (most common in this age group) occurs due to a "bell-clapper" deformity
  • Window for salvage: testicle must be detorsed within 4-6 hours. Do NOT wait for ultrasound if clinical suspicion is high - go straight to surgical exploration.
  • Doppler ultrasound shows absent or reduced testicular blood flow
Campbell-Walsh-Wein Urology, 3-Volume Set

3. Epididymitis / Orchitis

Common in sexually active adolescents.
  • In adolescents: consider Chlamydia trachomatis, Neisseria gonorrhoeae (STI), or structural urinary tract anomalies
  • Presentation: gradual onset scrotal/inguinal pain and swelling, tenderness on palpation, possible urethral discharge, fever
  • Cremasteric reflex usually intact; Prehn's sign (pain relief with elevation) may be present but does not reliably exclude torsion
  • Management: STI testing, UA and culture. Treat with IM ceftriaxone 500 mg (gonococcal) + doxycycline 100 mg BD x 7 days or azithromycin 1g PO (chlamydial)
Rosen's Emergency Medicine, 9th Ed.

4. Torsion of Appendix Testis

  • Usually prepubertal males but can occur at 18
  • More gradual onset, less intense pain at the superior pole of testis
  • "Blue dot sign" may be visible through scrotal skin
  • Self-limiting but can mimic torsion; ultrasound distinguishes

5. Athletic Pubalgia / "Sports Hernia" (Core Muscle Injury)

Very relevant in an active 18-year-old male.
  • Caused by posterior inguinal wall weakness (torn internal oblique/transversus abdominis) without a true hernia bulge
  • Pain: chronic exertional groin pain, worse with kicking, twisting, sit-ups; may radiate to adductors or scrotum
  • Often in footballers, hockey players, sprinters
  • No palpable hernia on exam; diagnosis via MRI or can be clinical
  • True inguinal hernias are less frequent in young athletes; nerve entrapment (ilioinguinal nerve) is another inguinal-related cause
Fischer's Mastery of Surgery, 8th Ed.

6. Other Musculoskeletal Causes

ConditionKey Features
Adductor muscle strain/tearMedial upper thigh pain, worse with adduction; common in sport
Iliopsoas strain/bursitisDeep groin/anterior hip pain with hip flexion
Osteitis pubisMidline pubic pain, bilateral tenderness at symphysis
Hip labral tear / FAIAnterior groin pain, worse with hip flexion-internal rotation
Femoral-acetabular impingementDeep groin pain, "C-sign," positive FABER/FADIR tests
Pubic stress fracturePain on weight-bearing; seen on MRI/CT

7. Non-musculoskeletal Causes (less common but important)

  • Appendicitis (right >> left, but consider): left-sided presentations are rare but can cause referred lower abdominal/inguinal pain
  • Inguinal lymphadenopathy: tender, enlarged nodes from STI, skin infection, or lymphoma - examine for nodes in bilateral groin
  • Psoas abscess: deep hip flexor region pain, often with fever, hip held in flexion
  • Varicocele: "bag of worms" feel in left spermatic cord (left side more common due to left testicular vein draining into left renal vein); dull ache worse on standing; associated with subfertility
  • Hydrocele of the cord: smooth, transilluminable swelling
  • Undescended testis / ectopic testis: inguinal canal - check if testis is present in scrotum bilaterally
  • Femoral hernia: less common in males; bulge below and lateral to pubic tubercle
  • Nerve entrapment (ilioinguinal, iliohypogastric, genitofemoral): burning, lancinating pain ± paresthesia in inguinal region/scrotum/inner thigh

Clinical Evaluation

History - ask about:
  • Onset and character: sudden (torsion, incarceration) vs. gradual (epididymitis, hernia)
  • Radiation to scrotum or thigh
  • Associated nausea/vomiting (torsion)
  • Presence of a visible or palpable bulge, cough impulse
  • Sexual activity, urethral discharge (STI/epididymitis)
  • Physical activity and sport history
  • Urinary symptoms
  • Fever, systemic symptoms
Examination:
  • Standing and supine exam of inguinal region and scrotum
  • Check for a hernia: palpate inguinal canal with finger invaginating scrotal skin, feel for cough impulse
  • Examine both testes: lie, size, tenderness, consistency
  • Cremasteric reflex (absent = torsion until proven otherwise)
  • Scrotal transillumination
  • Inguinal lymph nodes
  • Hip joint: range of movement, FABER/FADIR tests
Investigations:
TestIndication
Scrotal Doppler ultrasoundTorsion, epididymitis, varicocele, hernia (if uncertain)
Urine analysis + cultureEpididymitis/UTI
STI swab / NAATAdolescent male with epididymitis
MRI pelvis/groinAthletic pubalgia, labral tear, FAI, occult stress fracture
Inguinal herniography / ultrasoundOccult hernia

Red Flags (Immediate Action Required)

SignDiagnosisAction
Absent cremasteric reflex + acute painTesticular torsionImmediate surgical exploration - do NOT delay for USS
Irreducible, tender inguinal bulgeIncarcerated herniaUrgent surgical review
Tender, erythematous inguinal swelling + systemic toxicityStrangulated herniaEmergency surgery
High-riding, transversely-lying testis + nausea/vomitingTesticular torsionOR within 4-6 hours

Summary

The most likely diagnoses in this patient are:
  1. Indirect inguinal hernia - most common at this age, examine for a bulge with cough impulse
  2. Testicular torsion - must be excluded urgently given adolescent age and acute pain
  3. Epididymitis - if sexually active, gradual onset
  4. Athletic pubalgia / adductor strain - if active in sport, no hernia bulge
Sources consulted:
  • Bailey and Love's Short Practice of Surgery, 28th Ed. - Inguinal Hernia chapter
  • Campbell-Walsh-Wein Urology - Testicular Torsion & Scrotal Pain
  • Rosen's Emergency Medicine - Epididymitis, Orchitis, Testicular Torsion
  • Fischer's Mastery of Surgery, 8th Ed. - Athletic Groin Pain differential
  • Schwartz's Principles of Surgery, 11th Ed. - Pediatric/Young Adult Inguinal Hernia

No prescription??

Asking for Preferences
Asking for Preferences
Good. A reducible indirect inguinal hernia in an 18-year-old is primarily a surgical condition - the definitive treatment is hernia repair, not pharmacological. However, here is the complete prescription and management plan:

Prescription & Management Plan

Diagnosis: Reducible Indirect Inguinal Hernia (Left)

Patient: 18-year-old male

Rx - Symptomatic (Pre-operative / Conservative)

1. Analgesic / Anti-inflammatory
Tab. Ibuprofen 400 mg
Sig: 1 tablet TDS after meals × 5 days
(for pain relief and local inflammation)
If gastric intolerance:
Tab. Paracetamol 500 mg
Sig: 1-2 tablets TDS × 5 days
2. Proton Pump Inhibitor (gastric cover with NSAID)
Tab. Pantoprazole 40 mg
Sig: 1 tablet OD before breakfast × 5 days
3. Stool Softener / Laxative (to reduce straining at stool)
Syrup Lactulose 15 mL
Sig: BD at bedtime × 7 days
OR
Tab. Ispaghula husk (Isabgol / Psyllium)
Sig: 1 sachet in water BD × 7 days
Straining is a key aggravating factor for inguinal hernias. Preventing constipation reduces discomfort and the risk of incarceration.

Non-Pharmacological Advice (Mandatory)

  • Avoid heavy lifting and strenuous exertion until surgically repaired
  • Truss/scrotal support may be used temporarily for comfort while awaiting surgery - though not a long-term solution
  • Cough management: treat any chronic cough if present (coughing increases intra-abdominal pressure)
  • Refer for elective herniorrhaphy - this is the definitive treatment

Surgical Referral (Definitive Treatment)

An 18-year-old male with a reducible indirect inguinal hernia requires elective surgical repair. The presence of a hernia in a young male is itself an indication for surgery, as the risk of incarceration/strangulation increases over time.
Options:
ApproachProcedure
Open (classic)Lichtenstein tension-free mesh repair (most common in adults)
LaparoscopicTAPP (Transabdominal Preperitoneal) or TEP (Totally Extraperitoneal)
  • At 18, the inguinal floor may need hernioplasty (floor reconstruction ± mesh) rather than simple herniorrhaphy (processus vaginalis ligation alone, as done in children)
  • Laparoscopic repair is preferred in young active patients for faster return to activity

Warning Signs - Return Immediately If:

  • Hernia becomes irreducible (cannot push it back)
  • Severe, sudden worsening of pain
  • Nausea, vomiting, abdominal distension
  • Overlying skin becomes red or discolored
  • Fever
These suggest incarceration or strangulation - a surgical emergency.

Summary

In an uncomplicated reducible inguinal hernia, there is no specific drug that treats the hernia itself. Pharmacotherapy is purely symptomatic (pain relief, stool softening). The prescription should always be paired with a surgical referral - that is the only cure.
Bailey and Love's Short Practice of Surgery, 28th Ed. - "Surgery - open or minimally invasive - is the definitive management of inguinal hernia."
attachment
attachment

Can't give this??

analyse_media
I can read the prescription. This is an inpatient/injectable prescription - which means this patient is being managed as an admitted/emergency case, not an outpatient. That changes the picture significantly.

Analysis of This Prescription

The prescription reads:
DrugDoseFrequencyPurpose
Inj. Xone (Ceftriaxone)1g IVBDBroad-spectrum antibiotic
Inj. Pan (Pantoprazole)40mg IVODGastric protection
Inj. Buscopancrossed out-(antispasmodic - cancelled)
Inj. Tramadol100mg IV/IMBDOpioid analgesia
Inj. Emset (Ondansetron)4mg IVTDSAntiemetic
DNS1 bottle IVMaintenance fluid
RL1 bottle IVMaintenance fluid
NS1 bottle IVMaintenance fluid

Can This Be Given?

Yes, this is a reasonable inpatient prescription, BUT with a few important points:

✅ What Makes Sense

  • Tramadol 100mg BD - adequate analgesia for moderate-severe inguinal pain (hernia ± incarceration concern)
  • Ondansetron (Emset) 4mg TDS - appropriate antiemetic; nausea is common with pain and opioid use
  • Pantoprazole 40mg OD - gastric cover, especially with IV medications
  • IV fluids (DNS + RL + NS) - standard maintenance hydration for an admitted patient

⚠️ Points to Consider

Ceftriaxone 1g IV BD - this is the most important question. For a simple reducible inguinal hernia, antibiotics are NOT indicated. However, this antibiotic makes sense if:
  • There is suspicion of epididymitis/orchitis co-existing
  • There is concern for incarceration with bowel compromise
  • Pre-operative prophylaxis is planned (though usually a single dose, not BD)
  • The diagnosis also includes an infectious component
If this is a pure reducible hernia with no infection, the ceftriaxone should be reconsidered. A single pre-operative dose of ceftriaxone 1g is standard prophylaxis for hernia repair - but not BD ongoing.

🔴 Tramadol Caution

  • Tramadol 100mg BD is a strong analgesic choice. Ensure:
    • No seizure history (tramadol lowers seizure threshold)
    • No concurrent SSRI/SNRI use (serotonin syndrome risk)
    • Monitor for sedation, respiratory depression
    • Give slowly if IV push

Suggested Complete Inpatient Prescription

1. Inj. Ceftriaxone 1g IV BD × 3-5 days     (if infection/pre-op)
2. Inj. Tramadol 100mg IV/IM BD              (analgesia - with monitoring)
3. Inj. Ondansetron (Emset) 4mg IV TDS       (antiemetic)
4. Inj. Pantoprazole 40mg IV OD              (gastric protection)
5. IVF: DNS 500mL + RL 500mL + NS 500mL     (maintenance hydration)
6. Tab. Lactulose 15mL BD / Ispaghula BD     (stool softener - avoid straining)
7. Surgical consult for elective/semi-urgent hernia repair

Bottom line: This prescription is clinically reasonable for an admitted patient with significant inguinal pain. The key question is whether ceftriaxone is justified - confirm the exact indication (infection vs. pre-op prophylaxis vs. empirical). Everything else is appropriate and can be given.

And what prescription we have written before

Here is the prescription I wrote earlier in our conversation:

Prescription Written Earlier (Outpatient / Pre-operative)

DrugDoseFrequencyDuration
Tab. Ibuprofen400mgTDS after meals5 days
Tab. Pantoprazole40mgOD before breakfast5 days
Syrup Lactulose15mLBD7 days
(Paracetamol 500mg TDS was given as an alternative to Ibuprofen if gastric intolerance)

Comparison: My Prescription vs. The Image Prescription

FeatureMy PrescriptionImage Prescription
SettingOutpatient / conservativeInpatient / admitted
RouteOralInjectable (IV/IM)
AnalgesiaTab. Ibuprofen 400mg TDSInj. Tramadol 100mg BD
Gastric coverTab. Pantoprazole 40mg ODInj. Pantoprazole 40mg OD
AntiemeticNot includedInj. Ondansetron 4mg TDS
AntibioticNot includedInj. Ceftriaxone 1g BD
AntispasmodicNot includedInj. Buscopan (crossed out)
IV FluidsNot includedDNS + RL + NS (1 bottle each)
Stool softenerLactulose / IspaghulaNot included

Why They Differ

  • My prescription assumed an outpatient, stable, reducible hernia - managed with oral medications for symptom relief while awaiting elective surgery.
  • The image prescription is an inpatient injectable regimen - suggesting the patient was admitted, likely with significant pain, nausea, or a more acute presentation (possible incarceration concern at triage, or co-existing epididymitis).
Both are valid - they serve different clinical settings. For an admitted patient with significant pain, the injectable regimen in the image is more appropriate.

What are the contraindications to tramadol in this patient?

Finding Sources
Reading File
Here is a complete breakdown of tramadol contraindications, applied specifically to this patient:

Contraindications to Tramadol in This Patient

Patient profile: 18-year-old male, inguinal hernia, admitted


🔴 Absolute Contraindications

ContraindicationRelevance to This Patient
Epilepsy / seizure disorderTramadol significantly lowers seizure threshold. Must ask about history before prescribing.
Concurrent MAOI use (or within 14 days)Risk of serotonin syndrome - potentially fatal. Ask about psychiatric medications.
Acute intoxication with alcohol, hypnotics, opioids, or CNS depressantsAdditive CNS/respiratory depression.
Uncontrolled respiratory depression / severe asthmaTramadol suppresses respiratory drive.
Hypersensitivity to tramadolPrevious allergic reaction - ask.
Age under 12 yearsNot applicable here (patient is 18).
Post-tonsillectomy/adenoidectomy pain in childrenNot applicable here.

🟠 Relative Contraindications / Cautions (check in this patient)

CautionWhy It Matters
Head injury / raised ICPTramadol can cause miosis, vomiting, and sedation masking neurological signs.
Hepatic impairmentTramadol is hepatically metabolized (CYP2D6, CYP3A4). Accumulation risk.
Renal impairmentActive metabolites accumulate - reduce dose / extend interval. In an 18-year-old, unlikely but check if any known kidney disease.
History of substance abuse / opioid dependenceTramadol has abuse potential.
Concurrent SSRIs / SNRIs / TCAs / triptansRisk of serotonin syndrome - fever, agitation, tremor, myoclonus, autonomic instability. Ask about antidepressant use.
Concurrent drugs that lower seizure thresholdAntipsychotics, bupropion, etc.
CYP2D6 ultra-rapid metabolizersConvert tramadol to morphine faster - risk of opioid toxicity.
Constipation / bowel obstructionTramadol (like all opioids) reduces gut motility - worsens constipation, which is already a concern in hernia patients. Add laxative/stool softener.

⚠️ Specific to This Patient (18-year-old male)

  1. Seizure history - most important question to ask before giving tramadol. Young males can have undiagnosed epilepsy.
  2. No antidepressant / recreational drug use - serotonin syndrome risk is real in adolescents.
  3. Opioid-naive - start at lower end; monitor for excessive sedation, nausea, respiratory rate.
  4. Pre-operative concern - if surgery is being planned, tramadol can contribute to post-op nausea/vomiting (PONV). Ondansetron (Emset) already co-prescribed covers this.
  5. Constipation - already a hernia aggravating factor. Add a stool softener alongside tramadol.

In Summary

For this specific patient, tramadol 100mg BD is safe to give provided:
  • No seizure disorder
  • No concurrent MAOI / SSRI / serotonin-active drug
  • No significant hepatic or renal disease
  • Respiratory rate and sedation level monitored
  • Stool softener co-prescribed
Fischer's Mastery of Surgery, 8th Ed. - Analgesic Therapy Options table

You written pcm if gastric intolerance But you must write in detail which type of

You're right - "Paracetamol 500mg" is too vague. Here is the detail:

Paracetamol - Which Formulation & Dose?

For This Patient (18-year-old male, moderate inguinal pain)


Correct Prescription

Tab. Paracetamol 650 mg (not 500 mg)
Sig: 1 tablet TDS × 5 days (after meals)
Max: Do not exceed 4g (4000mg) per day
650 mg is preferred over 500 mg in adults for adequate analgesia. 500 mg is a sub-therapeutic dose for moderate pain in an adult male.

Available Formulations of Paracetamol

FormulationBrand ExamplesWhen to Use
Tab. Paracetamol 500 mgCalpol, MetacinMild pain, children/elderly
Tab. Paracetamol 650 mgCrocin Pain Relief, Dolo 650Preferred in adults for moderate pain
Tab. Paracetamol 1000 mg (1g)Calpol 1gSevere pain, short-term, max 4 doses/day
Syrup Paracetamol 125mg/5mLCalpol SyrupPediatric use
Suppository (PR)-When oral route not possible (vomiting)
Inj. Paracetamol 1g/100mL IVPerfalgan, IntraparacetamolInpatient - rapid onset, NPO patients, pre/post-op

Why Paracetamol Instead of Ibuprofen Here?

Ibuprofen is contraindicated / cautioned in:
Gastric ConditionReason to Avoid NSAIDs
Peptic ulcer disease (PUD)NSAIDs inhibit COX-1 → reduced prostaglandin E2 → impaired gastric mucosal protection → ulceration, bleeding
GERD / gastritisNSAIDs worsen mucosal irritation
History of GI bleedNSAIDs increase bleeding risk (COX-1 inhibition → reduced thromboxane A2 → platelet dysfunction)
H. pylori positiveCombined NSAID + H. pylori = very high ulcer risk
IBD (Crohn's / UC)NSAIDs can trigger flares
In all these situations, switch to Paracetamol - it works centrally (COX inhibition in CNS), has no significant gastric mucosal toxicity, and is safe orally.

Complete Corrected Outpatient Prescription

Tab. Ibuprofen 400 mg TDS after meals × 5 days
        OR (if gastric intolerance/ulcer/gastritis/GI bleed history)
Tab. Paracetamol 650 mg TDS after meals × 5 days
        (Max 4g/day; avoid if liver disease)

Tab. Pantoprazole 40 mg OD before breakfast × 5 days
        (If on Ibuprofen - mandatory gastric cover)
        (With Paracetamol - may omit if no gastric symptoms)

Tab. Ispaghula husk 1 sachet in water BD × 7 days
        (Stool softener - avoid straining)

Key Teaching Point

Never write just "Paracetamol" - always specify:
  • Strength (500mg vs 650mg vs 1g)
  • Route (oral / IV / PR)
  • Frequency (TDS / QID)
  • Duration
  • Max daily dose warning (especially if patient also taking combination cold/flu drugs that contain paracetamol)

Create a detailed PDF on analgesic choice algorithm for surgical pain

Reading File
Finding Sources
Running Command

mkdir -p /home/daytona/workspace/analgesic-pdf && ls /home/daytona/workspace/

Writing File

~/analgesic-pdf/generate_pdf.py

from reportlab.lib.pagesizes import A4
from reportlab.lib import colors
from reportlab.lib.units import cm
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.platypus import (
    SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle,
    HRFlowable, KeepTogether
)
from reportlab.lib.enums import TA_CENTER, TA_LEFT, TA_JUSTIFY
from reportlab.platypus import PageBreak

OUTPUT = "/home/daytona/workspace/analgesic-pdf/Analgesic_Choice_Algorithm_Surgical_Pain.pdf"

doc = SimpleDocTemplate(
    OUTPUT,
    pagesize=A4,
    rightMargin=1.8*cm,
    leftMargin=1.8*cm,
    topMargin=2*cm,
    bottomMargin=2*cm,
)

# ── Colour palette ──────────────────────────────────────────────
DARK_BLUE   = colors.HexColor("#1a3a5c")
MED_BLUE    = colors.HexColor("#2563a8")
LIGHT_BLUE  = colors.HexColor("#dbeafe")
STEP1_COL   = colors.HexColor("#16a34a")   # green  – mild
STEP2_COL   = colors.HexColor("#d97706")   # amber  – moderate
STEP3_COL   = colors.HexColor("#dc2626")   # red    – severe
TEAL        = colors.HexColor("#0d9488")
PURPLE      = colors.HexColor("#7c3aed")
LIGHT_GRAY  = colors.HexColor("#f1f5f9")
MID_GRAY    = colors.HexColor("#64748b")
WHITE       = colors.white
ORANGE      = colors.HexColor("#ea580c")

styles = getSampleStyleSheet()

def style(name="Normal", **kw):
    return ParagraphStyle(name, parent=styles[name], **kw)

title_style = style("Title",
    fontSize=22, textColor=WHITE, alignment=TA_CENTER,
    spaceAfter=4, fontName="Helvetica-Bold")

subtitle_style = style("Normal",
    fontSize=11, textColor=colors.HexColor("#bfdbfe"),
    alignment=TA_CENTER, spaceAfter=2, fontName="Helvetica")

section_head = style("Normal",
    fontSize=13, textColor=WHITE, fontName="Helvetica-Bold",
    alignment=TA_LEFT, spaceAfter=4, spaceBefore=6,
    leftIndent=6)

body = style("Normal",
    fontSize=9.5, textColor=colors.HexColor("#1e293b"),
    leading=14, spaceAfter=3, fontName="Helvetica")

body_bold = style("Normal",
    fontSize=9.5, textColor=DARK_BLUE,
    leading=14, spaceAfter=3, fontName="Helvetica-Bold")

small = style("Normal",
    fontSize=8.5, textColor=MID_GRAY,
    leading=12, spaceAfter=2, fontName="Helvetica-Oblique")

bullet = style("Normal",
    fontSize=9.5, textColor=colors.HexColor("#1e293b"),
    leading=14, spaceAfter=2, fontName="Helvetica",
    leftIndent=12, bulletIndent=2)

table_header_style = ParagraphStyle("th",
    fontSize=9, textColor=WHITE, fontName="Helvetica-Bold",
    alignment=TA_CENTER, leading=12)

table_cell_style = ParagraphStyle("td",
    fontSize=8.5, textColor=colors.HexColor("#1e293b"),
    fontName="Helvetica", alignment=TA_LEFT, leading=12)

table_cell_center = ParagraphStyle("tdc",
    fontSize=8.5, textColor=colors.HexColor("#1e293b"),
    fontName="Helvetica", alignment=TA_CENTER, leading=12)

warning_style = style("Normal",
    fontSize=9, textColor=colors.HexColor("#7f1d1d"),
    fontName="Helvetica-Bold", leading=13,
    leftIndent=8, spaceAfter=3)

note_style = style("Normal",
    fontSize=8.5, textColor=colors.HexColor("#374151"),
    fontName="Helvetica-Oblique", leading=13,
    leftIndent=8, spaceAfter=3)

# ── Helper: coloured section banner ──────────────────────────────
def section_banner(title, bg=MED_BLUE):
    data = [[Paragraph(title, section_head)]]
    t = Table(data, colWidths=[17.4*cm])
    t.setStyle(TableStyle([
        ("BACKGROUND", (0,0), (-1,-1), bg),
        ("TOPPADDING",    (0,0), (-1,-1), 6),
        ("BOTTOMPADDING", (0,0), (-1,-1), 6),
        ("LEFTPADDING",   (0,0), (-1,-1), 10),
        ("ROUNDEDCORNERS", [4,4,4,4]),
    ]))
    return t

def colored_box(content_list, bg=LIGHT_BLUE, border=MED_BLUE):
    inner = Table([[c] for c in content_list], colWidths=[16.6*cm])
    inner.setStyle(TableStyle([
        ("BACKGROUND", (0,0), (-1,-1), bg),
        ("TOPPADDING",    (0,0), (-1,-1), 4),
        ("BOTTOMPADDING", (0,0), (-1,-1), 4),
        ("LEFTPADDING",   (0,0), (-1,-1), 8),
        ("RIGHTPADDING",  (0,0), (-1,-1), 8),
        ("LINEAFTER",  (0,0), (0,-1), 3, border),
    ]))
    return inner

# ═══════════════════════════════════════════════════════════════
# BUILD STORY
# ═══════════════════════════════════════════════════════════════
story = []

# ── COVER HEADER ─────────────────────────────────────────────────
cover_data = [[
    Paragraph("ANALGESIC CHOICE ALGORITHM", title_style),
    Paragraph("Surgical Pain Management", subtitle_style),
    Paragraph("Evidence-Based Guide for Pre-operative, Intra-operative & Post-operative Analgesia", subtitle_style),
]]
cover = Table([[Paragraph("ANALGESIC CHOICE ALGORITHM", title_style)],
               [Paragraph("Surgical Pain Management", subtitle_style)],
               [Paragraph("Evidence-Based Guide for Pre-operative, Intra-operative & Post-operative Analgesia", subtitle_style)]],
              colWidths=[17.4*cm])
cover.setStyle(TableStyle([
    ("BACKGROUND",    (0,0), (-1,-1), DARK_BLUE),
    ("TOPPADDING",    (0,0), (-1,-1), 14),
    ("BOTTOMPADDING", (0,0), (-1,-1), 14),
    ("LEFTPADDING",   (0,0), (-1,-1), 16),
    ("RIGHTPADDING",  (0,0), (-1,-1), 16),
]))
story.append(cover)
story.append(Spacer(1, 0.4*cm))

# ── SECTION 1: PAIN ASSESSMENT ───────────────────────────────────
story.append(section_banner("1.  PAIN ASSESSMENT — THE FIRST STEP"))
story.append(Spacer(1, 0.2*cm))

story.append(Paragraph("<b>Always assess before prescribing:</b>", body_bold))
assess_data = [
    [Paragraph("<b>Tool</b>", table_header_style),
     Paragraph("<b>Use In</b>", table_header_style),
     Paragraph("<b>Score / Output</b>", table_header_style)],
    [Paragraph("VAS (Visual Analogue Scale)", table_cell_style),
     Paragraph("Adults, cooperative patients", table_cell_style),
     Paragraph("0–10 line; 0=no pain, 10=worst", table_cell_style)],
    [Paragraph("NRS (Numerical Rating Scale)", table_cell_style),
     Paragraph("Most clinical settings", table_cell_style),
     Paragraph("0–10 number; ≤3 mild, 4–6 mod, ≥7 severe", table_cell_style)],
    [Paragraph("Wong-Baker FACES", table_cell_style),
     Paragraph("Children, low literacy", table_cell_style),
     Paragraph("0–10 faces", table_cell_style)],
    [Paragraph("CPOT (Critical-Care Pain Obs Tool)", table_cell_style),
     Paragraph("ICU / non-verbal patients", table_cell_style),
     Paragraph("0–8; ≥3 = significant pain", table_cell_style)],
]
assess_table = Table(assess_data, colWidths=[5*cm, 6*cm, 6.4*cm])
assess_table.setStyle(TableStyle([
    ("BACKGROUND",    (0,0), (-1,0), MED_BLUE),
    ("ROWBACKGROUNDS",(0,1), (-1,-1), [WHITE, LIGHT_GRAY]),
    ("GRID",          (0,0), (-1,-1), 0.4, colors.HexColor("#cbd5e1")),
    ("TOPPADDING",    (0,0), (-1,-1), 5),
    ("BOTTOMPADDING", (0,0), (-1,-1), 5),
    ("LEFTPADDING",   (0,0), (-1,-1), 6),
]))
story.append(assess_table)
story.append(Spacer(1, 0.3*cm))

# ── SECTION 2: WHO ANALGESIC LADDER ──────────────────────────────
story.append(section_banner("2.  WHO ANALGESIC LADDER  (adapted for Surgical Pain)"))
story.append(Spacer(1, 0.2*cm))

ladder_data = [
    [Paragraph("<b>STEP 1</b>\nMild Pain\nNRS 1–3", table_header_style),
     Paragraph("<b>STEP 2</b>\nModerate Pain\nNRS 4–6", table_header_style),
     Paragraph("<b>STEP 3</b>\nSevere Pain\nNRS 7–10", table_header_style)],
    [
     Paragraph("• Paracetamol 650 mg–1g TDS/QID\n• Ibuprofen 400 mg TDS (with food)\n• Diclofenac 50 mg TDS\n• Naproxen 250–500 mg BD\n\n<i>± Adjuvants</i>", table_cell_style),
     Paragraph("• Tramadol 50–100 mg TDS/QID\n• Codeine 30–60 mg QID\n• Buprenorphine SL 0.2 mg TDS\n\n+ Step 1 drug (multimodal)\n\n<i>± Adjuvants</i>", table_cell_style),
     Paragraph("• Morphine 5–10 mg IV/IM/SC\n• Fentanyl IV (titrated)\n• Pethidine 50–100 mg IM\n• Oxycodone PO/IV\n• PCA (patient-controlled)\n\n+ Step 1 drug\n<i>± Adjuvants</i>", table_cell_style),
    ],
    [Paragraph("<font color='#15803d'><b>GREEN — Start here\nfor mild/outpatient</b></font>", table_cell_center),
     Paragraph("<font color='#b45309'><b>AMBER — Add weak\nopioid</b></font>", table_cell_center),
     Paragraph("<font color='#b91c1c'><b>RED — Strong opioid\n± regional block</b></font>", table_cell_center)],
]
ladder_table = Table(ladder_data, colWidths=[5.8*cm, 5.8*cm, 5.8*cm])
ladder_table.setStyle(TableStyle([
    ("BACKGROUND",    (0,0), (0,0), STEP1_COL),
    ("BACKGROUND",    (1,0), (1,0), STEP2_COL),
    ("BACKGROUND",    (2,0), (2,0), STEP3_COL),
    ("BACKGROUND",    (0,1), (0,1), colors.HexColor("#dcfce7")),
    ("BACKGROUND",    (1,1), (1,1), colors.HexColor("#fef9c3")),
    ("BACKGROUND",    (2,1), (2,1), colors.HexColor("#fee2e2")),
    ("BACKGROUND",    (0,2), (0,2), colors.HexColor("#f0fdf4")),
    ("BACKGROUND",    (1,2), (1,2), colors.HexColor("#fffbeb")),
    ("BACKGROUND",    (2,2), (2,2), colors.HexColor("#fff1f2")),
    ("GRID",          (0,0), (-1,-1), 0.5, colors.HexColor("#94a3b8")),
    ("TOPPADDING",    (0,0), (-1,-1), 8),
    ("BOTTOMPADDING", (0,0), (-1,-1), 8),
    ("LEFTPADDING",   (0,0), (-1,-1), 8),
    ("VALIGN",        (0,0), (-1,-1), "TOP"),
]))
story.append(ladder_table)
story.append(Spacer(1, 0.15*cm))
story.append(Paragraph("↑ Titrate UP if pain uncontrolled after 30–60 min reassessment   |   ↓ Step DOWN as pain improves", small))
story.append(Spacer(1, 0.35*cm))

# ── SECTION 3: ANALGESIC CHOICE BY SETTING ───────────────────────
story.append(section_banner("3.  ANALGESIC CHOICE BY CLINICAL SETTING", bg=TEAL))
story.append(Spacer(1, 0.2*cm))

setting_data = [
    [Paragraph("<b>Setting</b>", table_header_style),
     Paragraph("<b>Preferred Analgesics</b>", table_header_style),
     Paragraph("<b>Avoid / Caution</b>", table_header_style)],
    [Paragraph("Outpatient\n(mild hernia, wound pain)", table_cell_style),
     Paragraph("PCM 650mg TDS + Ibuprofen 400mg TDS (with PPI)", table_cell_style),
     Paragraph("Strong opioids", table_cell_style)],
    [Paragraph("Admitted — pre-op\n(awaiting surgery)", table_cell_style),
     Paragraph("PCM IV/PO + Tramadol 50–100mg BD/TDS + Ondansetron", table_cell_style),
     Paragraph("NSAIDs if pre-op (bleeding risk)\nMorphine if bowel obs query", table_cell_style)],
    [Paragraph("Intra-operative\n(GA/regional)", table_cell_style),
     Paragraph("Fentanyl IV + Ketorolac IV + local infiltration\nNeuraxial (spinal/epidural)", table_cell_style),
     Paragraph("Tramadol intra-op (PONV risk)", table_cell_style)],
    [Paragraph("Post-operative\nDay 0–1 (severe)", table_cell_style),
     Paragraph("Morphine PCA or IV PRN + regular PCM IV\n+ Ketorolac 15–30mg IV TDS\n+ Ondansetron 4mg TDS", table_cell_style),
     Paragraph("NSAIDs if renal risk, anastomosis, elderly", table_cell_style)],
    [Paragraph("Post-operative\nDay 2–3 (moderate)", table_cell_style),
     Paragraph("Step down: Tramadol PO + PCM 650mg TDS + Diclofenac 50mg TDS", table_cell_style),
     Paragraph("Avoid strong opioids if mobilising", table_cell_style)],
    [Paragraph("Post-operative\nDay 4+ (mild, discharge)", table_cell_style),
     Paragraph("PCM 650mg TDS ± Ibuprofen 400mg TDS (short course)", table_cell_style),
     Paragraph("Opioids (constipation, falls risk)", table_cell_style)],
    [Paragraph("Paediatric\n(<12 years)", table_cell_style),
     Paragraph("PCM weight-based (15mg/kg QID)\nIbuprofen 5–10mg/kg TDS\nMorphine IV 0.05–0.1mg/kg PRN", table_cell_style),
     Paragraph("Tramadol, Codeine\n(respiratory risk)", table_cell_style)],
    [Paragraph("Elderly (>65 years)", table_cell_style),
     Paragraph("PCM 500–650mg TDS (first choice)\nReduce opioid doses by 25–50%", table_cell_style),
     Paragraph("NSAIDs (renal, GI, CV risk)\nTramadol (delirium, falls, seizure)", table_cell_style)],
    [Paragraph("Renal Impairment", table_cell_style),
     Paragraph("PCM (safest)\nFentanyl (safest opioid)\nHydromorphone", table_cell_style),
     Paragraph("NSAIDs (absolute)\nMorphine (active metabolite M6G accumulates)\nTramadol (reduce dose/interval)", table_cell_style)],
    [Paragraph("Hepatic Impairment", table_cell_style),
     Paragraph("NSAIDs (short-term, mild disease)\nTramadol (reduce dose)\nFentanyl", table_cell_style),
     Paragraph("PCM — REDUCE dose (max 2g/day)\nMorphine (encephalopathy risk)", table_cell_style)],
    [Paragraph("Peptic Ulcer / GI bleed", table_cell_style),
     Paragraph("PCM (safest)\nTramadol (if needed)\nWeak opioids", table_cell_style),
     Paragraph("All NSAIDs (absolute contraindication without PPI cover)", table_cell_style)],
    [Paragraph("Pregnancy\n(surgical emergency)", table_cell_style),
     Paragraph("PCM (all trimesters — safest)\nMorphine (short-term, supervised)\nRegional anaesthesia preferred", table_cell_style),
     Paragraph("NSAIDs (3rd trimester: premature closure ductus arteriosus)\nTramadol, Codeine (avoid near term)", table_cell_style)],
    [Paragraph("Epilepsy", table_cell_style),
     Paragraph("PCM, NSAIDs, Morphine, Fentanyl", table_cell_style),
     Paragraph("Tramadol (lowers seizure threshold — CONTRAINDICATED)\nPethidine (norpethidine → seizures)", table_cell_style)],
]
setting_table = Table(setting_data, colWidths=[3.8*cm, 7.2*cm, 6.4*cm])
setting_table.setStyle(TableStyle([
    ("BACKGROUND",    (0,0), (-1,0), TEAL),
    ("ROWBACKGROUNDS",(0,1), (-1,-1), [WHITE, LIGHT_GRAY]),
    ("GRID",          (0,0), (-1,-1), 0.4, colors.HexColor("#cbd5e1")),
    ("TOPPADDING",    (0,0), (-1,-1), 5),
    ("BOTTOMPADDING", (0,0), (-1,-1), 5),
    ("LEFTPADDING",   (0,0), (-1,-1), 5),
    ("VALIGN",        (0,0), (-1,-1), "TOP"),
]))
story.append(setting_table)
story.append(Spacer(1, 0.35*cm))

# ── PAGE BREAK ────────────────────────────────────────────────────
story.append(PageBreak())

# ── SECTION 4: DRUG PROFILES ─────────────────────────────────────
story.append(section_banner("4.  KEY DRUG PROFILES", bg=PURPLE))
story.append(Spacer(1, 0.2*cm))

drug_data = [
    [Paragraph("<b>Drug</b>", table_header_style),
     Paragraph("<b>Class</b>", table_header_style),
     Paragraph("<b>Dose (Adult)</b>", table_header_style),
     Paragraph("<b>Onset</b>", table_header_style),
     Paragraph("<b>Contraindications</b>", table_header_style),
     Paragraph("<b>Key Cautions</b>", table_header_style)],
    [Paragraph("Paracetamol\n(PCM)", table_cell_style),
     Paragraph("Non-opioid\nCNS-COX inhibitor", table_cell_style),
     Paragraph("PO: 500–1000mg QID\nIV: 1g QID\nMax 4g/day", table_cell_style),
     Paragraph("PO: 30–60 min\nIV: 5–10 min", table_cell_style),
     Paragraph("Severe hepatic failure", table_cell_style),
     Paragraph("Reduce to 2g/day in hepatic/renal impairment, elderly, alcohol use", table_cell_style)],
    [Paragraph("Ibuprofen", table_cell_style),
     Paragraph("NSAID\n(non-selective COX-1/2)", table_cell_style),
     Paragraph("400–600 mg TDS\nAfter meals\n(max 2.4g/day)", table_cell_style),
     Paragraph("PO: 30 min", table_cell_style),
     Paragraph("PUD, GI bleed, CKD, 3rd trimester, post-CABG, severe asthma", table_cell_style),
     Paragraph("Always add PPI; avoid pre-op; CV risk with long-term use", table_cell_style)],
    [Paragraph("Diclofenac", table_cell_style),
     Paragraph("NSAID\n(COX-1/2)", table_cell_style),
     Paragraph("50 mg TDS PO\n75mg IM BD\nSR 75mg BD", table_cell_style),
     Paragraph("PO: 30 min\nIM: 15 min", table_cell_style),
     Paragraph("PUD, CKD, GI bleed, 3rd trimester", table_cell_style),
     Paragraph("Highest CV risk among NSAIDs; hepatotoxicity risk", table_cell_style)],
    [Paragraph("Ketorolac", table_cell_style),
     Paragraph("NSAID\n(potent, parenteral)", table_cell_style),
     Paragraph("15–30 mg IV/IM TDS\nMax 5 days", table_cell_style),
     Paragraph("IV: 5–10 min", table_cell_style),
     Paragraph("CKD, PUD, bleeding disorder, pre-op, >65 yrs (15mg max)", table_cell_style),
     Paragraph("Best parenteral NSAID for post-op; strictly max 5 days", table_cell_style)],
    [Paragraph("Tramadol", table_cell_style),
     Paragraph("Weak opioid\n+ SNRI", table_cell_style),
     Paragraph("50–100 mg TDS/QID\nIV/IM: 50–100mg slow\nMax 400mg/day", table_cell_style),
     Paragraph("PO: 30–60 min\nIV: 10–20 min", table_cell_style),
     Paragraph("Epilepsy, MAOI use, severe respiratory depression, age <12", table_cell_style),
     Paragraph("Serotonin syndrome with SSRIs; PONV; seizure threshold↓; abuse potential", table_cell_style)],
    [Paragraph("Codeine", table_cell_style),
     Paragraph("Weak opioid\n(prodrug → morphine)", table_cell_style),
     Paragraph("30–60 mg QID\n(max 240mg/day)", table_cell_style),
     Paragraph("PO: 30–45 min", table_cell_style),
     Paragraph("Children post-tonsillectomy, CYP2D6 poor metabolisers, breastfeeding", table_cell_style),
     Paragraph("Ultra-rapid metabolisers: risk of toxicity; ineffective in poor metabolisers", table_cell_style)],
    [Paragraph("Morphine", table_cell_style),
     Paragraph("Strong opioid\n(μ-agonist)", table_cell_style),
     Paragraph("IV: 2–5 mg PRN\nIM/SC: 5–10 mg\nPO: 5–10 mg 4-hrly", table_cell_style),
     Paragraph("IV: 5 min\nIM: 15–20 min\nPO: 30–60 min", table_cell_style),
     Paragraph("Severe respiratory depression, head injury, MAOIs, paralytic ileus", table_cell_style),
     Paragraph("Avoid in CKD (M6G accumulates); always have naloxone available", table_cell_style)],
    [Paragraph("Fentanyl", table_cell_style),
     Paragraph("Strong opioid\n(lipophilic μ-agonist)", table_cell_style),
     Paragraph("IV: 1–2 mcg/kg\nPatch: 12–100 mcg/h\nIN: 1–2 mcg/kg", table_cell_style),
     Paragraph("IV: 1–2 min\nPatch: 12–24h", table_cell_style),
     Paragraph("Severe respiratory depression; opioid-naive patients (patch)", table_cell_style),
     Paragraph("Safest opioid in renal/hepatic impairment; chest wall rigidity with rapid IV", table_cell_style)],
    [Paragraph("Ondansetron\n(Emset)", table_cell_style),
     Paragraph("5-HT3 antagonist\n(antiemetic)", table_cell_style),
     Paragraph("4–8 mg IV/IM/PO TDS", table_cell_style),
     Paragraph("IV: <5 min", table_cell_style),
     Paragraph("Prolonged QT syndrome", table_cell_style),
     Paragraph("ECG check in cardiac patients; co-prescribe with opioids routinely", table_cell_style)],
]
drug_table = Table(drug_data, colWidths=[2.3*cm, 2.3*cm, 3*cm, 1.8*cm, 4*cm, 4*cm])
drug_table.setStyle(TableStyle([
    ("BACKGROUND",    (0,0), (-1,0), PURPLE),
    ("ROWBACKGROUNDS",(0,1), (-1,-1), [WHITE, colors.HexColor("#f5f3ff")]),
    ("GRID",          (0,0), (-1,-1), 0.4, colors.HexColor("#ddd6fe")),
    ("TOPPADDING",    (0,0), (-1,-1), 5),
    ("BOTTOMPADDING", (0,0), (-1,-1), 5),
    ("LEFTPADDING",   (0,0), (-1,-1), 4),
    ("VALIGN",        (0,0), (-1,-1), "TOP"),
    ("FONTSIZE",      (0,0), (-1,-1), 8),
]))
story.append(drug_table)
story.append(Spacer(1, 0.35*cm))

# ── SECTION 5: MULTIMODAL ANALGESIA ──────────────────────────────
story.append(section_banner("5.  MULTIMODAL ANALGESIA (ERAS PRINCIPLE)", bg=ORANGE))
story.append(Spacer(1, 0.2*cm))

story.append(Paragraph(
    "Combining analgesics from different classes achieves better pain control at lower doses of each drug, "
    "reducing side effects. This is the cornerstone of Enhanced Recovery After Surgery (ERAS) protocols.",
    body))

mm_data = [
    [Paragraph("<b>Component</b>", table_header_style),
     Paragraph("<b>Examples</b>", table_header_style),
     Paragraph("<b>Benefit</b>", table_header_style)],
    [Paragraph("Non-opioid base", table_cell_style),
     Paragraph("PCM + NSAID (or COX-2 inhibitor)", table_cell_style),
     Paragraph("Reduces opioid requirement by 20–30%", table_cell_style)],
    [Paragraph("Regional / neuraxial", table_cell_style),
     Paragraph("Spinal, epidural, TAP block, wound infiltration", table_cell_style),
     Paragraph("Excellent local/regional control, opioid-sparing", table_cell_style)],
    [Paragraph("Weak opioid", table_cell_style),
     Paragraph("Tramadol, Codeine", table_cell_style),
     Paragraph("Bridges moderate pain; PRN basis", table_cell_style)],
    [Paragraph("Adjuvants", table_cell_style),
     Paragraph("Gabapentin 300mg pre-op, Ketamine sub-anaesthetic IV,\nDexamethasone, Clonidine", table_cell_style),
     Paragraph("Preemptive analgesia, anti-hyperalgesia, PONV reduction", table_cell_style)],
    [Paragraph("Strong opioid (PRN only)", table_cell_style),
     Paragraph("Morphine PCA, Fentanyl IV", table_cell_style),
     Paragraph("Rescue analgesia for breakthrough severe pain", table_cell_style)],
]
mm_table = Table(mm_data, colWidths=[3.5*cm, 7.5*cm, 6.4*cm])
mm_table.setStyle(TableStyle([
    ("BACKGROUND",    (0,0), (-1,0), ORANGE),
    ("ROWBACKGROUNDS",(0,1), (-1,-1), [WHITE, colors.HexColor("#fff7ed")]),
    ("GRID",          (0,0), (-1,-1), 0.4, colors.HexColor("#fed7aa")),
    ("TOPPADDING",    (0,0), (-1,-1), 5),
    ("BOTTOMPADDING", (0,0), (-1,-1), 5),
    ("LEFTPADDING",   (0,0), (-1,-1), 6),
    ("VALIGN",        (0,0), (-1,-1), "TOP"),
]))
story.append(mm_table)
story.append(Spacer(1, 0.35*cm))

# ── SECTION 6: SPECIAL SITUATIONS – QUICK DECISION TABLE ─────────
story.append(section_banner("6.  SPECIAL SITUATIONS — QUICK REFERENCE", bg=colors.HexColor("#0f766e")))
story.append(Spacer(1, 0.2*cm))

sp_data = [
    [Paragraph("<b>Situation</b>", table_header_style),
     Paragraph("<b>Use</b>", table_header_style),
     Paragraph("<b>Avoid</b>", table_header_style),
     Paragraph("<b>Notes</b>", table_header_style)],
    [Paragraph("Seizure disorder", table_cell_style),
     Paragraph("PCM, NSAIDs, Fentanyl, Morphine", table_cell_style),
     Paragraph("Tramadol, Pethidine", table_cell_style),
     Paragraph("Both lower seizure threshold", table_cell_style)],
    [Paragraph("On SSRIs / SNRIs", table_cell_style),
     Paragraph("PCM, NSAIDs, Morphine", table_cell_style),
     Paragraph("Tramadol, Pethidine", table_cell_style),
     Paragraph("Serotonin syndrome risk", table_cell_style)],
    [Paragraph("On MAOIs", table_cell_style),
     Paragraph("PCM, Morphine (caution)", table_cell_style),
     Paragraph("Tramadol, Pethidine (ABSOLUTE)", table_cell_style),
     Paragraph("Hypertensive crisis, serotonin syndrome", table_cell_style)],
    [Paragraph("Asthma (NSAID-sensitive)", table_cell_style),
     Paragraph("PCM, Opioids (titrated)", table_cell_style),
     Paragraph("Aspirin, NSAIDs (Samter's triad)", table_cell_style),
     Paragraph("~10% asthmatics are NSAID-sensitive", table_cell_style)],
    [Paragraph("Anticoagulated patient", table_cell_style),
     Paragraph("PCM, Opioids", table_cell_style),
     Paragraph("NSAIDs, Ketorolac (bleeding risk)", table_cell_style),
     Paragraph("Check INR before any procedural analgesia", table_cell_style)],
    [Paragraph("NPO / cannot swallow", table_cell_style),
     Paragraph("PCM IV, Morphine IV, Fentanyl IV,\nKetorolac IV, regional block", table_cell_style),
     Paragraph("Oral formulations", table_cell_style),
     Paragraph("IV PCM = fastest non-opioid onset", table_cell_style)],
    [Paragraph("Suspected bowel obstruction", table_cell_style),
     Paragraph("Fentanyl IV (least gut motility effect)\nRegional/neuraxial", table_cell_style),
     Paragraph("Morphine, Codeine (reduce gut motility)\nNSAIDs (if perforation risk)", table_cell_style),
     Paragraph("Adequate analgesia does NOT mask diagnosis; provide it", table_cell_style)],
]
sp_table = Table(sp_data, colWidths=[3.2*cm, 5*cm, 4.8*cm, 4.4*cm])
sp_table.setStyle(TableStyle([
    ("BACKGROUND",    (0,0), (-1,0), colors.HexColor("#0f766e")),
    ("ROWBACKGROUNDS",(0,1), (-1,-1), [WHITE, colors.HexColor("#f0fdfa")]),
    ("GRID",          (0,0), (-1,-1), 0.4, colors.HexColor("#99f6e4")),
    ("TOPPADDING",    (0,0), (-1,-1), 5),
    ("BOTTOMPADDING", (0,0), (-1,-1), 5),
    ("LEFTPADDING",   (0,0), (-1,-1), 5),
    ("VALIGN",        (0,0), (-1,-1), "TOP"),
]))
story.append(sp_table)
story.append(Spacer(1, 0.3*cm))

# ── SECTION 7: RED FLAGS & OPIOID MONITORING ─────────────────────
story.append(PageBreak())
story.append(section_banner("7.  RED FLAGS — OPIOID SIDE EFFECTS & MONITORING", bg=colors.HexColor("#b91c1c")))
story.append(Spacer(1, 0.2*cm))

rf_data = [
    [Paragraph("<b>Side Effect</b>", table_header_style),
     Paragraph("<b>Sign / Symptom</b>", table_header_style),
     Paragraph("<b>Action</b>", table_header_style)],
    [Paragraph("Respiratory depression\n(MOST DANGEROUS)", table_cell_style),
     Paragraph("RR <10/min, SpO2 <92%, drowsy, miosis", table_cell_style),
     Paragraph("STOP opioid. O2. Naloxone 0.4mg IV, repeat every 2–3 min (max 10mg). Call anaesthesia.", table_cell_style)],
    [Paragraph("Oversedation", table_cell_style),
     Paragraph("RASS score < -2, unrousable", table_cell_style),
     Paragraph("Hold dose. Stimulate. Naloxone if severe. Monitor vitals.", table_cell_style)],
    [Paragraph("Nausea / Vomiting", table_cell_style),
     Paragraph("Post-dose nausea, retching", table_cell_style),
     Paragraph("Ondansetron 4mg IV. Consider antiemetic pre-medication with next dose.", table_cell_style)],
    [Paragraph("Urinary retention", table_cell_style),
     Paragraph("Inability to void, bladder distension", table_cell_style),
     Paragraph("Catheterise. Consider reducing opioid. Tamsulosin in males.", table_cell_style)],
    [Paragraph("Opioid-induced constipation", table_cell_style),
     Paragraph("No bowel movement >3 days, bloating", table_cell_style),
     Paragraph("Lactulose BD + Senna TDS. Consider methylnaltrexone if refractory.", table_cell_style)],
    [Paragraph("Pruritus (itching)", table_cell_style),
     Paragraph("Generalised itch, especially with spinal opioids", table_cell_style),
     Paragraph("Naloxone low dose (0.04mg IV). Chlorphenamine. Rotate opioid.", table_cell_style)],
    [Paragraph("Serotonin Syndrome\n(Tramadol + SSRI)", table_cell_style),
     Paragraph("Fever, agitation, tremor, myoclonus, hyperreflexia, tachycardia", table_cell_style),
     Paragraph("STOP offending drug. Cyproheptadine 4–8mg. ICU if severe.", table_cell_style)],
]
rf_table = Table(rf_data, colWidths=[3.5*cm, 6*cm, 7.9*cm])
rf_table.setStyle(TableStyle([
    ("BACKGROUND",    (0,0), (-1,0), colors.HexColor("#b91c1c")),
    ("ROWBACKGROUNDS",(0,1), (-1,-1), [WHITE, colors.HexColor("#fff1f2")]),
    ("GRID",          (0,0), (-1,-1), 0.4, colors.HexColor("#fca5a5")),
    ("TOPPADDING",    (0,0), (-1,-1), 5),
    ("BOTTOMPADDING", (0,0), (-1,-1), 5),
    ("LEFTPADDING",   (0,0), (-1,-1), 5),
    ("VALIGN",        (0,0), (-1,-1), "TOP"),
]))
story.append(rf_table)
story.append(Spacer(1, 0.35*cm))

# ── SECTION 8: STEP-DOWN PROTOCOL ────────────────────────────────
story.append(section_banner("8.  POST-OPERATIVE STEP-DOWN PROTOCOL", bg=MED_BLUE))
story.append(Spacer(1, 0.2*cm))

stepdown_data = [
    [Paragraph("<b>POD</b>", table_header_style),
     Paragraph("<b>Pain Level</b>", table_header_style),
     Paragraph("<b>Recommended Regimen</b>", table_header_style),
     Paragraph("<b>Route</b>", table_header_style)],
    [Paragraph("Day 0\n(0–6h post-op)", table_cell_style),
     Paragraph("Severe\n(NRS 7–10)", table_cell_style),
     Paragraph("PCM 1g IV QID + Ketorolac 15–30mg IV TDS + Morphine IV PRN (PCA)\n+ Ondansetron 4mg TDS", table_cell_style),
     Paragraph("IV", table_cell_style)],
    [Paragraph("Day 0–1\n(6–24h post-op)", table_cell_style),
     Paragraph("Moderate-Severe\n(NRS 5–7)", table_cell_style),
     Paragraph("PCM 1g IV QID + Ketorolac 30mg IV TDS + Tramadol 50–100mg IV PRN", table_cell_style),
     Paragraph("IV", table_cell_style)],
    [Paragraph("Day 1–2", table_cell_style),
     Paragraph("Moderate\n(NRS 4–6)", table_cell_style),
     Paragraph("PCM 650mg PO QID + Ibuprofen 400mg PO TDS + Tramadol 50mg PO TDS PRN", table_cell_style),
     Paragraph("Oral", table_cell_style)],
    [Paragraph("Day 3–4", table_cell_style),
     Paragraph("Mild-Moderate\n(NRS 3–5)", table_cell_style),
     Paragraph("PCM 650mg TDS + Ibuprofen 400mg TDS (both after meals + PPI)", table_cell_style),
     Paragraph("Oral", table_cell_style)],
    [Paragraph("Day 5+\n(Discharge)", table_cell_style),
     Paragraph("Mild\n(NRS 1–3)", table_cell_style),
     Paragraph("PCM 500–650mg TDS PRN. Advise stop NSAIDs after 5 days.", table_cell_style),
     Paragraph("Oral", table_cell_style)],
]
stepdown_table = Table(stepdown_data, colWidths=[2.5*cm, 2.8*cm, 9.1*cm, 3*cm])
stepdown_table.setStyle(TableStyle([
    ("BACKGROUND",    (0,0), (-1,0), MED_BLUE),
    ("ROWBACKGROUNDS",(0,1), (-1,-1), [colors.HexColor("#fee2e2"), colors.HexColor("#fef9c3"),
                                        colors.HexColor("#fef9c3"), colors.HexColor("#dcfce7"),
                                        colors.HexColor("#dcfce7")]),
    ("GRID",          (0,0), (-1,-1), 0.4, colors.HexColor("#cbd5e1")),
    ("TOPPADDING",    (0,0), (-1,-1), 5),
    ("BOTTOMPADDING", (0,0), (-1,-1), 5),
    ("LEFTPADDING",   (0,0), (-1,-1), 5),
    ("VALIGN",        (0,0), (-1,-1), "TOP"),
]))
story.append(stepdown_table)
story.append(Spacer(1, 0.35*cm))

# ── SECTION 9: QUICK MEMORY AIDS ─────────────────────────────────
story.append(section_banner("9.  QUICK MEMORY AIDS", bg=colors.HexColor("#7c3aed")))
story.append(Spacer(1, 0.2*cm))

aids = [
    ("<b>PCM is ALWAYS safe first</b> — unless severe hepatic failure. Start there for every surgical patient.",  WHITE, colors.HexColor("#4c1d95")),
    ("<b>No NSAIDs in 4 Ks:</b> Kidney disease · Known PUD · Koagulation (bleeding) · K-trimester (3rd)", WHITE, colors.HexColor("#1e40af")),
    ("<b>No Tramadol in 4 Ss:</b> Seizures · SSRIs on board · Severe resp. depression · age <12 (kids)", WHITE, colors.HexColor("#065f46")),
    ("<b>No Pethidine in CNS:</b> Norpethidine metabolite causes seizures & CNS excitation. Avoid in elderly and renal failure.", WHITE, colors.HexColor("#7f1d1d")),
    ("<b>Fentanyl = safest opioid in organ impairment</b> (renal or hepatic) — hepatic metabolism, inactive metabolites", WHITE, colors.HexColor("#713f12")),
    ("<b>Multimodal always beats monotherapy</b> — combine PCM + NSAID + weak opioid to minimise each drug's dose", WHITE, colors.HexColor("#1e3a5f")),
]
for text, fg, bg in aids:
    row = Table([[Paragraph(text, ParagraphStyle("aid", fontSize=9, textColor=fg,
                fontName="Helvetica-Bold", leading=13, leftIndent=6))]], colWidths=[17.4*cm])
    row.setStyle(TableStyle([
        ("BACKGROUND",    (0,0), (-1,-1), bg),
        ("TOPPADDING",    (0,0), (-1,-1), 6),
        ("BOTTOMPADDING", (0,0), (-1,-1), 6),
        ("LEFTPADDING",   (0,0), (-1,-1), 10),
        ("BOTTOMMARGIN",  (0,0), (-1,-1), 3),
    ]))
    story.append(row)
    story.append(Spacer(1, 0.1*cm))

story.append(Spacer(1, 0.3*cm))

# ── FOOTER ────────────────────────────────────────────────────────
footer_data = [[
    Paragraph(
        "References: Bailey & Love's Short Practice of Surgery 28e · Fischer's Mastery of Surgery 8e · "
        "Schwartz's Principles of Surgery 11e · WHO Analgesic Ladder · ERAS Society Guidelines 2023  |  "
        "For clinical use by licensed practitioners only. Always verify doses against current formulary.",
        ParagraphStyle("foot", fontSize=7.5, textColor=WHITE, fontName="Helvetica-Oblique",
                       alignment=TA_CENTER, leading=11))
]]
footer = Table(footer_data, colWidths=[17.4*cm])
footer.setStyle(TableStyle([
    ("BACKGROUND",    (0,0), (-1,-1), DARK_BLUE),
    ("TOPPADDING",    (0,0), (-1,-1), 7),
    ("BOTTOMPADDING", (0,0), (-1,-1), 7),
    ("LEFTPADDING",   (0,0), (-1,-1), 10),
]))
story.append(footer)

# ── BUILD ─────────────────────────────────────────────────────────
doc.build(story)
print(f"PDF saved to: {OUTPUT}")
Running Command

cd /home/daytona/workspace/analgesic-pdf && python generate_pdf.py

Your PDF is ready:

Analgesic Choice Algorithm Surgical Pain

PDF Document · PDF


What's inside the PDF (3 pages):

SectionContent
1. Pain AssessmentVAS, NRS, Wong-Baker FACES, CPOT tools with scoring
2. WHO Analgesic Ladder3-step ladder adapted for surgical pain with colour coding
3. Choice by Clinical SettingOutpatient → Pre-op → Intra-op → Post-op Day 0-4 → Discharge; plus Paediatric, Elderly, Renal, Hepatic, Pregnancy, Epilepsy
4. Drug ProfilesPCM, Ibuprofen, Diclofenac, Ketorolac, Tramadol, Codeine, Morphine, Fentanyl, Ondansetron - doses, onset, contraindications
5. Multimodal AnalgesiaERAS principle - non-opioid base + regional + adjuvants
6. Special SituationsSeizures, SSRIs, MAOIs, Asthma, Anticoagulation, NPO, Bowel obstruction
7. Red FlagsOpioid side effects and emergency management (including naloxone protocol)
8. Step-Down ProtocolPOD 0 to discharge - exact regimens with route
9. Quick Memory Aids"No NSAIDs in 4 Ks", "No Tramadol in 4 Ss", colour-coded rules
This is a shared conversation. Sign in to Orris to start your own chat.