#!/usr/bin/env python3
"""
Week 5: Subtraction Facts — From 10 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,
    generate_simple,
    tg_page, tg_page_multi, tg_sec, tg_grid_answers, tg_note, tg_section_title,
    BLUE, GREEN, RED, YELLOW, ORANGE, PURPLE, PINK, TEAL, GOLD,
)
from activity_builders import (
    build_subtraction_facts,
    build_subtraction_word_problems,
    build_fact_families_subtraction,
    build_fact_families,
    build_challenge_box,
)

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


# ═══════════════════════════════════════════════════
# MONDAY — Subtraction Facts from 10
# ═══════════════════════════════════════════════════
def monday():
    # 1: Subtraction is the opposite
    s1 = build_challenge_box({
        "problems": [
            {"text": "<b>Subtraction is the Opposite of Addition</b>"},
            {"text": "3 + 7 = 10"},
            {"text": "10 − 3 = 7  |  10 − 7 = 3"},
            {"text": "One fact family — same 3 numbers!"},
        ]
    })
    
    # 2: Subtract from 10 (combined practice)
    s2 = build_subtraction_facts({
        "facts": [(10, 3), (10, 7), (10, 6), (10, 4), (10, 9), (10, 1), (10, 2), (10, 8), (10, 5)],
        "note": "Fill in the answer.",
        "cols": 3,
    })
    
    # 3: Use addition to help subtract
    s3 = build_challenge_box({
        "problems": [
            {"text": "10 − 5: 5 + " + b("5em") + " = 10, so 10 − 5 = " + b("5em")},
            {"text": "10 − 4: 4 + " + b("5em") + " = 10, so 10 − 4 = " + b("5em")},
            {"text": "10 − 8: 8 + " + b("5em") + " = 10, so 10 − 8 = " + b("5em")},
            {"text": "10 − 2: 2 + " + b("5em") + " = 10, so 10 − 2 = " + b("5em")},
        ]
    })
    
    # 4: Missing numbers
    s4 = build_challenge_box({
        "problems": [
            {"text": b("5em") + " − 3 = 7"},
            {"text": "10 − " + b("5em") + " = 4"},
            {"text": b("5em") + " − 6 = 4"},
            {"text": "10 − " + b("5em") + " = 9"},
            {"text": b("5em") + " − 8 = 2"},
            {"text": "10 − " + b("5em") + " = 1"},
        ]
    })
    
    # 5: Subtraction chain
    s5 = build_challenge_box({
        "problems": [
            {"text": "<b>Subtraction Chain</b> — Start at 20, subtract 2 each time."},
            {"text": "20 − 2 = " + b("5em") + ", − 2 = " + b("5em") + ", − 2 = " + b("5em") + ", − 2 = " + b("5em") + ", − 2 = " + b("5em")},
        ]
    })
    
    body = f'''{sec(1, "Subtraction is the Opposite", s1)}
{sec(2, "Subtract from 10", s2, pb=True)}
{sec(3, "Use Addition to Help Subtract", s3)}
{sec(4, "Missing Numbers", s4)}
{sec(5, "Challenge: Subtraction Chain", s5)}'''
    
    return page_multi(5, "Monday", "Subtraction Facts — From 10", body)


# ═══════════════════════════════════════════════════
# TUESDAY — Subtracting 11-20
# ═══════════════════════════════════════════════════
def tuesday():
    # 1: Review subtract from 10
    s1 = build_subtraction_facts({
        "facts": [(10, 4), (10, 7), (10, 2), (10, 5), (10, 8), (10, 3), (10, 6), (10, 9), (10, 1)],
        "note": "Review: subtract from 10.",
        "cols": 3,
    })
    
    # 2: Learn subtracting 11-20
    s2 = build_challenge_box({
        "problems": [
            {"text": "<b>Subtracting from Numbers 11-20</b>"},
            {"text": "13 − 5: Think 'What adds to 5 to make 13?' or '13 − 3 = 10, then 10 − 2 = 8'"},
        ]
    })
    
    # 3: Practice problems (combined)
    s3 = build_subtraction_facts({
        "facts": [(13, 5), (14, 6), (12, 4), (15, 7), (11, 3), (16, 8), (17, 9), (18, 10), (19, 11)],
        "note": "Use any strategy.",
        "cols": 3,
    })
    
    # 4: Fill in the steps
    s4 = build_challenge_box({
        "problems": [
            {"text": "14 − 6: 14 − " + b("5em") + " = 10, 10 − " + b("5em") + " = " + b("5em")},
            {"text": "15 − 7: 15 − " + b("5em") + " = 10, 10 − " + b("5em") + " = " + b("5em")},
            {"text": "16 − 8: 16 − " + b("5em") + " = 10, 10 − " + b("5em") + " = " + b("5em")},
            {"text": "17 − 9: 17 − " + b("5em") + " = 10, 10 − " + b("5em") + " = " + b("5em")},
        ]
    })
    
    # 5: Explain thinking
    s5 = build_challenge_box({
        "problems": [
            {"text": "<b>Explain Your Thinking</b>"},
            {"text": "How would you solve 16 − 8? Write or draw your strategy."},
            {"text": "<div style='min-height:60px; border:1px dashed #ccc;'></div>"},
        ]
    })
    
    body = f'''{sec(1, "Review: Subtract from 10", s1, pb=True)}
{sec(2, "Subtracting from 11-20", s2)}
{sec(3, "Practice Problems", s3, pb=True)}
{sec(4, "Fill in the Steps", s4)}
{sec(5, "Challenge: Explain Your Thinking", s5)}'''
    
    return page_multi(5, "Tuesday", "Subtracting 11-20", body)


# ═══════════════════════════════════════════════════
# WEDNESDAY — Mixed Subtraction Practice
# ═══════════════════════════════════════════════════
def wednesday():
    # 1: Quick review
    s1 = build_subtraction_facts({
        "facts": [(10, 5), (10, 8), (10, 3), (10, 7), (10, 4), (10, 9), (10, 2), (10, 6), (10, 1)],
        "note": "Quick review.",
        "cols": 3,
    })
    
    # 2: Subtract from 11-20
    s2 = build_subtraction_facts({
        "facts": [(12, 5), (13, 6), (14, 7), (15, 8), (16, 9), (17, 10), (18, 11), (19, 12)],
        "note": "Subtract from 11-20.",
        "cols": 2,
    })
    
    # 3: Solve any way
    s3 = build_subtraction_facts({
        "facts": [(15, 9), (16, 8), (17, 7), (18, 6), (19, 5), (14, 8), (13, 7), (12, 6), (11, 5)],
        "note": "Solve using any strategy.",
        "cols": 3,
    })
    
    # 4: Word problems (combined)
    s4 = build_subtraction_word_problems({
        "problems": [
            {"text": "Tom had 15 stickers. He gave 7 to his friend. How many does he have left? 15 − 7 = " + b("5em")},
            {"text": "There were 18 cookies on the plate. Sarah ate 9. How many are left? 18 − 9 = " + b("5em")},
        ]
    })
    
    # 5: Challenge
    s5 = build_challenge_box({
        "problems": [
            {"text": "<b>Challenge Problems</b>"},
            {"text": "1. 20 − 13 = " + b("5em")},
            {"text": "2. 19 − 12 = " + b("5em")},
            {"text": "3. 17 − 9 = " + b("5em")},
            {"text": "4. Write your own word problem: " + bl()},
        ]
    })
    
    body = f'''{sec(1, "Quick Review", s1, pb=True)}
{sec(2, "Subtract from 11-20", s2)}
{sec(3, "Solve Any Way", s3, pb=True)}
{sec(4, "Word Problems", s4)}
{sec(5, "Challenge Problems", s5)}'''
    
    return page_multi(5, "Wednesday", "Mixed Subtraction Practice", body)


# ═══════════════════════════════════════════════════
# THURSDAY — Fact Families
# ═══════════════════════════════════════════════════
def thursday():
    # 1: What is a fact family
    s1 = build_challenge_box({
        "problems": [
            {"text": "<b>Fact Families</b>"},
            {"text": "3, 7, 10 make a family:"},
            {"text": "3 + 7 = 10  |  7 + 3 = 10  |  10 − 3 = 7  |  10 − 7 = 3"},
        ]
    })
    
    # 2: Complete fact families
    s2 = build_fact_families({
        "families": [[4, 6, 10], [5, 5, 10], [3, 8, 11], [6, 7, 13]],
        "note": "Complete each fact family.",
        "show_triangle": True,
        "include_subtraction": True,
    })
    
    # 3: Related subtraction facts
    s3 = build_fact_families_subtraction({
        "families": [[5, 6, 11], [7, 8, 15], [4, 9, 13], [6, 8, 14]],
        "note": "Write the related subtraction fact.",
        "cols": 2,
    })
    
    # 4: Find missing number
    s4 = build_challenge_box({
        "problems": [
            {"text": b("5em") + ", 7, 12 (7 + 5 = 12, 12 − 7 = 5)"},
            {"text": "6, " + b("5em") + ", 15 (6 + 9 = 15, 15 − 6 = 9)"},
            {"text": "8, 4, " + b("5em") + " (8 + 4 = 12, 12 − 8 = 4)"},
            {"text": "5, " + b("5em") + ", 14 (5 + 9 = 14, 14 − 5 = 9)"},
        ]
    })
    
    # 5: Word problem
    s5 = build_subtraction_word_problems({
        "problems": [
            {"text": "Sarah has 13 stickers. She gives 5 to Tom. How many does she have left? 13 − 5 = " + b("5em")},
            {"text": "Write the addition fact that checks your answer: " + bl()},
        ]
    })
    
    body = f'''{sec(1, "What is a Fact Family?", s1, pb=True)}
{sec(2, "Complete Fact Families", s2)}
{sec(3, "Related Subtraction Facts", s3, pb=True)}
{sec(4, "Find the Missing Number", s4)}
{sec(5, "Word Problem", s5)}'''
    
    return page_multi(5, "Thursday", "Fact Families", body)


# ═══════════════════════════════════════════════════
# FRIDAY — Week 5 Review & Challenge
# ═══════════════════════════════════════════════════
def friday():
    # 1: Subtract from 10
    s1 = build_subtraction_facts({
        "facts": [(10, 4), (10, 7), (10, 2), (10, 5), (10, 8), (10, 3), (10, 6), (10, 9), (10, 1)],
        "note": "Quick facts: subtract from 10.",
        "cols": 3,
    })
    
    # 2: Subtract from 11-20
    s2 = build_subtraction_facts({
        "facts": [(13, 5), (14, 6), (15, 7), (16, 8), (17, 9), (18, 10), (12, 4), (11, 3), (19, 11)],
        "note": "Subtract from 11-20.",
        "cols": 3,
    })
    
    # 3: Solve any way
    s3 = build_subtraction_facts({
        "facts": [(15, 8), (16, 9), (17, 10), (18, 11), (19, 12), (14, 7), (13, 6), (12, 5), (11, 4)],
        "note": "Solve using any strategy.",
        "cols": 3,
    })
    
    # 4: Word problems (combined)
    s4 = build_subtraction_word_problems({
        "problems": [
            {"text": "Tom had 17 marbles. He lost 8. How many does he have left? 17 − 8 = " + b("5em")},
            {"text": "There were 19 apples. Sarah picked 12. How many are left on the tree? 19 − 12 = " + b("5em")},
        ]
    })
    
    # 5: Challenge
    s5 = build_challenge_box({
        "problems": [
            {"text": "<b>Challenge Questions</b>"},
            {"text": "1. 20 − 15 = " + b("5em")},
            {"text": "2. 18 − 9 = " + b("5em")},
            {"text": "3. What is the difference between 17 and 9? " + b("5em")},
            {"text": "4. Write a word problem: " + bl()},
        ]
    })
    
    body = f'''{sec(1, "Subtract from 10", s1, pb=True)}
{sec(2, "Subtract from 11-20", s2)}
{sec(3, "Solve Any Way", s3, pb=True)}
{sec(4, "Word Problems", s4)}
{sec(5, "Challenge Questions", s5)}'''
    
    return page_multi(5, "Friday", "Week 5 Review & Challenge", body)


# ═══════════════════════════════════════════════════
# TEACHER GUIDE
# ═══════════════════════════════════════════════════
def monday_tg():
    return tg_page_multi(5, "Monday", "Subtraction Facts — From 10",
        f'''{tg_section_title("Subtraction is the Opposite")}
{tg_note("3+7=10, 10-3=7, 10-7=3")}
{tg_grid_answers(1, "Subtract from 10", [7, 3, 4, 6, 1, 9, 8, 2])}
{tg_section_title("Use Addition to Help Subtract")}
{tg_note("5+5=10 so 10-5=5 | 4+6=10 so 10-4=6 | 8+2=10 so 10-8=2 | 2+8=10 so 10-2=8")}
''',
        f'''{tg_grid_answers(1, "Quick 10s Subtraction Drill", [5, 10, 4, 0, 7, 1, 8, 2, 3])}
{tg_grid_answers(1, "Missing Numbers", [10, 6, 10, 1, 10, 9])}
{tg_section_title("Challenge: Subtraction Chain")}
{tg_note("18, 16, 14, 12, 10")}'''
    )

def tuesday_tg():
    return tg_page_multi(5, "Tuesday", "Subtracting 11-20",
        f'''{tg_grid_answers(1, "Review: Subtract from 10", [6, 3, 8, 5, 2, 7, 4, 1, 9])}
{tg_section_title("Subtracting from 11-20")}
{tg_note("13-5: 13-3=10, 10-2=8")}
{tg_grid_answers(1, "Practice Problems", [8, 8, 8, 8, 8, 8, 8, 8])}
''',
        f'''{tg_section_title("Fill in the Steps")}
{tg_note("14-4=10, 10-2=8 | 15-5=10, 10-2=8 | 16-6=10, 10-2=8 | 17-7=10, 10-2=8")}
{tg_grid_answers(1, "More Practice", [9, 9, 9, 9, 9, 9, 9, 7, 5])}
{tg_section_title("Challenge: Explain Your Thinking")}
{tg_note("Accept any valid strategy")}'''
    )

def wednesday_tg():
    return tg_page_multi(5, "Wednesday", "Mixed Subtraction Practice",
        f'''{tg_grid_answers(1, "Quick Review", [5, 2, 7, 3, 6, 1, 8, 4, 9])}
{tg_grid_answers(1, "Subtract from 11-20", [7, 7, 7, 7, 7, 7, 7, 7])}
{tg_grid_answers(1, "Solve Any Way", [6, 8, 10, 12, 14, 6, 6, 6, 6])}
''',
        f'''{tg_section_title("Word Problem 1")}
{tg_note("15 − 7 = 8")}
{tg_section_title("Word Problem 2")}
{tg_note("18 − 9 = 9")}
{tg_section_title("Challenge Problems")}
{tg_note("7, 7, 8, accept any valid word problem")}'''
    )

def thursday_tg():
    return tg_page_multi(5, "Thursday", "Fact Families",
        f'''{tg_section_title("What is a Fact Family?")}
{tg_note("3+7=10, 7+3=10, 10-3=7, 10-7=3")}
{tg_grid_answers(1, "Complete Fact Families", [10, 6, 10, 5, 11, 8, 13, 7])}
{tg_grid_answers(1, "Related Subtraction Facts", [6, 8, 9, 8])}
''',
        f'''{tg_section_title("Find the Missing Number")}
{tg_note("5, 9, 12, 9")}
{tg_section_title("Word Problem")}
{tg_note("13 − 5 = 8, check: 8 + 5 = 13")}
{tg_section_title("Challenge: Create a Fact Family")}
{tg_note("Accept any valid fact family")}'''
    )

def friday_tg():
    return tg_page_multi(5, "Friday", "Week 5 Review & Challenge",
        f'''{tg_grid_answers(1, "Subtract from 10", [6, 3, 8, 5, 2, 7, 4, 1, 9])}
{tg_grid_answers(1, "Subtract from 11-20", [8, 8, 8, 8, 8, 8, 8, 8, 8])}
{tg_grid_answers(1, "Solve Any Way", [7, 7, 7, 7, 7, 7, 7, 7, 7])}
''',
        f'''{tg_section_title("Word Problem 1")}
{tg_note("17 − 8 = 9")}
{tg_section_title("Word Problem 2")}
{tg_note("19 − 12 = 7")}
{tg_section_title("Challenge Questions")}
{tg_note("5, 9, 8, accept any valid word problem")}'''
    )


def main():
    import glob
    for f in glob.glob(os.path.join(WEEK, "*.pdf")):
        os.remove(f)
    
    days = [
        ("Monday", "Subtraction from 10", monday, monday_tg),
        ("Tuesday", "Subtracting 11-20", tuesday, tuesday_tg),
        ("Wednesday", "Mixed Subtraction", wednesday, wednesday_tg),
        ("Thursday", "Fact Families", thursday, thursday_tg),
        ("Friday", "Week 5 Review", friday, friday_tg),
    ]
    generate_simple(WEEK, days)
    
    print("Week 5 generated: 10 PDFs")


if __name__ == "__main__":
    main()