#!/usr/bin/env python3
"""
Friday Generator - Show What You Know (3 pages)

Usage:
    from friday import generate_friday
    generate_friday(week_dir=..., week_num=..., phonics_label=..., words=..., ...)
"""
import os, sys, random
sys.path.insert(0, os.path.dirname(__file__))
from helpers import CSS, header, sectitle, secnote, write_line, page_break
from weasyprint import HTML


def generate_friday(**kw):
    week_dir = kw["week_dir"]
    week_num = kw["week_num"]
    day_name = kw.get("day_name", "Friday")
    phonics_label = kw["phonics_label"]
    words = kw["words"]
    review_story = kw["review_story"]
    story_questions = kw["story_questions"]
    opposite_words = kw["opposite_words"]
    builder_words = kw["builder_words"]
    journal_words = kw["journal_words"]
    celebration = kw.get("celebration",
        'Great Job This Week! You practiced your spelling words all week. Keep up the amazing work!')
    # NEW: Vocabulary / context clue support
    guess_meaning = kw.get("guess_meaning", [])  # [(sentence_with_blank, word), ...]
    paragraph_parts_review = kw.get("paragraph_parts_review", "")  # paragraph text for labeling

    day_dir = os.path.join(week_dir, "Friday")
    os.makedirs(day_dir, exist_ok=True)
    WORD_NAMES = [w[0] for w in words]

    # Spelling test
    test_html = ""
    for i, (word, *_) in enumerate(words, 1):
        test_html += f'<div class="test-item"><span class="num">{i}.</span><div style="flex:1;"><div style="margin-bottom:6px;"></div>{write_line()}</div></div>'

    # Story questions
    sq_html = "".join(f'<div style="margin-bottom:8px;"><strong style="font-size:12px;">{q}</strong><br>{write_line()}</div>' for q in story_questions)

    # Opposites
    opp_html = ""
    for w in opposite_words:
        opp_html += f'<div style="margin:4px 0;font-size:12px;"><strong style="color:#673ab7;font-size:12px;">{w}</strong><br>{write_line()}</div>'

    # Sentence Builder
    builder_html = ""
    for w in builder_words:
        builder_html += f'<div style="margin:6px 0;"><span style="font-size:11px;font-weight:bold;color:#26A69A;">{w}:</span>{write_line()}</div>'

    # Word Journal
    journal_html = ""
    for w in journal_words:
        journal_html += f'<div style="margin:8px 0;padding:6px 8px;background:#FAFAFA;border:1.5px solid #B2DFDB;border-radius:6px;"><div style="font-weight:bold;color:#26A69A;font-size:12px;margin-bottom:4px;">{w}</div><div style="font-size:10.5px;color:#888;">Meaning: _______________</div>{write_line()}</div>'

    celebration_box = (
        '<div style="text-align:center;margin:16px 0;padding:12px;background:#FFF8E1;border:2px solid #FFD54F;border-radius:8px;">'
        f'<div style="font-size:16px;font-weight:bold;color:#FF7043;">Great Job This Week!</div>'
        f'<div style="font-size:12px;color:#555;margin-top:4px;">{celebration}</div>'
        '</div>'
    )

    # Guess the Meaning — context clue quiz (NEW)
    guess_section = ""
    if guess_meaning:
        guess_html = ""
        for sentence, word in guess_meaning:
            guess_html += f'<div style="margin:6px 0;padding:6px 8px;background:#E8F5E9;border:1.5px solid #A5D6A7;border-radius:6px;"><div style="font-size:11px;margin-bottom:4px;">{sentence}</div><div style="font-size:11px;">What does <strong>{word}</strong> mean? _______________</div></div>'
        guess_section = f"""{sectitle("Guess the Meaning", "coral")}
    {secnote("Read each sentence. Use context clues to guess the meaning of the bold word.")}
    {guess_html}"""

    # Paragraph Parts Review — identify T/D/C (NEW)
    para_review_section = ""
    if paragraph_parts_review:
        para_review_section = f"""{sectitle("Paragraph Parts Review", "coral")}
    {secnote("Read the paragraph below. Identify the topic sentence (T), detail sentences (D), and conclusion (C).")}\n""" + \
                f'<div style="background:#E3F2FD;border:1.5px solid #90CAF9;border-radius:6px;padding:10px;margin:8px 0;">\n' + \
                f'<div style="font-size:12px;line-height:1.6;">{paragraph_parts_review}</div>\n' + \
                f'</div>\n' + \
                f'<div style="font-size:11px;margin-top:8px;">Topic sentence: _______________  Detail sentences: _______________  Conclusion: _______________</div>'

    pdf_name = f"Week0{week_num}_{day_name}_{phonics_label.replace(' ', '_')}.pdf"

    html = f"""<!DOCTYPE html>
    <html><head><meta charset="utf-8"><style>{CSS}</style></head><body>

    <div>
    {header("English Language Arts", f"Week {week_num}, {day_name} -- Show What You Know")}
    {sectitle("Spelling Test")}
    {secnote("Listen as your partner says each word, a sentence, then the word again. Write it correctly.")}
    {test_html}
    </div>
    {page_break()}

    <div>
    {sectitle("Review Reading")}
    {secnote("Read the story. Circle each spelling word you find.")}
    <div class="story-box">{review_story}</div>
    {sectitle("Story Questions")}
    {secnote("Answer each question in a complete sentence.")}
    {sq_html}
    {sectitle("Opposite Pairs")}
    {secnote("Write the opposite of each word.")}
    {opp_html}
    </div>
    {page_break()}

    <div>
    {sectitle("Sentence Builder")}
    {secnote("Write a complete sentence using each word.")}
    {builder_html}
    {sectitle("Show What You Know")}
    {secnote("Write the meaning of each word and copy it neatly.")}
    {journal_html}
    {guess_section}
    {para_review_section}
    {celebration_box}
    </div>

    </body></html>"""

    HTML(string=html).write_pdf(os.path.join(day_dir, pdf_name))
    print(f"[ok] Week {week_num} Friday generated")