#!/usr/bin/env python3
"""5. Sınıf Unit 1 "My Day" — TABLO formatında vocabulary testi.

Her satırda kelime çifti (English | Türkçe) bulunur; satırın RASTGELE bir tarafı
boş bırakılır, öğrenci doldurur. Kelime listesi gen_unit1_vocab_test.py'den gelir.

Çıktı:
  ws-grade5-unit1-vocabulary-table.spec.json   (öğrenci kağıdı)
  ws-grade5-unit1-vocabulary-table.docx
  ws-grade5-unit1-vocabulary-table-keys.spec.json  (cevap anahtarı)
  ws-grade5-unit1-vocabulary-table_cevaplar.docx
"""
import json
import random
from pathlib import Path

from gen_unit1_vocab_test import SECTIONS

DIR = Path(__file__).parent
SEED = 11
BLANK = ""            # boş hücre
HEADERS = ["#", "English", "Türkçe"]
WIDTHS = [1.2, 8.0, 8.0]


def build_specs():
    rng = random.Random(SEED)
    total = sum(len(w) for _, w in SECTIONS)

    # her satır için boş taraf: 0=ingilizce boş, 1=türkçe boş  (~50/50)
    sides = [0] * (total // 2) + [1] * (total - total // 2)
    rng.shuffle(sides)

    student_blocks, key_blocks = [], []
    n = 0
    sit = iter(sides)
    for si, (title, words) in enumerate(SECTIONS):
        student_blocks.append({"type": "section", "text": title})
        key_blocks.append({"type": "section", "text": title})

        srows, krows = [], []
        for eng, tr in words:
            n += 1
            side = next(sit)
            e_cell = BLANK if side == 0 else eng
            t_cell = BLANK if side == 1 else tr
            srows.append([str(n), e_cell, t_cell])
            krows.append([str(n), eng, tr])
        student_blocks.append({"type": "table", "headers": HEADERS, "rows": srows,
                               "widths": WIDTHS, "shade": "E4EBF6", "font_size": 10.5})
        key_blocks.append({"type": "table", "headers": HEADERS, "rows": krows,
                           "widths": WIDTHS, "shade": "E4EBF6", "font_size": 10.5})
        if si != len(SECTIONS) - 1:
            student_blocks.append({"type": "page_break"})

    meta = {
        "font": "Calibri", "base_size": 11, "paper": "A4", "orientation": "portrait",
        "margins": {"top": 1.4, "bottom": 1.4, "left": 1.9, "right": 1.9},
        "name_fields": True, "score_field": True,
        "header_left": "Serdarli Primary School", "header_right": "English — Grade 5",
        "footer_text": "Unit 1 'My Day' — Vocabulary (Table)",
        "answer_key": "none", "lang": "en",
        "accent": "1A559E", "callout_fill": "FDF3E3",
        "example_fill": "F0F6EF", "header_fill": "E4EBF6",
    }

    student = {
        "title": "Unit 1 — Vocabulary",
        "subtitle": ("Grade 5 — Unit 1 \"My Day\" | Fill in the missing English or "
                     f"Turkish word | {total} words"),
        "meta": dict(meta),
        "blocks": [
            {"type": "instructions",
             "text": "Her satırda bir taraf boştur. Boş olan yere doğru "
                     "İngilizce ya da Türkçe karşılığı yazın. (Bir taraf boş bırakılmıştır.)"},
        ] + student_blocks,
    }
    keys = {
        "title": "Unit 1 — Vocabulary (Answer Key)",
        "subtitle": f"Grade 5 — Unit 1 \"My Day\" | {total} words — full list",
        "meta": dict(meta),
        "blocks": key_blocks,
    }
    return student, keys, total


def main():
    student, keys, total = build_specs()
    (DIR / "ws-grade5-unit1-vocabulary-table.spec.json").write_text(
        json.dumps(student, ensure_ascii=False, indent=1), encoding="utf-8")
    (DIR / "ws-grade5-unit1-vocabulary-table-keys.spec.json").write_text(
        json.dumps(keys, ensure_ascii=False, indent=1), encoding="utf-8")
    print(f"spec yazildi — {total} kelime")
    for title, words in SECTIONS:
        print(f"  {title}: {len(words)}")


if __name__ == "__main__":
    main()
