#!/usr/bin/env python3
"""
Friday Generator - Word Parts & Meaning Phase (Show What You Know, 3 pages)

Usage:
    from friday_wp import generate_friday
    generate_friday(week_dir=..., week_num=..., topic_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")
    topic_label = kw["topic_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"]
 

    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>'

  
    safe_label = topic_label.replace(' ', '_')
    pdf_name = f"Week0{week_num}_{day_name}_{safe_label}.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}
    </div>

    </body></html>"""

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