#!/usr/bin/env python3
"""
Thursday Generator for Math Curriculum
"""
import os
import sys
sys.path.insert(0, os.path.dirname(__file__))
from math_helpers import CSS, page_multi, sec, b, bw, bl, BLUE, GREEN, GOLD
from weasyprint import HTML


def generate_thursday(week_dir: str, week_num: int, day_data: dict):
    topic = day_data.get("topic", "Thursday Practice")
    activities = day_data.get("activities", [])
    
    sections = []
    for i, activity in enumerate(activities, 1):
        activity_type = activity.get("type", "generic")
        activity_data = activity.get("data", {})
        title = activity_data.get("title", f"Activity {i}")
        section_html = build_activity_section(activity_type, activity_data)
        sections.append((i, title, section_html))
    
    mid = (len(sections) + 1) // 2
    
    page1 = ""
    for i, title, html in sections[:mid]:
        page1 += sec(i, title, html)
    
    page2 = ""
    for i, title, html in sections[mid:]:
        page2 += sec(i, title, html, pb=True)
    
    html_content = page_multi(week_num, "Thursday", topic, page1, page2)
    
    pdf_path = os.path.join(week_dir, f"Week_{week_num}_Thursday.pdf")
    HTML(string=html_content).write_pdf(pdf_path)
    print(f"[ok] Week {week_num} Thursday generated: {pdf_path}")
    
    return day_data.get("answers", [])


def build_activity_section(activity_type: str, data: dict) -> str:
    builders = {
        "word_problems": build_word_problems,
        "money_tens_ones": build_money_tens_ones,
        "group_into_tens": build_group_into_tens,
        "expanded_form_word_problems": build_expanded_form_word_problems,
        "shopping_table": build_shopping_table,
        "riddles": build_riddles,
        "generic": build_generic,
    }
    builder = builders.get(activity_type, build_generic)
    return builder(data)


def build_word_problems(data: dict) -> str:
    html = '<div class="wp">'
    for problem in data.get("problems", []):
        text = problem.get("text", "")
        blanks = problem.get("blanks", 1)
        blank_html = " ".join([b("4em") for _ in range(blanks)])
        html += f'<p>{text} {blank_html}</p>'
    html += '</div>'
    return html


def build_money_tens_ones(data: dict) -> str:
    html = '<p class="note">' + data.get("note", "Each $10 bill is a ten. Each $1 coin is a one.") + '</p>'
    html += '<div class="mgrid c2">'
    for item in data.get("items", []):
        text = item.get("text", "")
        html += f'<div class="abox"><p>{text}</p><p>= $' + b('4em') + '</p></div>'
    html += '</div>'
    return html


def build_group_into_tens(data: dict) -> str:
    html = '<p class="note">' + data.get("note", "How many tens and ones can you make?") + '</p>'
    html += '<div class="mgrid c2">'
    for item in data.get("items", []):
        text = item.get("text", "")
        html += f'<div class="abox"><p>{text} = ' + b('4em') + ' tens and ' + b('4em') + ' ones</p></div>'
    html += '</div>'
    return html


def build_expanded_form_word_problems(data: dict) -> str:
    html = '<p class="note">' + data.get("note", "Write the answer in expanded form.") + '</p>'
    html += '<div class="mgrid c2">'
    for item in data.get("items", []):
        text = item.get("text", "")
        html += f'<div class="abox"><p>{text}</p>'
        html += f'<p>{b("4em")} + {b("4em")} = {b("4em")}</p></div>'
    html += '</div>'
    return html


def build_shopping_table(data: dict) -> str:
    html = '<table class="tbl"><tr><th>Item</th><th>Price</th><th>Tens</th><th>Ones</th></tr>'
    for item, price in data.get("items", []):
        html += f'<tr><td>{item}</td><td>${price}</td><td>{b("4em")}</td><td>{b("4em")}</td></tr>'
    html += '</table>'
    return html


def build_riddles(data: dict) -> str:
    html = '<div class="starbox"><p><b>' + data.get("title", "Riddles") + '</b></p>'
    for riddle in data.get("riddles", []):
        html += f'<p>{riddle} → {b("4em")}</p>'
    html += '</div>'
    return html


def build_generic(data: dict) -> str:
    return data.get("html", "")
