#!/usr/bin/env python3
"""4. Sınıf İngilizce — Tüm Üniteler 30 Soruluk KGS Hazırlık Testi DOCX
   Cevaplar A/B/C/D arasında dağıtılmıştır."""

from docx import Document
from docx.shared import Inches, Pt, Cm, RGBColor
from docx.enum.text import WD_ALIGN_PARAGRAPH
import random
import os

OUT = "/root/Akademik/İngilizce/4. Sınıf/4_Sinif_Ingilizce_30_Soru_Test.docx"

# Sabit seed — tekrar üretildiğinde aynı dağılım
random.seed(42)

doc = Document()

# Page margins
for section in doc.sections:
    section.top_margin = Cm(2)
    section.bottom_margin = Cm(2)
    section.left_margin = Cm(2.5)
    section.right_margin = Cm(2.5)

style = doc.styles['Normal']
font = style.font
font.name = 'Calibri'
font.size = Pt(11)

# ── Header ──
h = doc.add_heading('4. Sınıf İngilizce — Tüm Üniteler KGS Hazırlık Testi', level=1)
h.alignment = WD_ALIGN_PARAGRAPH.CENTER

sub = doc.add_paragraph()
sub.alignment = WD_ALIGN_PARAGRAPH.CENTER
run = sub.add_run('30 Soru • 4 Seçenekli • Cevap Anahtarı Sonda')
run.font.size = Pt(10)
run.font.color.rgb = RGBColor(100, 100, 100)

doc.add_paragraph()
doc.add_paragraph()

# ── Questions (correct answer marked with 'correct') ──
# Her soruda 4 seçenek var, doğru olan 'correct' ile işaretli
questions = [
    # === PRESENT CONTINUOUS (Trik #1, #2) ===
    {
        "q": "Look! The cat ______ on the sofa now.",
        "options": [
            ("is sleeping", True),
            ("sleeps", False),
            ("sleep", False),
            ("are sleeping", False),
        ],
        "topic": "Present Continuous — 'Look!' + now",
        "trick": "'Look!' → Present Continuous zorunlu. 'sleeps' ❌ (Trik #2)"
    },
    {
        "q": "Listen! The students ______ a song in the classroom.",
        "options": [
            ("sing", False),
            ("are singing", True),
            ("sings", False),
            ("is singing", False),
        ],
        "topic": "Present Continuous — 'Listen!' + plural subject",
        "trick": "'Listen!' → PC. students = çoğul → are. 'is singing' ❌ (Trik #1, #2)"
    },
    # === PRESENT SIMPLE vs CONTINUOUS (Trik #3) ===
    {
        "q": "I ______ to school every day, but today I ______ by bus.",
        "options": [
            ("walk / am going", True),
            ("am walking / go", False),
            ("walks / am going", False),
            ("walking / goes", False),
        ],
        "topic": "Present Simple vs Present Continuous",
        "trick": "'every day' → Simple, 'today' → Continuous (Trik #3)"
    },
    {
        "q": "She ______ breakfast at 7 o'clock every morning.",
        "options": [
            ("have", False),
            ("is having", False),
            ("has", True),
            ("having", False),
        ],
        "topic": "Present Simple — he/she/it + V1+s",
        "trick": "'every morning' → Simple. 'she have' ❌ → 'she has' ✅ (Trik #3)"
    },
    # === TO BE vs DO/DOES (Trik #4) ===
    {
        "q": "______ you like playing football?",
        "options": [
            ("Are", False),
            ("Do", True),
            ("Is", False),
            ("Can", False),
        ],
        "topic": "do/does vs to be — eylem fiili",
        "trick": "'like' = eylem fiili → Do. 'Are you like' ❌ (Trik #4)"
    },
    {
        "q": "______ your mother a teacher?",
        "options": [
            ("Does", False),
            ("Do", False),
            ("Is", True),
            ("Can", False),
        ],
        "topic": "to be — sıfat/meslek bildirme",
        "trick": "'a teacher' = isim → Is. 'Does she a teacher' ❌ (Trik #4)"
    },
    # === WOULD / CAN (Trik #5) ===
    {
        "q": "A: Would you like some juice?\nB: Yes, ______.",
        "options": [
            ("I like juice.", False),
            ("I do.", False),
            ("please.", True),
            ("I can.", False),
        ],
        "topic": "Would you like — kibarca teklif",
        "trick": "'Would you like...?' → 'Yes, please.' (Trik #5)"
    },
    {
        "q": "Can I have ______ water, please?",
        "options": [
            ("some", True),
            ("a", False),
            ("any", False),
            ("many", False),
        ],
        "topic": "Can I have some...? — kibarca isteme",
        "trick": "İstekte 'some' kullanılır, 'any' değil (Trik #5)"
    },
    # === SUBJECT vs OBJECT PRONOUNS (Trik #6) ===
    {
        "q": "Look at ______! He is playing in the garden.",
        "options": [
            ("he", False),
            ("his", False),
            ("him", True),
            ("she", False),
        ],
        "topic": "Object pronouns — preposition'dan sonra",
        "trick": "'Look at' + object pronoun → him. 'Look at he' ❌ (Trik #6)"
    },
    {
        "q": "My sister loves music. ______ plays the piano every evening.",
        "options": [
            ("She", True),
            ("Her", False),
            ("Hers", False),
            ("She's", False),
        ],
        "topic": "Subject pronouns — özne konumunda",
        "trick": "Cümle başında özne → She. 'Her plays' ❌ (Trik #6)"
    },
    # === WH QUESTIONS (Trik #7) ===
    {
        "q": "______ is your birthday?",
        "options": [
            ("Where", False),
            ("When", True),
            ("What", False),
            ("Who", False),
        ],
        "topic": "WH questions — When (ne zaman)",
        "trick": "Tarih sorusu → When. 'What is your birthday' ❌ (Trik #7)"
    },
    {
        "q": "______ students are there in your class?",
        "options": [
            ("How", False),
            ("What", False),
            ("Who", False),
            ("How many", True),
        ],
        "topic": "WH questions — How many (kaç tane)",
        "trick": "Sayılabilen çoğul → How many (Trik #7)"
    },
    # === 'S KULLANIMI (Trik #8) ===
    {
        "q": "My friend ______ got a new bicycle.",
        "options": [
            ("is", False),
            ("has", True),
            ("have", False),
            ("are", False),
        ],
        "topic": "'s = has got (sahiplik)",
        "trick": "'s + got → has. 'is got' ❌ (Trik #8)"
    },
    {
        "q": "This is Ali. ______ car is very fast.",
        "options": [
            ("Ali", False),
            ("Ali's", True),
            ("Alis", False),
            ("Ali is", False),
        ],
        "topic": "Possessive 's — iyelik",
        "trick": "İyelik 's → Ali's. 'Ali car' ❌ (Trik #8)"
    },
    # === CAN + V1 (Trik #9) ===
    {
        "q": "My little brother can ______ very well.",
        "options": [
            ("swims", False),
            ("swimming", False),
            ("swim", True),
            ("to swim", False),
        ],
        "topic": "can + V1 (yalın fiil)",
        "trick": "can'den sonra fiil YALIN. 'can swims/swimming' ❌ (Trik #9)"
    },
    # === TO + V1 (Trik #9a) ===
    {
        "q": "I want to ______ a doctor when I grow up.",
        "options": [
            ("being", False),
            ("be", True),
            ("is", False),
            ("been", False),
        ],
        "topic": "to + V1 (infinitive — yalın fiil)",
        "trick": "to'dan sonra fiil YALIN. 'to being' ❌ (Trik #9a)"
    },
    # === HAVE GOT / HAS GOT (Trik #10) ===
    {
        "q": "She ______ got a beautiful garden.",
        "options": [
            ("have", False),
            ("has", True),
            ("is", False),
            ("are", False),
        ],
        "topic": "have got / has got — he/she/it",
        "trick": "She → has got. 'She have got' ❌ (Trik #10)"
    },
    # === HAVE/HAS (GOT'SIZ) + DOESN'T (Trik #10a) ===
    {
        "q": "Tom ______ have a car. He goes everywhere by bike.",
        "options": [
            ("don't", False),
            ("doesn't", True),
            ("isn't", False),
            ("hasn't", False),
        ],
        "topic": "don't/doesn't have — olumsuz",
        "trick": "He → doesn't have. 'doesn't has' ❌ → doesn't'tan sonra V1 (Trik #10a)"
    },
    # === LIKE/LOVE + VING (Trik #11) ===
    {
        "q": "My sister likes ______ pictures on weekends.",
        "options": [
            ("draw", False),
            ("drawing", True),
            ("draws", False),
            ("to drawing", False),
        ],
        "topic": "like + Ving (gerund)",
        "trick": "like + -ing. 'likes draw' ❌ (Trik #11)"
    },
    # === WANT TO + V1 (Trik #11a) ===
    {
        "q": "He ______ to play basketball with his friends.",
        "options": [
            ("wants", True),
            ("want", False),
            ("wanting", False),
            ("likes playing", False),
        ],
        "topic": "want to + V1 (want Ving ALMAZ)",
        "trick": "want → sadece to + V1. 'wants playing' ❌ (Trik #11a)"
    },
    # === THERE IS / THERE ARE (Trik #12) ===
    {
        "q": "There ______ three books on the desk.",
        "options": [
            ("is", False),
            ("are", True),
            ("am", False),
            ("be", False),
        ],
        "topic": "There is/are — çoğul isim",
        "trick": "three books = çoğul → are. 'There is three' ❌ (Trik #12)"
    },
    # === THERE IS/ARE + A LOT OF (Trik #12a) ===
    {
        "q": "There ______ a lot of water in the bottle.",
        "options": [
            ("is", True),
            ("are", False),
            ("am", False),
            ("have", False),
        ],
        "topic": "There is/are + a lot of + uncountable",
        "trick": "water = sayılamaz → is. 'a lot of'a değil isme bak! (Trik #12a)"
    },
    # === THIS/THAT/THESE/THOSE (Trik #13) ===
    {
        "q": "______ are my new shoes. Do you like them?",
        "options": [
            ("This", False),
            ("These", True),
            ("That", False),
            ("It", False),
        ],
        "topic": "this/that/these/those — çoğul yakın",
        "trick": "shoes = çoğul + yakın → These. 'This are' ❌ (Trik #13)"
    },
    # === PREPOSITIONS (Trik #14) ===
    {
        "q": "My birthday is ______ Saturday.",
        "options": [
            ("in", False),
            ("at", False),
            ("on", True),
            ("to", False),
        ],
        "topic": "Prepositions of time — gün",
        "trick": "gün → on. 'at Saturday' ❌ 'in Saturday' ❌ (Trik #14)"
    },
    # === POSSESSIVE ADJECTIVES (Trik #15) ===
    {
        "q": "This is my sister. ______ name is Ayşe.",
        "options": [
            ("Her", True),
            ("She", False),
            ("His", False),
            ("My", False),
        ],
        "topic": "Possessive adjectives — she → her",
        "trick": "İyelik sıfatı → Her. 'She name' ❌ (Trik #15)"
    },
    # === WANT TO / WANTS TO (Trik #16) ===
    {
        "q": "He ______ to visit his grandmother this weekend.",
        "options": [
            ("want", False),
            ("wants", True),
            ("wanting", False),
            ("is wanting", False),
        ],
        "topic": "want to / wants to — he/she/it",
        "trick": "He → wants to. 'He want to' ❌ (Trik #16)"
    },
    # === WANT + ISIM vs WANT TO + FIIL (Trik #16a) ===
    {
        "q": "I am thirsty. I want ______.",
        "options": [
            ("to water", False),
            ("water", True),
            ("drinking", False),
            ("to drinking", False),
        ],
        "topic": "want + isim (to YOK)",
        "trick": "İsim → to yok. 'want to water' ❌ (Trik #16a)"
    },
    # === ADVERBS OF FREQUENCY (Trik #18) ===
    {
        "q": "I ______ brush my teeth before bed.",
        "options": [
            ("always", True),
            ("go always", False),
            ("always am", False),
            ("am always", False),
        ],
        "topic": "Adverbs of frequency — fiilden önce",
        "trick": "always → fiilden ÖNCE. 'I go always' ❌ (Trik #18)"
    },
    # === CONJUNCTIONS (Trik #19) ===
    {
        "q": "I like dogs ______ I don't like cats.",
        "options": [
            ("and", False),
            ("or", False),
            ("but", True),
            ("because", False),
        ],
        "topic": "Conjunctions — but (zıtlık)",
        "trick": "zıtlık → but. 'and' = ekleme (Trik #19)"
    },
    # === DO/DOES SHORT ANSWERS (Trik #20) ===
    {
        "q": "A: Does your father like coffee?\nB: No, ______.",
        "options": [
            ("he don't.", False),
            ("he doesn't.", True),
            ("he doesn't like.", False),
            ("he isn't.", False),
        ],
        "topic": "do/does Yes-No — kısa cevap",
        "trick": "'No, he doesn't.' — 'doesn't like' ❌ (Trik #20)"
    },
]

# ── Shuffle options for each question ──
shuffled_questions = []
answer_distribution = {"A": 0, "B": 0, "C": 0, "D": 0}

for q in questions:
    opts = q["options"][:]
    random.shuffle(opts)

    correct_idx = None
    for i, (text, is_correct) in enumerate(opts):
        if is_correct:
            correct_idx = i
            break

    letters = ["A", "B", "C", "D"]
    correct_letter = letters[correct_idx]
    answer_distribution[correct_letter] += 1

    shuffled_questions.append({
        "q": q["q"],
        "a": opts[0][0],
        "b": opts[1][0],
        "c": opts[2][0],
        "d": opts[3][0],
        "answer": correct_letter,
        "topic": q["topic"],
        "trick": q["trick"],
    })

# ── Write DOCX ──
for i, q in enumerate(shuffled_questions, 1):
    p = doc.add_paragraph()
    run = p.add_run(f"Question {i}")
    run.bold = True
    run.font.size = Pt(11)

    p2 = doc.add_paragraph()
    p2.add_run(q["q"]).font.size = Pt(11)

    opts = [("A", q["a"]), ("B", q["b"]), ("C", q["c"]), ("D", q["d"])]
    for letter, text in opts:
        po = doc.add_paragraph()
        po.paragraph_format.left_indent = Cm(1)
        r = po.add_run(f"{letter}) {text}")
        r.font.size = Pt(11)

    doc.add_paragraph()

# ── Answer Key ──
doc.add_page_break()
ak = doc.add_heading('Cevap Anahtarı', level=2)
ak.alignment = WD_ALIGN_PARAGRAPH.CENTER

doc.add_paragraph()

for i, q in enumerate(shuffled_questions, 1):
    p = doc.add_paragraph()
    r = p.add_run(f"Q{i}: {q['answer']}")
    r.bold = True
    r.font.size = Pt(10)

    p2 = doc.add_paragraph()
    p2.paragraph_format.left_indent = Cm(1)
    correct_text = q[q['answer'].lower()]
    r2 = p2.add_run(f"Doğru cevap: {q['answer']}) {correct_text}")
    r2.font.size = Pt(9)

    p3 = doc.add_paragraph()
    p3.paragraph_format.left_indent = Cm(1)
    r3 = p3.add_run(f"Konu: {q['topic']}")
    r3.font.size = Pt(9)
    r3.font.color.rgb = RGBColor(100, 100, 100)

    p4 = doc.add_paragraph()
    p4.paragraph_format.left_indent = Cm(1)
    r4 = p4.add_run(f"⚡ Trik: {q['trick']}")
    r4.font.size = Pt(9)
    r4.font.color.rgb = RGBColor(180, 60, 20)

    doc.add_paragraph()

# ── Notes ──
doc.add_paragraph()
p = doc.add_paragraph()
r = p.add_run("📋 Kapsam Notu:")
r.bold = True
r.font.size = Pt(9)

notes = doc.add_paragraph()
notes.add_run(
    "• Sorular 4. Sınıf tüm üniteler (1-9) kapsamında hazırlanmıştır.\n"
    "• Öğrencilerin ileride göreceği konular KESİNLİKLE sorulmamıştır.\n"
    "• Çeldiriciler, 4. Sınıf Trik Noktalar dosyasındaki sık yapılan hatalara göre yerleştirilmiştir.\n"
    "• Zorluk seviyesi: A1\n"
    "• Her soru bir trik noktasını test eder — öğrencinin kavram yanılgısı ölçülür.\n"
    "• Kaynak: KKTC MEB Temel Eğitim İngilizce Dersi Öğretim Programı (4. Sınıf)"
).font.size = Pt(9)

doc.save(OUT)
print(f"✅ DOCX kaydedildi: {OUT}")
print(f"Toplam soru: {len(shuffled_questions)}")
print(f"Cevap dağılımı: {answer_distribution}")