#!/usr/bin/env python3
"""
Week 27: Equal Groups & Arrays
Phase 5: Multiplication Introduction

Standards: 2.OA.A.4 — use addition to find total objects in rectangular arrays

New Standard:
- Reduced drill (4-6 problems per section)
- Estimation before calculating
- Multiple strategies (groups vs arrays)
- Better word problems (reasoning, real-world equal-group contexts)
- Open-ended challenges (Create Your Own)
- Number talks (explain WITHOUT calculating)
- Friday self-check with emoji reflection
"""

import os
import glob
from weasyprint import HTML

from math_helpers import (
    CSS, TEACHER_CSS,
    page, page_multi, hdr, sec, b, bw, bl,
    tg_page_multi, tg_sec, tg_note, tg_section_title,
    array_svg, eq_group_svg, grid_svg,
    star_svg, medal_svg, trophy_svg, check_svg,
    times_table_html, skip_count_html, mini_chart_html,
)
from activity_builders import (
    build_equal_groups,
    build_skip_counting,
    build_times_table,
    build_challenge_box,
    build_two_step_problems,
)

BASE = os.path.expanduser("~/Home_School/2nd_Grade/Math")
WEEK = os.path.join(BASE, "Week_27")


# ═══════════════════════════════════════════════════
# MONDAY — What Are Equal Groups?
# ═══════════════════════════════════════════════════
def monday():
    # 1: Fluency warmup — skip count by 2s (connects to equal groups)
    s1 = '<div class="abox" style="background:#e8f5e9;">'\
         '<p><b>2-min Fluency Warm-up</b> — Skip count by 2s. Fill in the missing numbers.</p>'
    s1 += build_skip_counting({
        "sequences": [
            {"start": 2, "step": 2, "count": 8, "blank_indices": [3, 6]},
            {"start": 2, "step": 2, "count": 8, "blank_indices": [2, 5, 7]},
        ],
        "note": "Skip counting helps us count equal groups fast!"
    })
    s1 += '</div>'

    # 2: Equal groups intro — guided visuals (reduced from 6 to 4)
    s2 = '<p class="note">A group is <b>equal</b> when every group has the SAME number of things. '\
         'Say it out loud: "3 groups of 4."</p>'
    s2 += build_equal_groups({
        "groups": [
            {"num_groups": 3, "items_per_group": 4, "blank_total": True},
            {"num_groups": 2, "items_per_group": 5, "blank_total": True},
            {"num_groups": 4, "items_per_group": 3, "blank_total": True},
            {"num_groups": 5, "items_per_group": 2, "blank_total": True},
        ],
        "note": "Count the equal groups. Write how many in ALL."
    })

    # 3: Write the "groups of" sentence (reduced from 6 to 4)
    s3 = '<p class="note">Look at each picture. Fill in the "groups of" sentence.</p>'\
         '<div class="mgrid c2">'
    for g, n in [(2, 4), (3, 3), (4, 2), (2, 6)]:
        pic = ''.join(eq_group_svg(n) for _ in range(g))
        s3 += f'<div class="abox">{pic}'\
              f'<p>{b("3em")} groups of {b("3em")}</p>'\
              f'<p>Total: {b("3em")}</p></div>'
    s3 += '</div>'

    # 4: Number Talk — explain WITHOUT calculating
    s4 = '<div class="abox" style="background:#e3f2fd;">'\
         '<p><b>Number Talk</b> — Explain WITHOUT counting every dot.</p>'\
         '<div class="mgrid c2">'
    s4 += '<div class="abox"><p>Which has MORE cookies?</p>'\
          '<p>A: 3 bags with 5 cookies each</p><p>B: 3 bags with 4 cookies each</p>'\
          '<p style="font-size:11px;">My thinking: ' + bw() + '</p></div>'
    s4 += '<div class="abox"><p>Are these the SAME or DIFFERENT totals?</p>'\
          '<p>A: 2 groups of 6</p><p>B: 6 groups of 2</p>'\
          '<p style="font-size:11px;">My thinking: ' + bw() + '</p></div>'
    s4 += '</div></div>'

    # 5: Challenge — draw your own equal groups
    s5 = '<div class="abox" style="background:#f3e5f5;">'\
         '<p><b>Create Your Own</b> — Draw a picture of <b>4 groups of 3</b>. '\
         'You can draw stars, apples, dots — anything you like!</p>'\
         '<p style="min-height:80px; border:1px dashed #999; border-radius:4px;"></p>'\
         '<p>How many in all? ' + b("4em") + '</p>'\
         '<p style="font-size:11px; color:#666;">How did you count them? ' + bw() + '</p>'\
         '</div>'

    return page(27, "Monday", "What Are Equal Groups?",
        f'''{sec(1, "Warm-up: Skip Count by 2s", s1)}
{sec(2, "Equal Groups", s2)}
{sec(3, "Say It: Groups Of", s3)}
{sec(4, "Number Talk: Compare Groups", s4)}
{sec(5, "Challenge: Create Your Own", s5)}''')


# ═══════════════════════════════════════════════════
# TUESDAY — Arrays: Rows and Columns
# ═══════════════════════════════════════════════════
def tuesday():
    # 1: Fluency warmup — skip count by 5s
    s1 = '<div class="abox" style="background:#e8f5e9;">'\
         '<p><b>2-min Fluency Warm-up</b> — Skip count by 5s.</p>'
    s1 += build_skip_counting({
        "sequences": [
            {"start": 5, "step": 5, "count": 8, "blank_indices": [2, 5]},
            {"start": 5, "step": 5, "count": 8, "blank_indices": [3, 6, 7]},
        ],
        "note": "Count by 5s — fast like counting nickels!"
    })
    s1 += '</div>'

    # 2: Array intro — rows and columns (guided, reduced to 4)
    s2 = '<p class="note">An <b>array</b> puts things in neat <b>rows</b> (across ➡) and '\
         '<b>columns</b> (down ⬇). We say "3 rows of 4."</p>'\
         '<div class="mgrid c2">'
    for r, c in [(3, 4), (2, 5), (4, 3), (2, 6)]:
        s2 += f'<div class="abox">{array_svg(r, c)}'\
              f'<p>{b("3em")} rows of {b("3em")}</p>'\
              f'<p>Total dots: {b("3em")}</p></div>'
    s2 += '</div>'

    # 3: Estimate First! — estimate before counting
    s3 = '<div class="abox" style="background:#fff3e0;">'\
         '<p><b>Estimate First!</b> Take a quick peek — do NOT count yet. '\
         'Write your estimate, THEN count by rows to check.</p>'\
         '<div class="mgrid c2">'
    for r, c in [(4, 5), (3, 6)]:
        s3 += f'<div class="abox">{array_svg(r, c)}'\
              f'<p>My estimate: {b("3em")}</p>'\
              f'<p>Actual count: {b("3em")}</p>'\
              f'<p style="font-size:11px; color:#666;">Was your estimate close?</p></div>'
    s3 += '</div></div>'

    # 4: Write an addition equation for each array (reduced to 4)
    s4 = '<p class="note">Add each ROW to find the total. Example: 3 rows of 4 → 4 + 4 + 4 = 12.</p>'\
         '<div class="mgrid c2">'
    for r, c in [(3, 5), (2, 4), (4, 2), (3, 3)]:
        s4 += f'<div class="abox">{array_svg(r, c)}'\
              f'<p>Addition equation: ' + bw("12em") + '</p>'\
              f'<p>Total: {b("3em")}</p></div>'
    s4 += '</div>'

    # 5: Two strategies — count by rows OR by columns
    s5 = '<div class="abox" style="background:#f3e5f5;">'\
         '<p><b>Show Two Different Ways</b> — Find the total for this array two ways: '\
         '(1) add the rows, (2) add the columns.</p>'\
         f'{array_svg(3, 4)}'\
         '<p>Way 1 (rows): 4 + 4 + 4 = ' + b("3em") + '</p>'\
         '<p>Way 2 (columns): 3 + 3 + 3 + 3 = ' + b("3em") + '</p>'\
         '<p style="font-size:11px; color:#666;">What do you notice about the two answers? ' + bw() + '</p>'\
         '</div>'

    return page(27, "Tuesday", "Arrays: Rows and Columns",
        f'''{sec(1, "Warm-up: Skip Count by 5s", s1)}
{sec(2, "Arrays: Rows and Columns", s2)}
{sec(3, "Estimate First!", s3)}
{sec(4, "Addition Equations for Arrays", s4)}
{sec(5, "Show Two Different Ways", s5)}''')


# ═══════════════════════════════════════════════════
# WEDNESDAY — Mixed: Groups & Arrays
# ═══════════════════════════════════════════════════
def wednesday():
    # 1: Fluency warmup — skip count by 10s
    s1 = '<div class="abox" style="background:#e8f5e9;">'\
         '<p><b>2-min Fluency Warm-up</b> — Skip count by 10s.</p>'
    s1 += build_skip_counting({
        "sequences": [
            {"start": 10, "step": 10, "count": 8, "blank_indices": [3, 5]},
            {"start": 10, "step": 10, "count": 8, "blank_indices": [2, 6, 7]},
        ],
        "note": "Count by 10s — like counting dimes!"
    })
    s1 += '</div>'

    # 2: Mixed practice — groups AND arrays (reduced from 6 to 4)
    s2 = '<p class="note">Some pictures show groups, some show arrays. Find each total.</p>'\
         '<div class="mgrid c2">'
    s2 += f'<div class="abox">{eq_group_svg(4)}{eq_group_svg(4)}{eq_group_svg(4)}'\
          f'<p>3 groups of 4 = {b("3em")}</p></div>'
    s2 += f'<div class="abox">{array_svg(2, 6)}'\
          f'<p>2 rows of 6 = {b("3em")}</p></div>'
    s2 += f'<div class="abox">{eq_group_svg(5)}{eq_group_svg(5)}'\
          f'<p>2 groups of 5 = {b("3em")}</p></div>'
    s2 += f'<div class="abox">{array_svg(4, 4)}'\
          f'<p>4 rows of 4 = {b("3em")}</p></div>'
    s2 += '</div>'

    # 3: Word problems — equal groups in real life (reasoning)
    s3 = '<div class="wp">'\
         '<p class="note">Draw the groups or the array. Then find the answer.</p>'\
         '<div class="mgrid c2">'
    problems = [
        ("A muffin tin has 3 rows. Each row holds 4 muffins. Mom filled every spot. How many muffins did she bake?", "muffins"),
        ("Diego puts his toy cars in 4 equal rows. Each row has 5 cars. How many toy cars does Diego have?", "cars"),
    ]
    for text, unit in problems:
        s3 += f'<div class="abox"><p style="font-size:0.95em;">{text}</p>'\
              '<p style="font-size:11px;">Draw your groups or array: ' + bw() + '</p>'\
              '<p>' + b("5em") + f' {unit}</p></div>'
    s3 += '</div></div>'

    # 4: Find the error — unequal groups
    s4 = '<div class="abox">'\
         '<p class="note">Mia says each picture shows equal groups. Is she right? Circle ✓ or ✗ and explain.</p>'\
         '<div class="mgrid c2">'
    s4 += f'<div class="abox">{eq_group_svg(4)}{eq_group_svg(4)}{eq_group_svg(3)}'\
          '<p>Equal groups? ✓ or ✗</p>'\
          '<p style="font-size:11px;">Explain: ' + bw() + '</p></div>'
    s4 += f'<div class="abox">{eq_group_svg(3)}{eq_group_svg(3)}{eq_group_svg(3)}{eq_group_svg(3)}'\
          '<p>Equal groups? ✓ or ✗</p>'\
          '<p style="font-size:11px;">Explain: ' + bw() + '</p></div>'
    s4 += '</div></div>'

    # 5: Create your own array
    s5 = '<div class="abox" style="background:#f3e5f5;">'\
         '<p><b>Create Your Own</b> — Draw an array that has exactly <b>12 dots</b>. '\
         'Hint: there is more than one way!</p>'\
         '<p style="min-height:80px; border:1px dashed #999; border-radius:4px;"></p>'\
         '<p>My array has ' + b("3em") + ' rows of ' + b("3em") + '</p>'\
         '<p style="font-size:11px; color:#666;">Can you think of a DIFFERENT array with 12 dots? '\
         'Write it: ' + bw() + '</p>'\
         '</div>'

    return page(27, "Wednesday", "Mixed Practice: Groups & Arrays",
        f'''{sec(1, "Warm-up: Skip Count by 10s", s1)}
{sec(2, "Groups and Arrays", s2)}
{sec(3, "Word Problems", s3)}
{sec(4, "Find the Error: Equal or Not?", s4)}
{sec(5, "Challenge: Create Your Own Array", s5)}''')


# ═══════════════════════════════════════════════════
# THURSDAY — Word Problems with Equal Groups
# ═══════════════════════════════════════════════════
def thursday():
    # 1: Fluency warmup — mixed skip counting
    s1 = '<div class="abox" style="background:#e8f5e9;">'\
         '<p><b>2-min Fluency Warm-up</b> — What comes next? Fill in the blank.</p>'\
         '<div class="mgrid c3">'
    for seq in ["2, 4, 6, ___", "5, 10, 15, ___", "10, 20, 30, ___", "4, 6, 8, ___"]:
        s1 += f'<div class="abox"><p>{seq.replace("___", b("3em"))}</p></div>'
    s1 += '</div></div>'

    # 2: Better word problems — real-world equal groups (reduced to 3)
    s2 = '<div class="wp">'\
         '<p class="note">Read carefully. Think: how many groups? How many in each group? Draw if it helps!</p>'\
         '<div class="mgrid c2">'
    problems = [
        ("A classroom has 5 tables. Each table seats 4 students. Every seat is taken. How many students are in the classroom?", "students"),
        ("Grandma plants 3 rows of tomato plants in her garden. Each row has 6 plants. How many tomato plants did she plant?", "plants"),
        ("An egg carton has 2 rows with 6 eggs in each row. How many eggs fit in the carton?", "eggs"),
    ]
    for text, unit in problems:
        s2 += f'<div class="abox"><p style="font-size:0.95em;">{text}</p>'\
              '<p style="font-size:11px;">Draw or write your thinking: ' + bw() + '</p>'\
              '<p>' + b("5em") + f' {unit}</p></div>'
    s2 += '</div></div>'

    # 3: Two-step problem
    s3 = '<div class="abox">'\
         '<p class="note">Solve step by step. Write each answer as you go.</p>'\
         + build_two_step_problems({
             "problems": [
                 {
                     "text": "A bakery tray holds 4 rows of 5 cookies. The baker fills one whole tray, then sells 8 cookies. How many cookies are left on the tray?",
                     "blanks": 2
                 }
             ],
             "note": "Step 1: find the total on the tray. Step 2: subtract what was sold."
         }) + '</div>'

    # 4: Number Talk — same total, different look
    s4 = '<div class="abox" style="background:#e3f2fd;">'\
         '<p><b>Number Talk</b> — Explain WITHOUT counting every dot.</p>'\
         '<div class="mgrid c2">'
    s4 += f'<div class="abox"><p>A: {array_svg(2, 6, width="100")}</p><p>B: {array_svg(6, 2, width="40")}</p>'\
          '<p>Do A and B have the same number of dots?</p>'\
          '<p style="font-size:11px;">My thinking: ' + bw() + '</p></div>'
    s4 += '<div class="abox"><p>Rosa has 4 boxes of 3 crayons. Sam has 3 boxes of 3 crayons.</p>'\
          '<p>Who has more? How do you KNOW without counting?</p>'\
          '<p style="font-size:11px;">My thinking: ' + bw() + '</p></div>'
    s4 += '</div></div>'

    # 5: Create your own word problem
    s5 = '<div class="abox" style="background:#f3e5f5;">'\
         '<p><b>Create Your Own Word Problem</b></p>'\
         '<p>Write an equal-groups word problem about something you like '\
         '(stickers, dinosaurs, snacks...). Use the words "rows" or "groups." Then solve it.</p>'\
         '<p style="min-height:60px; border-bottom:1px solid #333;"></p>'\
         '<p style="min-height:30px; border-bottom:1px solid #333;"></p>'\
         '<p style="font-size:11px;">My picture or thinking: ' + bw() + '</p>'\
         '<p>Answer: ' + bl() + '</p>'\
         '</div>'

    return page(27, "Thursday", "Word Problems with Equal Groups",
        f'''{sec(1, "Warm-up: What Comes Next?", s1)}
{sec(2, "Word Problems: Groups in Real Life", s2)}
{sec(3, "Two-Step Problem", s3)}
{sec(4, "Number Talk: Same or Different?", s4)}
{sec(5, "Challenge: Create Your Own", s5)}''')


# ═══════════════════════════════════════════════════
# FRIDAY — Review & Self-Check
# ═══════════════════════════════════════════════════
def friday():
    # 1: Fluency warmup — skip counting mix
    s1 = '<div class="abox" style="background:#e8f5e9;">'\
         '<p><b>2-min Fluency Warm-up</b> — Fill in the missing numbers. Go fast!</p>'
    s1 += build_skip_counting({
        "sequences": [
            {"start": 2, "step": 2, "count": 8, "blank_indices": [4, 7]},
            {"start": 5, "step": 5, "count": 8, "blank_indices": [3, 6]},
        ],
        "note": "Count by 2s, then by 5s."
    })
    s1 += '</div>'

    # 2: Estimate First! — arrays
    s2 = '<div class="abox" style="background:#fff3e0;">'\
         '<p><b>Estimate First!</b> Peek quickly, estimate, then count by rows to check.</p>'\
         '<div class="mgrid c2">'
    for r, c in [(5, 4), (3, 7)]:
        s2 += f'<div class="abox">{array_svg(r, c)}'\
              f'<p>My estimate: {b("3em")}</p>'\
              f'<p>Actual: {b("3em")}</p></div>'
    s2 += '</div></div>'

    # 3: Review — groups and arrays (reduced from 6 to 4)
    s3 = '<p class="note">Fill in the sentence AND the addition equation for each.</p>'\
         '<div class="mgrid c2">'
    s3 += f'<div class="abox">{eq_group_svg(5)}{eq_group_svg(5)}{eq_group_svg(5)}'\
          f'<p>{b("3em")} groups of {b("3em")} = {b("5em")}</p>'\
          '<p style="font-size:11px;">Addition: ' + bw("10em") + '</p></div>'
    s3 += f'<div class="abox">{array_svg(4, 3)}'\
          f'<p>{b("3em")} rows of {b("3em")} = {b("5em")}</p>'\
          '<p style="font-size:11px;">Addition: ' + bw("10em") + '</p></div>'
    s3 += f'<div class="abox">{eq_group_svg(2)}{eq_group_svg(2)}{eq_group_svg(2)}{eq_group_svg(2)}'\
          f'<p>{b("3em")} groups of {b("3em")} = {b("5em")}</p>'\
          '<p style="font-size:11px;">Addition: ' + bw("10em") + '</p></div>'
    s3 += f'<div class="abox">{array_svg(3, 6)}'\
          f'<p>{b("3em")} rows of {b("3em")} = {b("5em")}</p>'\
          '<p style="font-size:11px;">Addition: ' + bw("10em") + '</p></div>'
    s3 += '</div>'

    # 4: Word problem review (reduced to 2)
    s4 = '<div class="wp">'\
         '<p class="note">Show your thinking with a drawing or equation.</p>'\
         '<div class="mgrid c2">'
    problems = [
        ("A parking lot has 4 rows of cars with 5 cars in each row. How many cars are parked?", "cars"),
        ("Nina fills 3 baskets with 6 apples each. How many apples in all?", "apples"),
    ]
    for text, unit in problems:
        s4 += f'<div class="abox"><p style="font-size:0.95em;">{text}</p>'\
              '<p style="font-size:11px;">Draw or write your thinking: ' + bw() + '</p>'\
              '<p>' + b("5em") + f' {unit}</p></div>'
    s4 += '</div></div>'

    # 5: Self-check with emoji reflection
    s5 = '<div class="selfcheck">'\
         '<p><b>Self-Check: How do you feel about equal groups and arrays?</b></p>'\
         '<div style="display:flex; gap:20px; flex-wrap:wrap;">'\
         '<div style="background:#fff3e0; padding:10px; border-radius:5px; flex:1; min-width:150px;">'\
         '<p><b>Making Equal Groups</b></p>'\
         '<p>😊 I got it!  |  😐 Getting there  |  😟 Need help</p>'\
         '</div>'\
         '<div style="background:#e3f2fd; padding:10px; border-radius:5px; flex:1; min-width:150px;">'\
         '<p><b>Arrays (Rows &amp; Columns)</b></p>'\
         '<p>😊 I got it!  |  😐 Getting there  |  😟 Need help</p>'\
         '</div>'\
         '<div style="background:#f3e5f5; padding:10px; border-radius:5px; flex:1; min-width:150px;">'\
         '<p><b>Addition Equations for Arrays</b></p>'\
         '<p>😊 I got it!  |  😐 Getting there  |  😟 Need help</p>'\
         '</div>'\
         '</div>'\
         '<p style="margin-top:10px;"><b>One thing I learned this week:</b> ' + bl() + '</p>'\
         '<p><b>One thing I want to practice more:</b> ' + bl() + '</p>'\
         '<p><b>My favorite way to count a group (rows, columns, or skip counting):</b> ' + bl() + '</p>'\
         '</div>'

    return page(27, "Friday", "Week 27 Review & Self-Check",
        f'''{sec(1, "Warm-up: Skip Counting", s1)}
{sec(2, "Estimate First!", s2)}
{sec(3, "Review: Groups and Arrays", s3)}
{sec(4, "Word Problems", s4)}
{sec(5, "Self-Check & Reflection", s5)}''')


# ═══════════════════════════════════════════════════
# TEACHER GUIDE — Combined for all days
# ═══════════════════════════════════════════════════
def teacher_guide():
    return tg_page_multi(
        "Week 27", "Equal Groups & Arrays — Teacher Guide",

        # Monday
        tg_section_title("Monday: What Are Equal Groups?") +
        tg_sec(1, "Warm-up 2s", ["Seq 1 blanks: 8, 14 | Seq 2 blanks: 6, 12, 16"]) +
        tg_sec(2, "Equal Groups", ["3×4=12, 2×5=10, 4×3=12, 5×2=10"]) +
        tg_sec(3, "Groups Of", ["2 groups of 4 = 8; 3 groups of 3 = 9; 4 groups of 2 = 8; 2 groups of 6 = 12"]) +
        tg_sec(4, "Number Talk", ["A has more (same bags, more per bag)", "Same total — 2 groups of 6 and 6 groups of 2 both make 12"]) +
        tg_sec(5, "Create Own", ["Any drawing of 4 groups of 3; total 12"]) +
        tg_note("Page break"),

        # Tuesday
        tg_section_title("Tuesday: Arrays") +
        tg_sec(1, "Warm-up 5s", ["Seq 1 blanks: 15, 30 | Seq 2 blanks: 20, 35, 40"]) +
        tg_sec(2, "Arrays", ["3 rows of 4 = 12; 2 rows of 5 = 10; 4 rows of 3 = 12; 2 rows of 6 = 12"]) +
        tg_sec(3, "Estimate First", ["4×5=20, 3×6=18 — accept reasonable estimates"]) +
        tg_sec(4, "Addition Equations", ["5+5+5=15; 4+4=8; 2+2+2+2=8; 3+3+3=9"]) +
        tg_sec(5, "Two Ways", ["Rows: 4+4+4=12; Columns: 3+3+3+3=12 — same answer!"]) +
        tg_note("Page break"),

        # Wednesday
        tg_section_title("Wednesday: Mixed Groups & Arrays") +
        tg_sec(1, "Warm-up 10s", ["Seq 1 blanks: 40, 60 | Seq 2 blanks: 30, 70, 80"]) +
        tg_sec(2, "Mixed", ["12, 12, 10, 16"]) +
        tg_sec(3, "Word Problems", ["12 muffins (3×4), 20 cars (4×5)"]) +
        tg_sec(4, "Find Error", ["✗ — groups are 4, 4, 3 (not equal)", "✓ — all groups have 3"]) +
        tg_sec(5, "Create Own", ["Any 12-dot array: 1×12, 2×6, 3×4, 4×3, 6×2, 12×1"]) +
        tg_note("Page break"),

        # Thursday
        tg_section_title("Thursday: Word Problems with Equal Groups") +
        tg_sec(1, "Warm-up", ["8, 20, 40, 10"]) +
        tg_sec(2, "Word Problems", ["20 students (5×4), 18 plants (3×6), 12 eggs (2×6)"]) +
        tg_sec(3, "Two-Step", ["Step 1: 4×5=20 cookies. Step 2: 20−8=12 cookies left"]) +
        tg_sec(4, "Number Talk", ["Same — 2 rows of 6 and 6 rows of 2 both have 12 (turned array)", "Rosa has more — one extra box of 3"]) +
        tg_sec(5, "Create Own", ["Accept student-created equal-group problems"]) +
        tg_note("Page break"),

        # Friday
        tg_section_title("Friday: Review & Self-Check") +
        tg_sec(1, "Warm-up", ["2s blanks: 10, 16 | 5s blanks: 20, 35"]) +
        tg_sec(2, "Estimate First", ["5×4=20, 3×7=21 — accept close estimates"]) +
        tg_sec(3, "Review", ["3 groups of 5 = 15 (5+5+5); 4 rows of 3 = 12 (3+3+3+3); 4 groups of 2 = 8 (2+2+2+2); 3 rows of 6 = 18 (6+6+6)"]) +
        tg_sec(4, "Word Problems", ["20 cars, 18 apples"]) +
        tg_sec(5, "Self-Check", ["Accept student self-assessment and reflection"])
    )


# ═══════════════════════════════════════════════════
# GENERATE
# ═══════════════════════════════════════════════════
if __name__ == "__main__":
    os.makedirs(WEEK, exist_ok=True)
    for f in glob.glob(os.path.join(WEEK, "*.pdf")):
        os.remove(f)
    for f in glob.glob(os.path.join(WEEK, "*.html")):
        os.remove(f)

    days = [
        (monday, "Monday", "What Are Equal Groups?"),
        (tuesday, "Tuesday", "Arrays: Rows and Columns"),
        (wednesday, "Wednesday", "Mixed Practice: Groups & Arrays"),
        (thursday, "Thursday", "Word Problems with Equal Groups"),
        (friday, "Friday", "Week 27 Review & Self-Check"),
    ]

    for lesson_func, day, title in days:
        html = lesson_func()
        fname = f"Week_27_{day}"
        path = os.path.join(WEEK, f"{fname}.html")
        with open(path, "w") as f:
            f.write(html)
        pdf_path = os.path.join(WEEK, f"{fname}.pdf")
        HTML(filename=path).write_pdf(pdf_path)
        print(f"Generated: {pdf_path}")

    html = teacher_guide()
    path = os.path.join(WEEK, "Week_27_Teacher_Guide.html")
    with open(path, "w") as f:
        f.write(html)
    pdf_path = os.path.join(WEEK, "Week_27_Teacher_Guide.pdf")
    HTML(filename=path).write_pdf(pdf_path)
    print(f"Generated: {pdf_path}")

    print("\nWeek 27 (Equal Groups & Arrays) complete!")
