#!/usr/bin/env python3
"""
Week 2: Place Value (Tens & Ones)
Phase 1: Number Sense & Operations
"""

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_grid_answers, tg_note, tg_section_title,
)
from activity_builders import (
    build_tens_ones_chart,
    build_expanded_form,
    build_build_number,
    build_compare_place_value,
    build_read_tens_ones,
    build_write_tens_ones,
    build_decompose_two_ways,
    build_place_value_table_full,
)

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


# ═══════════════════════════════════════════════════
# MONDAY β€” Tens and Ones Introduction
# ═══════════════════════════════════════════════════
def monday():
    # 1: What is a ten?
    s1 = '<p class="note">Each group of 10 sticks is called a <b>"ten"</b>. Count the groups.</p>'
    s1 += '<div class="mgrid c3">'
    s1 += '<div class="abox"><p><span style="color:#4caf50;">||||||||||</span></p><p>1 group = ' + b('4em') + ' ten(s)</p></div>'
    s1 += '<div class="abox"><p><span style="color:#4caf50;">|||||||||| ||||||||||</span></p><p>2 groups = ' + b('4em') + ' ten(s)</p></div>'
    s1 += '<div class="abox"><p><span style="color:#4caf50;">|||||||||| |||||||||| |||||||||</span></p><p>3 groups = ' + b('4em') + ' ten(s)</p></div>'
    s1 += '</div>'
    
    # 2: Ones are left over
    s2 = '<p class="note">The ones that do not make a full ten stay as <b>"ones"</b>.</p>'
    s2 += '<div class="mgrid c2">'
    s2 += '<div class="abox"><p><span style="color:#4caf50;">||||||||||</span> || ||||</p><p>Tens: ' + b('4em') + '  Ones: ' + b('4em') + '</p></div>'
    s2 += '<div class="abox"><p><span style="color:#4caf50;">|||||||||| ||||||||||</span> ||||</p><p>Tens: ' + b('4em') + '  Ones: ' + b('4em') + '</p></div>'
    s2 += '<div class="abox"><p><span style="color:#4caf50;">|||||||||| |||||||||| |||||||||| |||||||||</span> || |||</p><p>Tens: ' + b('4em') + '  Ones: ' + b('4em') + '</p></div>'
    s2 += '</div>'
    
    # 3: Read tens and ones, write the number
    s3 = build_read_tens_ones({
        "phrases": [
            "3 tens and 4 ones", "5 tens and 2 ones",
            "1 ten and 9 ones", "7 tens and 6 ones"
        ]
    })
    
    # 4: Write tens and ones
    s4 = build_write_tens_ones({
        "numbers": [27, 63, 15]
    })
    
    # 5: Tens and ones chart (removed drawing activity)
    s5 = build_tens_ones_chart({
        "numbers": [12, 35, 70, 46],
        "note": "Fill in the chart."
    })
    
    return page(2, "Monday", "Tens and Ones Introduction",
        f'''{sec(1, "What Is a Ten?", s1)}
{sec(2, "Ones Are Left Over", s2)}
{sec(3, "Read Tens and Ones", s3)}
{sec(4, "Write Tens and Ones", s4)}
{sec(5, "Tens and Ones Chart", s5)}'''
    )


# ═══════════════════════════════════════════════════
# TUESDAY β€” Expanded Form & Decomposing
# ═══════════════════════════════════════════════════
def tuesday():
    # 1: Expanded form introduction
    s1 = build_expanded_form({
        "numbers": [46, 71],
        "show_examples": True,
        "note": "Write each number as tens + ones.",
        "cols": 2
    })
    
    # 2: Write in expanded form
    s2 = build_expanded_form({
        "numbers": [67, 15, 94, 30],
        "show_examples": False,
        "cols": 2
    })
    
    # 3: Build the number from expanded form
    s3 = build_build_number({
        "additions": [(40, 5), (70, 9), (20, 8)]
    })
    
    # 4: Decompose in two ways
    s4 = build_decompose_two_ways({
        "numbers": [36, 54]
    })
    
    # 5: Place value table (removed word problems)
    s5 = build_place_value_table_full({
        "numbers": [41, 73, 28, 95]
    })
    
    return page(2, "Tuesday", "Expanded Form & Decomposing",
        f'''{sec(1, "Expanded Form β€” Examples", s1)}
{sec(2, "Write in Expanded Form", s2)}
{sec(3, "Put It Together", s3)}
{sec(4, "Decompose in Two Ways", s4)}
{sec(5, "Place Value Table", s5)}'''
    )


# ═══════════════════════════════════════════════════
# WEDNESDAY β€” Mixed Place Value Practice
# ═══════════════════════════════════════════════════
def wednesday():
    # 1: Tens or ones place?
    s1 = '<p class="note">Circle whether the digit is in the tens or ones place.</p>'
    s1 += '<div class="mgrid c2">'
    nums = [(68, 8, "ones"), (35, 3, "tens"), (92, 2, "ones")]
    for n, digit, _ in nums:
        s1 += f'<div class="abox"><p>{n} β†’ <span style="text-decoration:underline;">{digit}</span> is <b>tens</b> / <b>ones</b></p></div>'
    s1 += '</div>'
    
    # 2: Fill the tens and ones table
    s2 = build_tens_ones_chart({
        "numbers": [56, 83, 14, 77],
        "note": "Fill in the table."
    })
    
    # 3: Expanded form practice
    s3 = build_expanded_form({
        "numbers": [29, 64, 81, 33],
        "show_examples": False,
        "cols": 2
    })
    
    # 4: Build the number from expanded form
    s4 = build_build_number({
        "additions": [(50, 7), (10, 6), (80, 2)]
    })
    
    # 5: Compare place values (removed drawing activity)
    s5 = build_compare_place_value({
        "comparisons": [
            {"left": "3 tens", "right": "2 tens"},
            {"left": "5 ones", "right": "8 ones"},
            {"left": "6 tens and 4 ones", "right": "64"},
            {"left": "4 tens and 9 ones", "right": "40"}
        ]
    })
    
    return page(2, "Wednesday", "Mixed Place Value Practice",
        f'''{sec(1, "Tens or Ones?", s1)}
{sec(2, "Fill the Table", s2)}
{sec(3, "Expanded Form", s3)}
{sec(4, "Build the Number", s4)}
{sec(5, "Compare Place Values", s5)}'''
    )


# ═══════════════════════════════════════════════════
# THURSDAY β€” Place Value in Real Life
# ═══════════════════════════════════════════════════
def thursday():
    # 1: Real-world word problems
    s1 = '<div class="wp">'
    s1 += '<p>A bakery has 3 boxes of 10 muffins and 4 extra muffins. How many muffins? ' + b('4em') + '</p>'
    s1 += '<p>There are 5 packs of 10 pencils and 7 loose pencils. How many pencils? ' + b('4em') + '</p></div>'
    
    # 2: Money as tens and ones
    s2 = '<p class="note">Each $10 bill is a ten. Each $1 coin is a one.</p>'
    s2 += '<div class="mgrid c2">'
    s2 += '<div class="abox"><p>4 ten-dollar bills and 3 one-dollar coins</p><p> = $' + b('4em') + '</p></div>'
    s2 += '<div class="abox"><p>2 ten-dollar bills and 8 one-dollar coins</p><p> = $' + b('4em') + '</p></div>'
    s2 += '</div>'
    
    # 3: Group into tens
    s3 = '<p class="note">How many tens and ones can you make?</p>'
    s3 += '<div class="mgrid c2">'
    s3 += '<div class="abox"><p>37 buttons = ' + b('4em') + ' tens and ' + b('4em') + ' ones</p></div>'
    s3 += '<div class="abox"><p>62 marbles = ' + b('4em') + ' tens and ' + b('4em') + ' ones</p></div>'
    s3 += '<div class="abox"><p>85 stickers = ' + b('4em') + ' tens and ' + b('4em') + ' ones</p></div>'
    s3 += '</div>'
    
    # 4: Shopping with place value (removed expanded form word problems)
    s4 = '<table class="tbl"><tr><th>Item</th><th>Price</th><th>Tens</th><th>Ones</th></tr>'
    for item, price in [("Backpack", 47), ("Shoes", 63), ("Jacket", 81)]:
        s4 += f'<tr><td>{item}</td><td>${price}</td><td>{b("4em")}</td><td>{b("4em")}</td></tr>'
    s4 += '</table>'
    
    # 5: Place value riddles
    s5 = '<div class="starbox"><p><b>Place Value Riddles</b></p>'
    s5 += '<p>"I am between 40 and 50. My ones digit is 7." β†’ ' + b('4em') + '</p>'
    s5 += '<p>"My tens digit is 3 more than my ones digit. My ones digit is 1." β†’ ' + b('4em') + '</p></div>'
    
    return page(2, "Thursday", "Place Value in Real Life",
        f'''{sec(1, "Real-World Tens and Ones", s1)}
{sec(2, "Money Tens and Ones", s2)}
{sec(3, "Group Into Tens", s3)}
{sec(4, "Shopping with Place Value", s4)}
{sec(5, "Place Value Riddles", s5)}'''
    )


# ═══════════════════════════════════════════════════
# FRIDAY β€” Week 2 Review & Challenge
# ═══════════════════════════════════════════════════
def friday():
    # 1: Tens and ones review table
    s1 = build_tens_ones_chart({
        "numbers": [17, 44, 70, 93],
        "note": "Tens and ones review."
    })
    
    # 2: Expanded form review
    s2 = build_expanded_form({
        "numbers": [38, 61, 95, 24],
        "show_examples": False,
        "cols": 2
    })
    
    # 3: Build the number
    s3 = build_build_number({
        "additions": [(40, 9), (80, 5), (30, 2)]
    })
    
    # 4: Which place is the digit in?
    s4 = '<p class="note">Which place is the digit in?</p>'
    s4 += '<div class="mgrid c2">'
    s4 += '<div class="abox"><p>In 58, the 8 is the ' + b('5em') + ' digit</p></div>'
    s4 += '<div class="abox"><p>In 32, the 3 is the ' + b('5em') + ' digit</p></div>'
    s4 += '<div class="abox"><p>In 71, the 1 is the ' + b('5em') + ' digit</p></div>'
    s4 += '</div>'
    
    # 5: Word problem review (removed compare activity)
    s5 = '<div class="wp">'
    s5 += '<p>Lily has 5 tens and 3 ones. How many does she have? ' + b('4em') + '</p>'
    s5 += '<p>Jake has $42. How many $10 bills? ' + b('4em') + ' How many $1 coins? ' + b('4em') + '</p></div>'
    
    return page(2, "Friday", "Week 2 Review & Challenge",
        f'''{sec(1, "Tens and Ones Review", s1)}
{sec(2, "Expanded Form Review", s2)}
{sec(3, "Build the Number", s3)}
{sec(4, "Place Value β€” Which Place?", s4)}
{sec(5, "Word Problem Review", s5)}'''
    )


# ═══════════════════════════════════════════════════
# COMBINED TEACHER GUIDE
# ═══════════════════════════════════════════════════

def teacher_guide():
    """Generate combined teacher guide for all 5 days."""
    return tg_page_multi(
        "Week 2", "Place Value (Tens & Ones) β€” Teacher Guide",
        
        # Monday
        tg_section_title("Monday: Tens and Ones Introduction") +
        tg_sec(1, "What Is a Ten?", ["1", "2", "3"]) +
        tg_sec(2, "Ones Left Over", ["1 ten, 3 ones", "2 tens, 4 ones", "4 tens, 3 ones"]) +
        tg_sec(3, "Read Tens and Ones", [
            "3 tens + 4 ones = 34", "5 tens + 2 ones = 52",
            "1 ten + 9 ones = 19", "7 tens + 6 ones = 76"
        ]) +
        tg_sec(4, "Write Tens and Ones", [
            "27 = 2 tens, 7 ones", "63 = 6 tens, 3 ones", "15 = 1 ten, 5 ones"
        ]) +
        tg_sec(5, "Draw the Number", ["24: 2 rods, 4 cubes", "57: 5 rods, 7 cubes"]) +
        tg_sec(6, "Chart", ["12:1/2", "35:3/5", "70:7/0", "46:4/6"]) +
        tg_note("Page break"),
        
        # Tuesday
        tg_section_title("Tuesday: Expanded Form & Decomposing") +
        tg_sec(1, "Expanded Form", ["46=40+6", "71=70+1"]) +
        tg_sec(2, "Write Expanded Form", [
            "67=60+7", "15=10+5", "94=90+4", "30=30+0"
        ]) +
        tg_sec(3, "Put Together", ["45", "79", "28"]) +
        tg_sec(4, "Decompose", ["36: 30+6 or 20+16", "54: 50+4 or 40+14"]) +
        tg_sec(5, "Table", ["41:4/1/40+1", "73:7/3/70+3", "28:2/8/20+8", "95:9/5/90+5"]) +
        tg_sec(6, "Word Problems", ["43", "76"]) +
        tg_note("Page break"),
        
        # Wednesday
        tg_section_title("Wednesday: Mixed Place Value Practice") +
        tg_sec(1, "Tens or Ones?", ["8β†’ones", "3β†’tens", "2β†’ones"]) +
        tg_sec(2, "Table", ["56:5/6", "83:8/3", "14:1/4", "77:7/7"]) +
        tg_sec(3, "Expanded Form", ["29=20+9", "64=60+4", "81=80+1", "33=30+3"]) +
        tg_sec(4, "Build Number", ["57", "16", "82"]) +
        tg_sec(5, "Draw Base-10 Blocks", ["45: 4 rods, 5 cubes", "72: 7 rods, 2 cubes"]) +
        tg_sec(6, "Compare", [">", "<", "=", ">"]) +
        tg_note("Page break"),
        
        # Thursday
        tg_section_title("Thursday: Place Value in Real Life") +
        tg_sec(1, "Real-World Tens and Ones", ["34 muffins", "57 pencils"]) +
        tg_sec(2, "Money Tens and Ones", ["$43", "$28"]) +
        tg_sec(3, "Group Into Tens", ["37: 3 tens, 7 ones", "62: 6 tens, 2 ones", "85: 8 tens, 5 ones"]) +
        tg_sec(4, "Expanded Form Word Problems", ["20+6=26", "50+9=59"]) +
        tg_sec(5, "Shopping", ["Backpack:4/7", "Shoes:6/3", "Jacket:8/1"]) +
        tg_sec(6, "Riddles", ["47", "41"]) +
        tg_note("Page break"),
        
        # Friday
        tg_section_title("Friday: Week 2 Review & Challenge") +
        tg_sec(1, "Tens and Ones Review", ["17:1/7", "44:4/4", "70:7/0", "93:9/3"]) +
        tg_sec(2, "Expanded Form Review", ["38=30+8", "61=60+1", "95=90+5", "24=20+4"]) +
        tg_sec(3, "Build the Number", ["49", "85", "32"]) +
        tg_sec(4, "Which Place?", ["ones", "tens", "ones"]) +
        tg_sec(5, "Compare", ["<", "<", "="]) +
        tg_sec(6, "Word Problems", ["53", "4 tens and 2 ones"])
    )


# ═══════════════════════════════════════════════════
# GENERATE
# ═══════════════════════════════════════════════════

if __name__ == "__main__":
    # Generate all days at once
    days = [
        (monday, "Monday", "Tens and Ones Introduction"),
        (tuesday, "Tuesday", "Expanded Form & Decomposing"),
        (wednesday, "Wednesday", "Mixed Place Value Practice"),
        (thursday, "Thursday", "Place Value in Real Life"),
        (friday, "Friday", "Week 2 Review & Challenge"),
    ]
    
    # Generate student worksheets
    for lesson_func, day, title in days:
        html = lesson_func()
        fname = f"Week_2_{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_2_Teacher_Guide.html")
    with open(path, "w") as f:
        f.write(html)
    pdf_path = os.path.join(WEEK, "Week_2_Teacher_Guide.pdf")
    HTML(filename=path).write_pdf(pdf_path)
    print(f"Generated: {pdf_path}")
    
    print("Week 2 complete!")