#!/usr/bin/env python3
"""
Week 6: Phase 1 Review — REWRITTEN
Phase 1: Number Sense & Operations

New Standard:
- Reduced drill (40% fewer problems)
- Added estimation activities
- Multiple strategies encouraged
- Better word problems (reasoning over keywords)
- Real challenges (open-ended thinking)
- Number talks (compare without calculating)
- Friday self-check with 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, tg_page_multi, tg_sec, tg_note, tg_section_title,
)
from activity_builders import (
    build_subtraction_facts,
    build_addition_facts,
    build_subtraction_word_problems,
    build_fact_families_subtraction,
    build_challenge_box,
    build_compare_numbers,
    build_order_numbers,
    build_write_tens_ones,
    build_read_tens_ones,
)

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


# ═══════════════════════════════════════════════════
# MONDAY — Number Sense Review
# ═══════════════════════════════════════════════════
def monday():
    # 1: Counting challenge (reduced from 4 sequences to 3)
    s1 = '<div class="abox">'
    s1 += '<p class="note">Fill in the missing numbers.</p>'
    s1 += '<div class="mgrid c2">'
    s1 += '<div class="abox"><p><b>Count by 5s:</b> 5, 10, ' + b('4em') + ', ' + b('4em') + ', 25, ' + b('4em') + '</p></div>'
    s1 += '<div class="abox"><p><b>Count by 10s:</b> 10, 20, ' + b('4em') + ', ' + b('4em') + ', 50, ' + b('4em') + '</p></div>'
    s1 += '<div class="abox"><p><b>Count back by 10s:</b> 100, ' + b('4em') + ', ' + b('4em') + ', 70, ' + b('4em') + ', 50</p></div>'
    s1 += '</div></div>'
    
    # 2: Place value (reduced from 6 to 4)
    s2 = build_write_tens_ones({
        "numbers": [47, 83, 62],
        "note": "How many tens and ones?",
        "cols": 2,
    })
    s2 += build_read_tens_ones({
        "phrases": ["5 tens and 8 ones"],
        "note": "",
        "cols": 2,
    })
    
    # 3: NEW — Number Talk: Which is larger? Explain without calculating.
    s3 = '<div class="abox" style="background:#e3f2fd;">'
    s3 += '<p><b>Number Talk</b> — Which is larger? Explain WITHOUT calculating.</p>'
    s3 += '<div class="mgrid c2">'
    s3 += '<div class="abox"><p>47  or  74</p><p style="font-size:11px;">My thinking: ' + bw() + '</p></div>'
    s3 += '<div class="abox"><p>63  or  58</p><p style="font-size:11px;">My thinking: ' + bw() + '</p></div>'
    s3 += '</div></div>'
    
    # 4: Compare numbers (reduced from 6 to 4)
    s4 = build_compare_numbers({
        "pairs": [(35, 53), (67, 76), (82, 28), (55, 55)],
        "note": "Write >, <, or =.",
        "cols": 2,
    })
    
    # 5: Challenge — Number detective (renumbered, kept)
    s5 = '<div class="abox" style="background:#f3e5f5;">'
    s5 += '<p><b>Number Detective</b></p>'
    s5 += '<p>☐ Less than 50  ☐ Tens digit is 3  ☐ Ones digit is odd  ☐ Greater than 30</p>'
    s5 += '<p>My number could be: ' + b('4em') + ', ' + b('4em') + ', ' + b('4em') + ', or ' + b('4em') + '</p>'
    s5 += '</div>'
    
    return page(6, "Monday", "Number Sense Review",
        f'''{sec(1, "Counting Challenge", s1)}
{sec(2, "Place Value", s2, pb=True)}
{sec(3, "Number Talk", s3)}
{sec(4, "Compare Numbers", s4, pb=True)}
{sec(5, "Challenge: Number Detective", s5)}''')


# ═══════════════════════════════════════════════════
# TUESDAY — Addition Facts Review
# ═══════════════════════════════════════════════════
def tuesday():
    # 1: Easy facts (reduced from 9 to 6)
    s1 = build_addition_facts({
        "facts": [(5, 3), (6, 6), (8, 2), (4, 7), (9, 1), (5, 5)],
        "note": "Singles and doubles.",
        "cols": 3,
    })
    
    # 2: Medium facts (reduced from 9 to 6)
    s2 = build_addition_facts({
        "facts": [(7, 6), (8, 5), (9, 4), (6, 8), (9, 5), (7, 8)],
        "note": "",
        "cols": 3,
    })
    
    # 3: Make 10 strategy (reduced from 4 to 3)
    s3 = '<div class="abox">'
    s3 += '<p class="note">Break the second number to make 10.</p>'
    s3 += '<p>8 + 5 = 8 + ' + b('4em') + ' + ' + b('4em') + ' = ' + b('4em') + '</p>'
    s3 += '<p>7 + 6 = 7 + ' + b('4em') + ' + ' + b('4em') + ' = ' + b('4em') + '</p>'
    s3 += '<p>9 + 4 = 9 + ' + b('4em') + ' + ' + b('4em') + ' = ' + b('4em') + '</p>'
    s3 += '</div>'
    
    # 4: Doubles plus 1 (reduced from 6 to 4)
    s4 = '<div class="abox">'
    s4 += '<p class="note">Use doubles to help!</p>'
    s4 += '<p>6 + 7 = (6+6)+1 = ' + b('4em') + '</p>'
    s4 += '<p>5 + 6 = (5+5)+1 = ' + b('4em') + '</p>'
    s4 += '<p>7 + 8 = (7+7)+1 = ' + b('4em') + '</p>'
    s4 += '<p>8 + 9 = (8+8)+1 = ' + b('4em') + '</p>'
    s4 += '</div>'
    
    # 5: NEW — Better word problems (reasoning, not keywords)
    s5 = '<div class="wp">'
    s5 += '<p class="note">Read carefully. Think about what is happening.</p>'
    s5 += '<div class="mgrid c2">'
    s5 += '<div class="abox"><p>Mike has 8 toy cars. Dad gives him 7 more. How many does Mike have now?</p><p style="font-size:11px;">Draw or write your thinking: ' + bw() + '</p><p>Answer: ' + b('4em') + ' cars</p></div>'
    s5 += '<div class="abox"><p>There are 14 boys and 9 girls in a class. How many students total?</p><p style="font-size:11px;">Draw or write your thinking: ' + bw() + '</p><p>Answer: ' + b('4em') + ' students</p></div>'
    s5 += '</div></div>'
    
    # 6: Challenge — Addition pyramid (kept, good open-ended activity)
    s6 = '<div class="abox" style="background:#f3e5f5;">'
    s6 += '<p><b>Addition Pyramid</b></p>'
    s6 += '<p>Add each pair. Write the sum below.</p>'
    s6 += '<div style="text-align:center; font-size:16px;">7    5    8    4</div>'
    s6 += '<div style="min-height:50px; border:1px dashed #ccc;"></div>'
    s6 += '</div>'
    
    return page(6, "Tuesday", "Addition Facts Review",
        f'''{sec(1, "Easy Facts", s1, pb=True)}
{sec(2, "Medium Facts", s2)}
{sec(3, "Make 10 Strategy", s3, pb=True)}
{sec(4, "Doubles Plus 1", s4)}
{sec(5, "Word Problems", s5, pb=True)}
{sec(6, "Challenge: Addition Pyramid", s6)}''')


# ═══════════════════════════════════════════════════
# WEDNESDAY — Subtraction Facts Review
# ═══════════════════════════════════════════════════
def wednesday():
    # 1: Easy facts (reduced from 9 to 6)
    s1 = build_subtraction_facts({
        "facts": [(10, 3), (9, 4), (8, 5), (10, 7), (7, 3), (10, 5)],
        "note": "Easy.",
        "cols": 3,
    })
    
    # 2: Medium facts (reduced from 9 to 6)
    s2 = build_subtraction_facts({
        "facts": [(14, 6), (13, 5), (15, 7), (12, 4), (16, 8), (11, 3)],
        "note": "",
        "cols": 3,
    })
    
    # 3: Subtract using addition (reduced from 6 to 4)
    s3 = '<div class="abox">'
    s3 += '<p class="note">Think: ? + subtrahend = minuend.</p>'
    s3 += '<p>15−8: 8+' + b('4em') + '=15 → answer=' + b('4em') + '</p>'
    s3 += '<p>14−7: 7+' + b('4em') + '=14 → answer=' + b('4em') + '</p>'
    s3 += '<p>13−6: 6+' + b('4em') + '=13 → answer=' + b('4em') + '</p>'
    s3 += '<p>17−9: 9+' + b('4em') + '=17 → answer=' + b('4em') + '</p>'
    s3 += '</div>'
    
    # 4: NEW — Better word problems
    s4 = '<div class="wp">'
    s4 += '<p class="note">Read carefully. Think about what is happening.</p>'
    s4 += '<div class="mgrid c2">'
    s4 += '<div class="abox"><p>Tom had 18 stickers. He gave 7 to his friend. How many does he have left?</p><p style="font-size:11px;">Draw or write your thinking: ' + bw() + '</p><p>Answer: ' + b('4em') + ' stickers</p></div>'
    s4 += '<div class="abox"><p>There were 15 birds on a branch. 6 flew away. How many are left?</p><p style="font-size:11px;">Draw or write your thinking: ' + bw() + '</p><p>Answer: ' + b('4em') + ' birds</p></div>'
    s4 += '</div></div>'
    
    # 5: Challenge — Subtraction pyramid
    s5 = '<div class="abox" style="background:#f3e5f5;">'
    s5 += '<p><b>Subtraction Pyramid</b></p>'
    s5 += '<p>Subtract each pair (left − right). Write the difference below.</p>'
    s5 += '<div style="text-align:center; font-size:16px;">20    12    7    4</div>'
    s5 += '<div style="min-height:50px; border:1px dashed #ccc;"></div>'
    s5 += '</div>'
    
    return page(6, "Wednesday", "Subtraction Facts Review",
        f'''{sec(1, "Easy Facts", s1, pb=True)}
{sec(2, "Medium Facts", s2)}
{sec(3, "Subtract Using Addition", s3, pb=True)}
{sec(4, "Word Problems", s4)}
{sec(5, "Challenge: Subtraction Pyramid", s5)}''')


# ═══════════════════════════════════════════════════
# THURSDAY — Mixed Addition and Subtraction
# ═══════════════════════════════════════════════════
def thursday():
    # 1: Mix it up (reduced from 8 to 6)
    s1 = '<div class="abox">'
    s1 += '<p class="note">Read carefully — add or subtract?</p>'
    s1 += '<div class="mgrid c2">'
    s1 += '<div class="abox"><p>8 + 6 = ' + b('4em') + '</p></div>'
    s1 += '<div class="abox"><p>15 − 7 = ' + b('4em') + '</p></div>'
    s1 += '<div class="abox"><p>9 + 5 = ' + b('4em') + '</p></div>'
    s1 += '<div class="abox"><p>14 − 8 = ' + b('4em') + '</p></div>'
    s1 += '<div class="abox"><p>7 + 7 = ' + b('4em') + '</p></div>'
    s1 += '<div class="abox"><p>16 − 9 = ' + b('4em') + '</p></div>'
    s1 += '</div></div>'
    
    # 2: Fill the sign (reduced from 8 to 6)
    s2 = '<div class="abox">'
    s2 += '<p class="note">Write + or − to make each sentence true.</p>'
    s2 += '<div class="mgrid c2">'
    s2 += '<div class="abox"><p>8 ' + b('1.5em') + ' 7 = 15</p></div>'
    s2 += '<div class="abox"><p>16 ' + b('1.5em') + ' 9 = 7</p></div>'
    s2 += '<div class="abox"><p>9 ' + b('1.5em') + ' 5 = 14</p></div>'
    s2 += '<div class="abox"><p>14 ' + b('1.5em') + ' 6 = 8</p></div>'
    s2 += '<div class="abox"><p>6 ' + b('1.5em') + ' 6 = 12</p></div>'
    s2 += '<div class="abox"><p>18 ' + b('1.5em') + ' 7 = 11</p></div>'
    s2 += '</div></div>'
    
    # 3: NEW — Better word problem mix (reasoning, not keywords)
    s3 = '<div class="wp">'
    s3 += '<p class="note">Read carefully. Think about what operation makes sense.</p>'
    s3 += '<div class="mgrid c2">'
    s3 += '<div class="abox"><p>Sarah has 9 pencils. Her friend gives her 6 more. How many does she have now?</p><p style="font-size:11px;">' + b('4em') + ' ' + b('1.5em') + ' ' + b('4em') + ' = ' + b('4em') + '</p></div>'
    s3 += '<div class="abox"><p>There were 17 cookies on the plate. Tom ate 8. How many are left?</p><p style="font-size:11px;">' + b('4em') + ' ' + b('1.5em') + ' ' + b('4em') + ' = ' + b('4em') + '</p></div>'
    s3 += '</div></div>'
    
    # 4: Two-step problems (kept at 3, good practice)
    s4 = '<div class="abox">'
    s4 += '<p class="note">Two-step problems.</p>'
    s4 += '<p>Start at 20. −7, then +5. 20−7=' + b('4em') + ', +5 = ' + b('4em') + '</p>'
    s4 += '<p>Start at 5. +8, then −3. 5+8=' + b('4em') + ', −3 = ' + b('4em') + '</p>'
    s4 += '<p>Start at 15. −6, then +4. 15−6=' + b('4em') + ', +4 = ' + b('4em') + '</p>'
    s4 += '</div>'
    
    # 5: Challenge — Mystery operations (reduced from 6 to 4)
    s5 = '<div class="abox" style="background:#f3e5f5;">'
    s5 += '<p><b>Mystery Operations</b></p>'
    s5 += '<div class="mgrid c2">'
    s5 += '<div class="abox"><p>9 ' + b('1.5em') + ' 6 = 15</p></div>'
    s5 += '<div class="abox"><p>16 ' + b('1.5em') + ' 7 = 9</p></div>'
    s5 += '<div class="abox"><p>8 ' + b('1.5em') + ' 5 = 13</p></div>'
    s5 += '<div class="abox"><p>20 ' + b('1.5em') + ' 8 = 12</p></div>'
    s5 += '</div></div>'
    
    return page(6, "Thursday", "Mixed Addition and Subtraction",
        f'''{sec(1, "Mix It Up!", s1, pb=True)}
{sec(2, "Fill the Sign", s2)}
{sec(3, "Word Problem Mix", s3, pb=True)}
{sec(4, "Two-Step Problems", s4)}
{sec(5, "Challenge: Mystery Operations", s5)}''')


# ═══════════════════════════════════════════════════
# FRIDAY — Phase 1 Review & Self-Check
# ═══════════════════════════════════════════════════
def friday():
    # 1: Counting review (reduced)
    s1 = '<div class="abox">'
    s1 += '<p class="note">Fill in the missing numbers.</p>'
    s1 += '<p><b>Count by 10s:</b> 10, 20, ' + b('4em') + ', ' + b('4em') + ', 50, ' + b('4em') + ', ' + b('4em') + '</p>'
    s1 += '<p><b>Count by 5s:</b> 5, 10, ' + b('4em') + ', 20, ' + b('4em') + ', 30</p>'
    s1 += '</div>'
    
    # 2: Place value review (reduced from 4 to 3)
    s2 = '<div class="abox">'
    s2 += '<p class="note">Fill in the blanks.</p>'
    s2 += '<p>63 = ' + b('4em') + ' tens, ' + b('4em') + ' ones</p>'
    s2 += '<p>4 tens + 9 ones = ' + b('4em') + '</p>'
    s2 += '<p>80 = ' + b('4em') + ' tens, ' + b('4em') + ' ones</p>'
    s2 += '</div>'
    
    # 3: Addition review (reduced from 9 to 6)
    s3 = build_addition_facts({
        "facts": [(8, 7), (9, 6), (5, 8), (7, 9), (6, 8), (9, 5)],
        "note": "Addition review.",
        "cols": 3,
    })
    
    # 4: Subtraction review (reduced from 9 to 6)
    s4 = build_subtraction_facts({
        "facts": [(15, 7), (14, 6), (18, 9), (13, 5), (17, 8), (20, 6)],
        "note": "Subtraction review.",
        "cols": 3,
    })
    
    # 5: NEW — Self-Check with reflection
    s5 = '<div class="selfcheck">'
    s5 += '<p><b>Self-Check: How do you feel about Phase 1 math?</b></p>'
    s5 += '<div style="display:flex; gap:20px; flex-wrap:wrap;">'
    s5 += '<div style="background:#fff3e0; padding:10px; border-radius:5px; flex:1; min-width:150px;">'
    s5 += '<p><b>Counting & Place Value</b></p>'
    s5 += '<p>😊 I got it!  |  😐 Getting there  |  😟 Need help</p>'
    s5 += '</div>'
    s5 += '<div style="background:#e8f5e9; padding:10px; border-radius:5px; flex:1; min-width:150px;">'
    s5 += '<p><b>Addition Facts</b></p>'
    s5 += '<p>😊 I got it!  |  😐 Getting there  |  😟 Need help</p>'
    s5 += '</div>'
    s5 += '<div style="background:#e3f2fd; padding:10px; border-radius:5px; flex:1; min-width:150px;">'
    s5 += '<p><b>Subtraction Facts</b></p>'
    s5 += '<p>😊 I got it!  |  😐 Getting there  |  😟 Need help</p>'
    s5 += '</div>'
    s5 += '</div>'
    s5 += '<p style="margin-top:10px;"><b>One thing I learned in Phase 1:</b> ' + bl() + '</p>'
    s5 += '<p><b>One thing I want to practice more:</b> ' + bl() + '</p>'
    s5 += '</div>'
    
    return page(6, "Friday", "Phase 1 Review & Self-Check",
        f'''{sec(1, "Counting Review", s1)}
{sec(2, "Place Value Review", s2)}
{sec(3, "Addition Review", s3)}
{sec(4, "Subtraction Review", s4)}
{sec(5, "Self-Check & Reflection", s5)}''')


# ═══════════════════════════════════════════════════
# TEACHER GUIDE — Combined for all days
# ═══════════════════════════════════════════════════
def teacher_guide():
    """Generate combined teacher guide for all 5 days."""
    return tg_page_multi(
        "Week 6", "Phase 1 Review — Teacher Guide",
        
        # Monday
        tg_section_title("Monday: Number Sense Review") +
        tg_sec(1, "Counting", ["15,20,30 / 30,40,60 / 90,80,60"]) +
        tg_sec(2, "Place Value", ["4/7, 8/3, 6/2 / 58"]) +
        tg_sec(3, "Number Talk", ["74>47 (7 tens>4 tens)", "63>58 (6 tens>5 tens)"]) +
        tg_sec(4, "Compare", ["<, <, >, ="]) +
        tg_sec(5, "Detective", ["Any of: 31, 33, 35, 37, 39"]) +
        tg_note("Page break"),
        
        # Tuesday
        tg_section_title("Tuesday: Addition Facts Review") +
        tg_sec(1, "Easy", ["8,12,10,11,10,10"]) +
        tg_sec(2, "Medium", ["13,13,13,14,14,15"]) +
        tg_sec(3, "Make 10", ["2,3,13 / 3,3,13 / 1,3,13"]) +
        tg_sec(4, "Doubles+1", ["13,11,15,17"]) +
        tg_sec(5, "Word Problems", ["15, 23"]) +
        tg_sec(6, "Pyramid", ["12, 13, 12 → 25, 25 → 50"]) +
        tg_note("Page break"),
        
        # Wednesday
        tg_section_title("Wednesday: Subtraction Facts Review") +
        tg_sec(1, "Easy", ["7,5,3,3,4,5"]) +
        tg_sec(2, "Medium", ["8,8,8,8,8,8"]) +
        tg_sec(3, "Subtract Using Addition", ["7,7 / 7,7 / 7,7 / 8,8"]) +
        tg_sec(4, "Word Problems", ["11, 9"]) +
        tg_sec(5, "Pyramid", ["8, 5, 3 → 3, 2 → 1"]) +
        tg_note("Page break"),
        
        # Thursday
        tg_section_title("Thursday: Mixed Addition and Subtraction") +
        tg_sec(1, "Mix", ["14, 8, 14, 6, 14, 7"]) +
        tg_sec(2, "Signs", ["+, −, +, −, +, −"]) +
        tg_sec(3, "Word Problems", ["9+6=15, 17-8=9"]) +
        tg_sec(4, "Two-Step", ["13, 18 / 13, 10 / 9, 13"]) +
        tg_sec(5, "Mystery", ["+, −, +, −"]) +
        tg_note("Page break"),
        
        # Friday
        tg_section_title("Friday: Phase 1 Review & Self-Check") +
        tg_sec(1, "Counting", ["30,40,60,70 / 15,25"]) +
        tg_sec(2, "Place Value", ["6/3, 49, 8/0"]) +
        tg_sec(3, "Addition", ["15,15,13,16,14,14"]) +
        tg_sec(4, "Subtraction", ["8,8,9,8,9,14"]) +
        tg_sec(5, "Self-Check", ["Accept student self-assessment and reflection"])
    )


# ═══════════════════════════════════════════════════
# GENERATE
# ═══════════════════════════════════════════════════
if __name__ == "__main__":
    # Clean old files
    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", "Number Sense Review"),
        (tuesday, "Tuesday", "Addition Facts Review"),
        (wednesday, "Wednesday", "Subtraction Facts Review"),
        (thursday, "Thursday", "Mixed Addition and Subtraction"),
        (friday, "Friday", "Phase 1 Review & Self-Check"),
    ]
    
    # Generate student worksheets
    for lesson_func, day, title in days:
        html = lesson_func()
        fname = f"Week_6_{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}")
    
    # Generate combined teacher guide
    html = teacher_guide()
    path = os.path.join(WEEK, "Week_6_Teacher_Guide.html")
    with open(path, "w") as f:
        f.write(html)
    pdf_path = os.path.join(WEEK, "Week_6_Teacher_Guide.pdf")
    HTML(filename=path).write_pdf(pdf_path)
    print(f"Generated: {pdf_path}")
    
    print("\nWeek 6 (rewritten) complete!")
