File size: 27,251 Bytes
0aa6283
 
 
7bfba03
 
618422f
2de6512
618422f
 
0aa6283
7bfba03
0aa6283
618422f
0aa6283
 
97ef44b
 
 
 
 
 
 
0aa6283
 
 
 
 
b677163
 
0aa6283
b677163
f92edaa
 
0aa6283
 
2de6512
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f3fcdf
0aa6283
 
 
 
 
7bfba03
 
 
 
 
 
 
 
 
 
 
0aa6283
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b677163
 
0aa6283
 
b677163
0aa6283
 
b677163
 
 
 
 
 
0aa6283
 
b677163
0aa6283
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fdb7966
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d03d1fd
 
fdb7966
 
 
 
 
 
d03d1fd
 
 
 
fdb7966
 
 
 
 
 
 
0aa6283
fdb7966
 
 
 
0aa6283
97ef44b
fdb7966
 
 
 
 
 
 
 
 
d03d1fd
 
fdb7966
 
d03d1fd
fdb7966
d03d1fd
 
fdb7966
 
 
d03d1fd
90985ea
 
fdb7966
90985ea
 
 
 
 
fdb7966
 
 
 
90985ea
 
d03d1fd
fdb7966
97ef44b
d03d1fd
b677163
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0aa6283
a74c82c
2f3fcdf
 
 
fa3f286
0aa6283
 
 
 
 
 
fa3f286
 
 
fc82d73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f3fcdf
fc82d73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a43d22
 
 
fc82d73
 
 
 
 
 
 
fa3f286
fc82d73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0aa6283
 
2f3fcdf
0aa6283
 
2f3fcdf
 
0aa6283
fc82d73
 
 
2f3fcdf
fc82d73
9fd7a03
fa3f286
 
 
 
 
 
1f8721e
 
fa3f286
 
 
 
 
0aa6283
425719f
fa3f286
fc82d73
 
 
2f3fcdf
fc82d73
 
fa3f286
fc82d73
2f3fcdf
fc82d73
2f3fcdf
f92edaa
2de6512
 
 
fc82d73
 
 
 
 
 
f92edaa
 
2de6512
 
 
f92edaa
fa3f286
 
 
fc82d73
fa3f286
9fd7a03
fa3f286
fc82d73
9fd7a03
 
 
 
 
 
fa3f286
9fd7a03
fa3f286
9fd7a03
 
fa3f286
0aa6283
fa3f286
9fd7a03
fc82d73
9fd7a03
fc82d73
9fd7a03
 
fa3f286
9fd7a03
fc82d73
9fd7a03
 
 
 
fc82d73
9fd7a03
fc82d73
9fd7a03
fc82d73
9fd7a03
 
fa3f286
9fd7a03
0aa6283
fa3f286
fc82d73
 
 
2f3fcdf
fc82d73
0aa6283
 
fa3f286
0aa6283
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
618422f
0aa6283
 
 
 
 
 
 
 
 
 
 
618422f
 
 
 
 
 
0aa6283
 
 
 
7bfba03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0aa6283
97ef44b
0aa6283
f92edaa
7bfba03
 
 
 
 
0aa6283
 
 
 
 
 
 
 
f92edaa
0aa6283
 
 
 
 
 
618422f
b677163
0aa6283
 
 
 
b677163
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
import streamlit as st
from typing import List, Dict, Any, Optional, Tuple
import re
import os
from utils import db as dbapi
from utils import api as backend_api # unified backend client
from phase.Student_view import quiz as quiz_page



USE_LOCAL_DB = os.getenv("DISABLE_DB", "1") != "1"


FALLBACK_TAG = "<!--fallback-->"

# --- Load external CSS (optional) ---
def load_css(file_name: str):
    try:
        with open(file_name, "r", encoding="utf-8") as f:
            st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
    except FileNotFoundError:
        st.warning("⚠️ Stylesheet not found. Please ensure 'assets/styles.css' exists.")
# ---------------------------------------------
# Page state helpers
# ---------------------------------------------
_SS_DEFAULTS = {
    "level": "beginner",              # beginner | intermediate | advanced
    "module_id": None,                # int (1-based)
    "topic_idx": 0,                   # 0-based within module
    "mode": "catalog",                # catalog | lesson | quiz | results
    "topics_cache": {},               # {(level, module_id): [(title, text), ...]}
    "show_quiz_prompt": False,        # <-- controls the post-module popup
    "selected_quiz": None,
}

# Works on Streamlit versions that support st.dialog
if hasattr(st, "dialog"):
    @st.dialog("Ready for a quick check-in?")
    def _quiz_prompt_dialog():
        st.write("Would you like to do **Quiz 1** to strengthen your knowledge?")
        c1, c2 = st.columns(2)
        if c1.button("Yes, start Quiz 1", key="dlg_yes"):
            st.session_state.show_quiz_prompt = False
            st.session_state.mode = "quiz"
            st.session_state.selected_quiz = 1
            st.session_state.current_q = 0
            st.session_state.answers = {}
            st.rerun()
        if c2.button("Maybe later", key="dlg_no"):
            st.session_state.show_quiz_prompt = False
            st.session_state.mode = "catalog"
            st.session_state.module_id = None
            st.session_state.topic_idx = 0
            st.rerun()
else:
    # Fallback: inline banner if st.dialog isn't available
    def _quiz_prompt_dialog():
        st.info("Would you like to do **Quiz 1** to strengthen your knowledge?")
        c1, c2 = st.columns(2)
        if c1.button("Yes, start Quiz 1", key="inline_yes"):
            st.session_state.mode = "quiz"
            st.session_state.selected_quiz = 1
            st.session_state.current_q = 0
            st.session_state.answers = {}
            st.rerun()
        if c2.button("Maybe later", key="inline_no"):
            st.session_state.mode = "catalog"
            st.session_state.module_id = None
            st.session_state.topic_idx = 0
            st.rerun()


def _ensure_state():
    for k, v in _SS_DEFAULTS.items():
        if k not in st.session_state:
            st.session_state[k] = v

def _fetch_assigned_lesson(lesson_id: int) -> dict:
    """
    Returns {"lesson": {...}, "sections": [...]}
    Falls back to backend_api if you're not using the local DB.
    """
    if USE_LOCAL_DB and hasattr(dbapi, "get_lesson"):
        return dbapi.get_lesson(lesson_id) or {}
    try:
        return backend_api.get_lesson(lesson_id) or {}
    except Exception:
        return {}

# ---------------------------------------------
# Content metadata (UI only)
# ---------------------------------------------
MODULES_META: Dict[str, List[Dict[str, Any]]] = {
    "beginner": [
        {
            "id": 1,
            "title": "Understanding Money",
            "description": "Learn the basics of what money is, its uses, and how to manage it.",
            "duration": "20 min",
            "completed": False,
            "locked": False,
            "difficulty": "Easy",
            "topics": [
                "What is Money?",
                "Needs vs. Wants",
                "Earning Money",
                "Saving Money",
                "Spending Wisely",
                "Play: Money Match",
                "Quiz",
                "Summary: My Money Plan"
            ]
        },
        {
            "id": 2,
            "title": "Basic Budgeting",
            "description": "Start building the habit of planning and managing money through budgeting.",
            "duration": "20 min",
            "completed": False,
            "locked": False,
            "difficulty": "Easy",
            "topics": [
                "What is a Budget?",
                "Income and Expenses",
                "Profit and Loss",
                "Saving Goals",
                "Making Choices",
                "Play: Budget Builder",
                "Quiz",
                "Summary: My First Budget"
            ]
        },
        {
            "id": 3,
            "title": "Money in Action",
            "description": "Learn how money is used in everyday transactions and its role in society.",
            "duration": "20 min",
            "completed": False,
            "locked": False,
            "difficulty": "Easy",
            "topics": [
                "Paying for Things",
                "Keeping Track of Money",
                "What Are Taxes?",
                "Giving and Sharing",
                "Money Safety",
                "Play: Piggy Bank Challenge",
                "Quiz",
                "Summary: Money Journal"
            ]
        },
        {
            "id": 4,
            "title": "Simple Business Ideas",
            "description": "Explore the basics of starting a small business and earning profit.",
            "duration": "20 min",
            "completed": False,
            "locked": False,
            "difficulty": "Easy",
            "topics": [
                "What is a Business?",
                "Costs in a Business",
                "Revenue in a Business",
                "Profit in a Business",
                "Advertising Basics",
                "Play: Smart Shopper",
                "Quiz",
                "Summary: My Business Plan"
            ]
        }
    ],
    "intermediate": [],
    "advanced": [],
}

def _topic_plan(level: str, module_id: int):
    """
    Returns a list of (title, backend_ordinal) after filtering:
    - drop any 'Play:' topic
    - drop 'Quiz'
    - keep first five + the Summary (6 total)
    backend_ordinal is the 1-based index in the original metadata (so backend files line up).
    """
    mod = next(m for m in MODULES_META[level] if m["id"] == module_id)
    raw = (mod.get("topics") or mod.get("topic_labels") or [])
    plan = []
    for i, t in enumerate(raw, start=1):
        tl = t.strip().lower()
        if tl == "quiz" or tl.startswith("play:"):
            continue
        plan.append((t, i))

    # Ensure at most 6 topics: first five + Summary if present
    if len(plan) > 6:
        summary_pos = next((idx for idx, (title, _) in enumerate(plan)
                            if title.strip().lower().startswith("summary")), None)
        if summary_pos is not None:
            plan = plan[:5] + [plan[summary_pos]]
        else:
            plan = plan[:6]
    return plan

def _topic_titles(level: str, module_id: int):
    return [t for (t, _) in _topic_plan(level, module_id)]

# ---------------------------------------------
# Backend integrations
# ---------------------------------------------
@st.cache_data(show_spinner=False, ttl=300)
def _fetch_topic_from_backend(level: str, module_id: int, topic_idx: int) -> Tuple[str, str]:
    """
    Returns (ui_title, content). Calls the FastAPI /lesson endpoint via utils.api.fetch_lesson_content.
    The backend expects files at /app/lessons/lesson_{module_id}/topic_{ordinal}.txt
    """
    plan = _topic_plan(level, module_id)
    ui_title, backend_ordinal = plan[topic_idx]  # 1-based ordinal in original list

    try:
        content = backend_api.fetch_lesson_content(
            lesson=f"lesson_{module_id}",
            module=str(module_id),
            topic=str(backend_ordinal),
        )
        content = (content or "").strip()
    except Exception as e:
        st.warning(f"Lesson fetch failed for lesson_{module_id}/topic_{backend_ordinal}: {e}")
        content = ""

    return ui_title, content



def _fallback_text(title: str, module_id: int, topic_ordinal: int) -> str:
    """
    Minimal on-brand copy if the backend has not supplied a topic file yet.
    Tailored to beginner modules so the UI stays useful.
    """
    t = title.strip().lower()
    if "what is money" in t or "money" == t:
        return ("Money is a tool we use to trade for goods and services. "
                "In Jamaica we use JMD coins and notes, and many people also pay digitally. "
                "You can spend, save, or share money, but it is limited, so plan how you use it.") + "\n" + FALLBACK_TAG
    if "need" in t and "want" in t:
        return ("Needs keep you safe and healthy, like food, clothes, and school supplies. "
                "Wants are nice to have, like toys or snacks. Cover needs first, then plan for wants.") + "\n" + FALLBACK_TAG
    if "earn" in t:
        return ("You earn money by doing work or providing value. Small jobs add up. "
                "Earnings give you choices to spend, save, or share, and teach the value of effort.") + "\n" + FALLBACK_TAG
    if "sav" in t:
        return ("Saving means putting aside some money now for future needs or goals. "
                "Start small and be consistent. A jar, a partner plan, or a bank account all help.") + "\n" + FALLBACK_TAG
    if "spend" in t or "wisely" in t:
        return ("Spending wisely means comparing prices, making a simple budget, and avoiding impulse buys. "
                "Aim for best value so your goals stay on track.") + "\n" + FALLBACK_TAG
    if "summary" in t or "journal" in t or "plan" in t:
        return ("Quick recap: cover needs first, set a small saving goal, and make one spending rule for the week. "
                "Write one action you will try before the next lesson.") + "\n" + FALLBACK_TAG
    if "quiz" in t:
        return ("Take a short quiz to check your understanding of this lesson.") + "\n" + FALLBACK_TAG
    # Generic fallback
    return (f"This unit will be populated from lesson_{module_id}/topic_{topic_ordinal}.txt. "
            "For now, review the key idea and write one example from daily life.") + "\n" + FALLBACK_TAG

# ---------------------------------------------
# UI building blocks
# ---------------------------------------------
def _render_catalog():
    st.header("Financial Education")
    st.caption("Build your financial knowledge with structured paths for every skill level.")
    
    # Custom CSS for card styling
    st.markdown("""
    <style>
    .card {
        background: white;
        padding: 1.5rem;
        border-radius: 12px;
        border: 1px solid #e1e5e9;
        box-shadow: 0 1px 3px rgba(0,0,0,0.1);
        margin-bottom: 1rem;
        height: 100%;
    }
    .card-header {
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        margin-bottom: 1rem;
    }
    .level-badge {
        background: #f8f9fa;
        color: #6c757d;
        padding: 0.25rem 0.75rem;
        border-radius: 20px;
        font-size: 0.875rem;
        font-weight: 500;
    }
    .card-title {
        font-size: 1.25rem;
        font-weight: 600;
        color: #212529;
        margin: 0.5rem 0;
    }
    .card-description {
        color: #6c757d;
        font-size: 0.95rem;
        line-height: 1.4;
        margin-bottom: 1rem;
    }
    .card-meta {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin: 1rem 0;
        color: #6c757d;
        font-size: 0.875rem;
    }
    .meta-item {
        display: flex;
        align-items: center;
        gap: 0.25rem;
    }
    .learn-section {
        margin: 1rem 0;
    }
    .learn-title {
        font-size: 0.75rem;
        font-weight: 600;
        color: #6c757d;
        text-transform: uppercase;
        letter-spacing: 0.5px;
        margin-bottom: 0.5rem;
    }
    .learn-item {
        color: #28a745;
        font-size: 0.9rem;
        margin-bottom: 0.25rem;
    }
    .primary-button, .secondary-button {
        width: 100% !important;
        border: none !important;
        border-radius: 6px !important;
        padding: 0.75rem 1.5rem !important;
        font-weight: 500 !important;
        margin-top: 1rem !important;
    }
    .primary-button {
        background: #28a745 !important;
        color: white !important;
    }
    .secondary-button {
        background: #f8f9fa !important;
        color: #495057 !important;
        border: 1px solid #dee2e6 !important;
    }
    </style>
    """, unsafe_allow_html=True)

    level = st.session_state.get("level", "beginner")
    
    cols = st.columns(2, gap="large")
    
    for i, mod in enumerate(MODULES_META[level]):
        with cols[i % 2]:
            with st.container():
                st.markdown(f"""
                <div class="card">
                    <div class="card-header">
                        <span class="level-badge">{mod['difficulty']}</span>
                    </div>
                    <h3 class="card-title">{mod['title']}</h3>
                    <p class="card-description">{mod['description']}</p>
                    <div class="card-meta">
                        <div class="meta-item">πŸ•’ {mod['duration']}</div>
                        <div class="meta-item">πŸ‘₯ {2500 - i * 350}</div>
                    </div>
                    <div class="learn-section">
                        <div class="learn-title">You'll Learn</div>
                """, unsafe_allow_html=True)


                with st.expander("πŸ“‹ All Topics"):
                    for topic in mod['topics']:
                        st.write(f"β€’ {topic}")

                button_class = mod.get('button_style', 'primary')
                button_text = "Start Learning" if button_class == 'primary' else "Review Module"
                
                if st.button(
                    f"{button_text} β†’", 
                    key=f"start_{level}_{mod['id']}",
                    help=f"Begin the {mod['title']} module"
                ):
                    st.session_state.module_id = mod["id"]
                    st.session_state.topic_idx = 0
                    st.session_state.mode = "lesson"
                    st.rerun()
                
                st.markdown("</div>", unsafe_allow_html=True)

                st.markdown("</div>", unsafe_allow_html=True)


def _get_topics(level: str, module_id: int) -> List[Tuple[str, str]]:
    """
    Build the six-topic plan from metadata titles, try backend for each,
    and if content is missing, provide a short fallback paragraph.
    """
    cache_key = (level, module_id)
    if cache_key in st.session_state.topics_cache:
        return st.session_state.topics_cache[cache_key]

    plan = _topic_plan(level, module_id)
    out: List[Tuple[str, str]] = []

    for idx in range(len(plan)):
        title, content = _fetch_topic_from_backend(level, module_id, idx)
        if not content:
            content = _fallback_text(title, module_id, idx + 1)
        out.append((title, content))

    st.session_state.topics_cache[cache_key] = out
    return out


def _render_lesson():

    """Render the lesson interface"""
    level = st.session_state.get("level", "beginner")
    module_id = st.session_state.get("module_id")

    if module_id is None:
        st.session_state.mode = "catalog"
        st.rerun()

    mod = next(m for m in MODULES_META[level] if m["id"] == module_id)

    # -------------------------
    # Styles
    # -------------------------
    st.markdown("""
    <style>
    .lesson-header {
        font-size: 2rem;
        font-weight: 600;
        color: #1f2937;
        margin-bottom: 2rem;
    }
    .lesson-card {
        background: white;
        border: 1px solid #e5e7eb;
        border-radius: 12px;
        padding: 2rem;
        margin-bottom: 1.5rem;
        box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
        min-height: 400px;
    }
    .unit-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 1.5rem;
        padding-bottom: 1rem;
        border-bottom: 1px solid #f3f4f6;
    }
    .unit-info {
        font-size: 0.875rem;
        color: #6b7280;
        font-weight: 500;
    }
    .read-time {
        display: flex;
        align-items: center;
        gap: 0.5rem;
        font-size: 0.875rem;
        color: #6b7280;
    }
    .lesson-content {
        font-size: 1rem;
        line-height: 1.6;
        color: #374151;
        margin-bottom: 2rem;
    }
  
   

    .sidebar-card {
        background: white;
        border: 1px solid #e5e7eb;
        border-radius: 12px;
        padding: 1.5rem;
        margin-bottom: 1rem;
    }
    .sidebar-card h4 {
        font-size: 1.1rem;
        font-weight: 600;
        color: #1f2937;
        margin-bottom: 1rem;
    }
    .unit-item {
        display: flex;
        align-items: center;
        gap: 0.75rem;
        padding: 0.5rem 0;
        font-size: 0.9rem;
    }
    .unit-active {
        background: #10b981;
        color: white;
        padding: 0.25rem 0.75rem;
        border-radius: 20px;
        font-weight: 500;
    }
    .unit-inactive {
        color: #6b7280;
    }
    .nav-buttons {
        display: flex;
        justify-content: space-between;
        margin-top: 2rem;
        gap: 1rem;
    }
    .btn-secondary {
        background: #f3f4f6;
        color: #374151;
        border: 1px solid #d1d5db;
        padding: 0.75rem 1.5rem;
        border-radius: 8px;
        font-weight: 500;
        text-decoration: none;
    }
    .btn-primary {
        background: #10b981;
        color: white;
        border: none;
        padding: 0.75rem 1.5rem;
        border-radius: 8px;
        font-weight: 500;
    }
    </style>
    """, unsafe_allow_html=True)

    st.markdown(f'<div class="lesson-header">{mod["title"]}</div>', unsafe_allow_html=True)

    topics = _get_topics(level, module_id)
    topic_idx = st.session_state.get("topic_idx", 0)

    if not topics:
        st.error("No lesson content available for this module.")
        return

    col_main, col_sidebar = st.columns([2, 1])
    
    with col_main:
        t_title, t_text = topics[topic_idx]

        st.markdown(f"""
        <div class="lesson-card">
            <div class="unit-header">
                <span class="unit-info">Unit {topic_idx + 1} of {len(topics)}</span>
                <div class="read-time">
                    <span>πŸ•</span>
                    <span>5-8 min read</span>
                </div>
            </div>
            <div class="lesson-content">
                {t_text.strip() if t_text else "<p>Content coming soon.</p>"}
            </div>
        </div>
        """, unsafe_allow_html=True)


        # Navigation Buttons OUTSIDE lesson card
        st.markdown('<div class="nav-buttons">', unsafe_allow_html=True)
        col1, col2, col3 = st.columns([1, 1, 1])
        with col1:
            if st.button("← Previous", disabled=topic_idx == 0, key="prev_btn"):
                st.session_state.topic_idx -= 1
                st.rerun()

        with col3:
            is_last = topic_idx >= len(topics) - 1
            if is_last:
                if st.button("Complete Module β†’", key="complete_btn"):
                    st.session_state.show_quiz_prompt = True
                    _quiz_prompt_dialog()   # open now
                    st.stop()               # prevent the rest of the page from rendering under it

            else:
                if st.button("Next Unit β†’", key="next_btn"):
                    st.session_state.topic_idx += 1
                    st.rerun()
        st.markdown('</div>', unsafe_allow_html=True)

        # ---- Post-module modal: offer to start Quiz 1 ----
        if st.session_state.get("show_quiz_prompt"):
            _quiz_prompt_dialog()

                

    # -----------------------------
    # Sidebar with Progress & Units
    # -----------------------------
    with col_sidebar:
        # Progress Card
        st.markdown("""
        <div class="sidebar-card">
            <h4>Module Progress</h4>
        """, unsafe_allow_html=True)
        progress = (topic_idx + 1) / max(1, len(topics))
        st.progress(progress)
        st.markdown(f"<p style='color: #6b7280; font-size: 0.875rem; margin-top: 0.5rem;'>Unit {topic_idx + 1} of {len(topics)}</p>", unsafe_allow_html=True)
        st.markdown("</div>", unsafe_allow_html=True)

        # Units Card
        st.markdown("""
        <div class="sidebar-card">
            <h4>Module Units</h4>
        """, unsafe_allow_html=True)

        for i, (tt, _) in enumerate(topics):
            if i == topic_idx:
                st.markdown(f"""
                <div class="unit-item">
                    <span style="background: #10b981; color: white; width: 20px; height: 20px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 0.75rem;">●</span>
                    <span class="unit-active">{tt}</span>
                </div>
                """, unsafe_allow_html=True)
            elif i < topic_idx:
                st.markdown(f"""
                <div class="unit-item">
                    <span style="color: #10b981; font-size: 1.1rem;">βœ“</span>
                    <span style="color: #374151;">{tt}</span>
                </div>
                """, unsafe_allow_html=True)
            else:
                st.markdown(f"""
                <div class="unit-item">
                    <span style="background: #e5e7eb; color: #9ca3af; width: 20px; height: 20px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 0.75rem;">β—‹</span>
                    <span class="unit-inactive">{tt}</span>
                </div>
                """, unsafe_allow_html=True)

        st.markdown("</div>", unsafe_allow_html=True)

        # Back Button
        if st.button("← Back to Modules", key="back_modules"):
            st.session_state.mode = "catalog"
            st.session_state.module_id = None
            st.session_state.topic_idx = 0
            st.rerun()



def _render_results():
    result = st.session_state.quiz_result or {}
    score = result.get("score", {})
    correct = score.get("correct", 0)
    total = score.get("total", 0)

    st.success(f"Quiz Complete! You scored {correct} / {total}.")

    wrong = result.get("wrong", [])
    if wrong:
        with st.expander("Review your answers"):
            for w in wrong:
                st.markdown(f"**{w.get('question','')}**")
                st.write(f"Your answer: {w.get('your_answer','')}")
                st.write(f"Correct answer: {w.get('correct_answer','')}")
                st.divider()

    fb = st.session_state.chatbot_feedback
    if fb:
        st.markdown("#### Tutor Explanation")
        st.write(fb)

    level = st.session_state.level
    module_id = st.session_state.module_id
    planned = next((m.get("topics", []) for m in MODULES_META[level] if m["id"] == module_id), [])

    c1, c2, c3 = st.columns([1, 1, 1])
    with c1:
        if st.button("Back to Modules"):
            st.session_state.mode = "catalog"
            st.session_state.module_id = None
            st.rerun()
    with c2:
        if st.button("Ask the Chatbot β†’"):
            st.session_state.current_page = "Chatbot"
            st.session_state.chatbot_prefill = fb
            st.rerun()
    # with c3:
    #     if quiz_index is not None and quiz_index + 1 < len(planned):
    #         if st.button("Continue Lesson β†’"):
    #             st.session_state.mode = "lesson"
    #             st.session_state.topic_idx = quiz_index + 1
    #             st.rerun()

# ---------------------------------------------
# Public entry point(s)
# ---------------------------------------------
def _render_assigned_lesson(lesson_id: int, assignment_id: Optional[int] = None):
    data = _fetch_assigned_lesson(lesson_id)
    if not data or not data.get("lesson"):
        st.error("Lesson not found or not available.")
        if st.button("β¬… Back to Classes"):
            st.session_state.lesson_route = None
            st.session_state.current_page = "Teacher Link"
            st.rerun()
        return

    L = data["lesson"]
    sections = sorted(data.get("sections", []), key=lambda s: int(s.get("position", 0)))

    st.markdown(f"### {L.get('title', 'Untitled')}")
    if L.get("description"):
        st.caption(L["description"])

    key = f"_dbsec_idx_{lesson_id}"
    if key not in st.session_state:
        st.session_state[key] = 0

    idx = st.session_state[key]
    total = max(1, len(sections))
    st.progress((idx + 1) / total, text=f"Unit {idx + 1} of {total}")

    if not sections:
        st.info("No sections have been added to this lesson yet.")
        if st.button("β¬… Back to Classes"):
            st.session_state.lesson_route = None
            st.session_state.current_page = "Teacher Link"
            st.rerun()
        return

    sec = sections[idx]
    sec_title = sec.get("title") or f"Section {idx + 1}"
    content = (sec.get("content") or sec.get("body") or "").strip()

    st.subheader(sec_title)
    if content:
        st.markdown(content, unsafe_allow_html=True)
    else:
        st.info("Content coming soon.")

    col1, col2, col3 = st.columns([1, 1, 1])
    with col1:
        if st.button("← Previous", disabled=idx == 0):
            st.session_state[key] = max(0, idx - 1)
            st.rerun()
    with col2:
        if st.button("Back to Classes"):
            st.session_state.lesson_route = None
            st.session_state.current_page = "Teacher Link"
            st.rerun()
    with col3:
        is_last = idx >= len(sections) - 1
        if is_last:
            if st.button("Finish"):
                st.success("Lesson complete.")
                st.session_state.lesson_route = None
                st.session_state.current_page = "Teacher Link"
                st.rerun()
        else:
            if st.button("Next β†’"):
                st.session_state[key] = idx + 1
                st.rerun()

def render():
    load_css(os.path.join("assets", "styles.css"))
    _ensure_state()
    
    route = st.session_state.get("lesson_route")
    if route and route.get("source") == "teacher":
        _render_assigned_lesson(int(route.get("lesson_id", 0)), route.get("assignment_id"))
        return

    st.caption("Learning Path Β· " + st.session_state.level.capitalize())

    mode = st.session_state.mode
    if mode == "catalog":
        _render_catalog()
    elif mode == "lesson":
        _render_lesson()
    elif mode == "quiz":
        quiz_page.show_page()
    elif mode == "results":
        _render_results()
    else:
        st.session_state.mode = "catalog"
        _render_catalog()


# For pages that import and call `show_page()`
show_page = render

if __name__ == "__main__":
    st.set_page_config(page_title="Lesson", page_icon="πŸ“˜", layout="centered")
    render()