#!/usr/bin/env python3
"""
Week 4: Addition Facts to 20
Phase 1: Number Sense & Operations
"""

import os
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_pairs_to_make,
    build_addition_facts,
    build_number_bonds,
    build_make_10_strategy,
    build_missing_addend_facts,
    build_addition_word_problems,
    build_challenge_box,
)

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


# ═══════════════════════════════════════════════════
# MONDAY — Addition Facts Review (0-10)
# ═══════════════════════════════════════════════════
def monday():
    # 1: Learn "making 10" pairs
    s1 = build_pairs_to_make({
        "target": 10,
        "pairs": [(0, 10), (1, 9), (2, 8), (3, 7), (4, 6), (5, 5)],
        "note": "These pairs make 10. Memorize them!",
        "show_sums": True,
    })
    
    # 2: Addition facts to 15
    s2 = build_addition_facts({
        "facts": ["8 + 5", "7 + 6", "9 + 4", "6 + 7", "8 + 6", "9 + 5"],
        "note": "Add these numbers.",
        "cols": 2,
    })
    
    # 3: Addition facts to 20
    s3 = build_addition_facts({
        "facts": ["9 + 9", "8 + 8", "7 + 7", "6 + 6", "9 + 8", "8 + 7"],
        "note": "Practice larger sums.",
        "cols": 2,
    })
    
    # 4: Missing addend
    s4 = build_missing_addend_facts({
        "facts": ["5 + ___ = 10", "7 + ___ = 15", "8 + ___ = 16", "6 + ___ = 14"],
        "note": "What number is missing?",
        "cols": 2,
    })
    
    # 5: Quick challenge
    s5 = build_challenge_box({
        "problems": [
            {"text": "9 + 9 = " + b("4em")},
            {"text": "8 + 7 = " + b("4em")},
            {"text": "7 + 8 = " + b("4em")},
        ]
    })
    
    body = f'''{sec(1, "Making 10 Pairs", s1)}
{sec(2, "Sums to 15", s2)}
{sec(3, "Sums to 20", s3)}
{sec(4, "Missing Addend", s4)}
{sec(5, "Challenge", s5)}'''
    
    return page(4, "Monday", "Addition Facts Review", body)


# ═══════════════════════════════════════════════════
# TUESDAY — Number Bonds
# ═══════════════════════════════════════════════════
def tuesday():
    # 1: Number bonds to 10
    s1 = build_number_bonds({
        "bonds": [
            {"total": 10, "part1": 7, "part2": None},
            {"total": 10, "part1": None, "part2": 3},
            {"total": 10, "part1": 4, "part2": None},
            {"total": 10, "part1": None, "part2": 6},
        ],
        "note": "Fill in the missing part of each number bond.",
        "cols": 2,
    })
    
    # 2: Number bonds to 15
    s2 = build_number_bonds({
        "bonds": [
            {"total": 15, "part1": 10, "part2": None},
            {"total": 15, "part1": None, "part2": 8},
            {"total": 15, "part1": 12, "part2": None},
            {"total": 15, "part1": None, "part2": 5},
        ],
        "note": "Fill in the missing part.",
        "cols": 2,
    })
    
    # 3: Number bonds to 20
    s3 = build_number_bonds({
        "bonds": [
            {"total": 20, "part1": 15, "part2": None},
            {"total": 20, "part1": None, "part2": 12},
            {"total": 20, "part1": 18, "part2": None},
            {"total": 20, "part1": None, "part2": 7},
        ],
        "note": "Fill in the missing part.",
        "cols": 2,
    })
    
    # 4: Three-part number bonds
    s4 = build_number_bonds({
        "bonds": [
            {"total": 10, "part1": 3, "part2": 4, "part3": None},
            {"total": 15, "part1": 5, "part2": None, "part3": 6},
            {"total": 20, "part1": 10, "part2": None, "part3": 5},
            {"total": 12, "part1": None, "part2": 4, "part3": 3},
        ],
        "note": "Find the missing part.",
        "cols": 2,
    })
    
    # 5: Draw number bonds
    s5 = build_number_bonds({
        "bonds": [
            {"total": 12, "part1": None, "part2": None},
            {"total": 18, "part1": None, "part2": None},
        ],
        "note": "Write two numbers that make the total.",
        "cols": 2,
    })
    
    body = f'''{sec(1, "Bonds to 10", s1)}
{sec(2, "Bonds to 15", s2)}
{sec(3, "Bonds to 20", s3)}
{sec(4, "Three-Part Bonds", s4)}
{sec(5, "Draw Your Own", s5)}'''
    
    return page(4, "Tuesday", "Number Bonds", body)


# ═══════════════════════════════════════════════════
# WEDNESDAY — Making 10 Strategy
# ═══════════════════════════════════════════════════
def wednesday():
    # 1: Learn the strategy (example format)
    s1 = build_make_10_strategy({
        "problems": [(8, 5)],
        "show_steps": True,
        "note": "To add 8 + 5, think: 8 + 2 = 10, then 10 + 3 = 13",
    })
    
    # 2: Practice with 8 +
    s2 = build_make_10_strategy({
        "problems": [(8, 4), (8, 5), (8, 6), (8, 7)],
        "show_steps": True,
        "note": "Use the \"make 10\" strategy.",
    })
    
    # 3: Practice with 9 +
    s3 = build_make_10_strategy({
        "problems": [(9, 3), (9, 4), (9, 5), (9, 6)],
        "show_steps": True,
        "note": "Use the \"make 10\" strategy.",
    })
    
    # 4: Mixed practice
    s4 = build_addition_facts({
        "facts": ["7 + 5", "6 + 6", "8 + 4", "9 + 2", "7 + 6", "8 + 5"],
        "note": "Use any strategy you know.",
        "cols": 2,
    })
    
    # 5: Challenge problems
    s5 = build_challenge_box({
        "problems": [
            {"text": "9 + 8 = ? (Hint: 9 + 1 = 10, then 10 + 7 = ?) " + b("4em")},
            {"text": "8 + 7 = ? (Hint: 8 + 2 = 10, then 10 + 5 = ?) " + b("4em")},
            {"text": "7 + 6 = ? (Hint: 7 + 3 = 10, then 10 + 3 = ?) " + b("4em")},
        ]
    })
    
    body = f'''{sec(1, "Learn the Strategy", s1)}
{sec(2, "Practice with 8+", s2)}
{sec(3, "Practice with 9+", s3)}
{sec(4, "Mixed Practice", s4)}
{sec(5, "Challenge", s5)}'''
    
    return page(4, "Wednesday", "Making 10 Strategy", body)


# ═══════════════════════════════════════════════════
# THURSDAY — Addition Word Problems
# ═══════════════════════════════════════════════════
def thursday():
    # 1: Simple word problems
    s1 = build_addition_word_problems({
        "problems": [
            {"text": "Sarah has 8 stickers. Tom gives her 5 more. How many stickers does Sarah have now?"},
            {"text": "There are 7 birds on a tree. 4 more birds fly in. How many birds are there now?"},
            {"text": "Maya has 6 apples. Her mom buys 8 more. How many apples does Maya have?"},
        ]
    })
    
    # 2: Two-step problems
    s2 = build_addition_word_problems({
        "problems": [
            {"text": "Jake has 5 toy cars. His dad buys him 4 more. Then his grandma gives him 3 more. How many cars does Jake have?"},
            {"text": "Lily has 8 stickers. She gets 5 more for her birthday. Then she finds 2 more. How many stickers does Lily have?"},
        ]
    })
    
    # 3: Compare and add
    s3 = build_addition_word_problems({
        "problems": [
            {"text": "Tom has 9 marbles. Sarah has 6 marbles. How many marbles do they have together? ___ marbles"},
            {"text": "Class A has 12 students. Class B has 8 students. How many students total? ___ students"},
            {"text": "Mom baked 7 cookies. Dad baked 9 cookies. How many cookies total? ___ cookies"},
            {"text": "Sam read 8 pages. Mike read 7 pages. How many pages did they read together? ___ pages"},
        ]
    })
    
    # 4: Missing information
    s4 = build_addition_word_problems({
        "problems": [
            {"text": "Sarah had some stickers. She got 5 more. Now she has 13. How many did she start with? ___ stickers"},
            {"text": "Tom had 8 marbles. He got some more. Now he has 15. How many did he get? ___ marbles"},
        ]
    })
    
    # 5: Challenge word problems
    s5 = build_challenge_box({
        "problems": [
            {"text": "Emma has 9 stickers. Jake has 7 stickers. Lily has 8 stickers. How many stickers do they have altogether? " + b("4em")},
            {"text": "There are 6 red balloons, 7 blue balloons, and 5 green balloons. How many balloons total? " + b("4em")},
        ]
    })
    
    body = f'''{sec(1, "Simple Problems", s1)}
{sec(2, "Two-Step Problems", s2)}
{sec(3, "Compare and Add", s3)}
{sec(4, "Missing Information", s4)}
{sec(5, "Challenge", s5)}'''
    
    return page(4, "Thursday", "Addition Word Problems", body)


# ═══════════════════════════════════════════════════
# FRIDAY — Week 4 Review & Fluency
# ═══════════════════════════════════════════════════
def friday():
    # 1: Quick fact fluency
    s1 = build_addition_facts({
        "facts": ["5 + 5", "6 + 6", "7 + 3", "8 + 4", "9 + 2", "7 + 4", "8 + 5", "9 + 3", "6 + 7", "8 + 6", "7 + 5", "9 + 4"],
        "note": "Solve as many as you can in 2 minutes!",
        "cols": 4,
        "font_size": 14,
    })
    
    # 2: Making 10 review
    s2 = build_missing_addend_facts({
        "facts": ["8 + ___ = 10", "9 + ___ = 10", "7 + ___ = 10", "6 + ___ = 10", "5 + ___ = 10", "4 + ___ = 10"],
        "note": "Fill in the blanks.",
        "cols": 3,
    })
    
    # 3: Number bond review
    s3 = build_number_bonds({
        "bonds": [
            {"total": 10, "part1": 7, "part2": None},
            {"total": 15, "part1": None, "part2": 8},
            {"total": 20, "part1": 12, "part2": None},
            {"total": 10, "part1": None, "part2": 6},
        ],
        "note": "Fill in the missing part.",
        "cols": 2,
    })
    
    # 4: Mixed addition facts
    s4 = build_addition_facts({
        "facts": ["8 + 7", "9 + 6", "7 + 8", "6 + 9", "8 + 8", "9 + 9"],
        "note": "Solve each problem.",
        "cols": 2,
    })
    
    # 5: Word problem review
    s5 = build_addition_word_problems({
        "problems": [
            {"text": "Tom has 8 toy cars. Sarah has 7 toy cars. How many cars do they have together?"},
            {"text": "Lily read 9 pages on Monday and 8 pages on Tuesday. How many pages did she read?"},
        ]
    })
    
    body = f'''{sec(1, "Fact Fluency", s1)}
{sec(2, "Making 10", s2)}
{sec(3, "Number Bonds", s3)}
{sec(4, "Mixed Facts", s4)}
{sec(5, "Word Problems", s5)}'''
    
    return page(4, "Friday", "Week 4 Review & Fluency", body)


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

def teacher_guide():
    """Generate combined teacher guide for all 5 days."""
    return tg_page_multi(
        "Week 4", "Addition Facts to 20 — Teacher Guide",
        
        # Monday
        tg_section_title("Monday: Addition Facts Review") +
        tg_sec(1, "Making 10", ["10", "10", "10", "10", "10", "10"]) +
        tg_sec(2, "Sums to 15", ["13", "13", "13", "13", "14", "14"]) +
        tg_sec(3, "Sums to 20", ["18", "16", "14", "12", "17", "15"]) +
        tg_sec(4, "Missing Addend", ["5", "8", "8", "8"]) +
        tg_sec(5, "Challenge", ["18", "15", "15"]) +
        tg_note("Page break"),
        
        # Tuesday
        tg_section_title("Tuesday: Number Bonds") +
        tg_sec(1, "Bonds to 10", ["3", "7", "6", "4"]) +
        tg_sec(2, "Bonds to 15", ["5", "7", "3", "10"]) +
        tg_sec(3, "Bonds to 20", ["5", "8", "2", "13"]) +
        tg_sec(4, "Three-Part", ["3", "4", "5", "5"]) +
        tg_sec(5, "Draw Your Own", ["Any pair making 12", "Any pair making 18"]) +
        tg_note("Page break"),
        
        # Wednesday
        tg_section_title("Wednesday: Making 10 Strategy") +
        tg_sec(1, "Learn Strategy", ["13"]) +
        tg_sec(2, "Practice 8+", ["12", "13", "14", "15"]) +
        tg_sec(3, "Practice 9+", ["12", "13", "14", "15"]) +
        tg_sec(4, "Mixed Practice", ["12", "12", "12", "11", "13", "13"]) +
        tg_sec(5, "Challenge", ["17", "15", "13"]) +
        tg_note("Page break"),
        
        # Thursday
        tg_section_title("Thursday: Addition Word Problems") +
        tg_sec(1, "Simple Problems", ["13", "11", "14"]) +
        tg_sec(2, "Two-Step", ["12", "15"]) +
        tg_sec(3, "Compare and Add", ["15", "20", "16", "15"]) +
        tg_sec(4, "Missing Info", ["8", "7"]) +
        tg_sec(5, "Challenge", ["24", "18"]) +
        tg_note("Page break"),
        
        # Friday
        tg_section_title("Friday: Week 4 Review & Fluency") +
        tg_sec(1, "Fact Fluency", ["10", "12", "10", "12", "11", "11", "13", "12", "13", "14", "12", "13"]) +
        tg_sec(2, "Making 10", ["2", "1", "3", "4", "5", "6"]) +
        tg_sec(3, "Number Bonds", ["3", "7", "8", "4"]) +
        tg_sec(4, "Mixed Facts", ["15", "15", "15", "15", "16", "18"]) +
        tg_sec(5, "Word Problems", ["15", "17"])
    )


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

if __name__ == "__main__":
    # Generate all days at once
    days = [
        (monday, "Monday", "Addition Facts Review"),
        (tuesday, "Tuesday", "Number Bonds"),
        (wednesday, "Wednesday", "Making 10 Strategy"),
        (thursday, "Thursday", "Addition Word Problems"),
        (friday, "Friday", "Week 4 Review & Fluency"),
    ]
    
    # Generate student worksheets
    for lesson_func, day, title in days:
        html = lesson_func()
        fname = f"Week_4_{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_4_Teacher_Guide.html")
    with open(path, "w") as f:
        f.write(html)
    pdf_path = os.path.join(WEEK, "Week_4_Teacher_Guide.pdf")
    HTML(filename=path).write_pdf(pdf_path)
    print(f"Generated: {pdf_path}")
    
    print("Week 4 complete!")