#!/usr/bin/env python3
"""Part 1: Grammar + Vocabulary — compact tables"""
from docx import Document
from docx.shared import Pt, Cm, RGBColor
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.oxml.ns import qn

PATH = "/root/Akademik/İngilizce/5. Sınıf/Unit1_My_Day.docx"
doc = Document()

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)

FONT = "Niramit"
BLUE = RGBColor(0x1a, 0x55, 0x9e)
DARK = RGBColor(0x22, 0x22, 0x22)
GRAY = RGBColor(0x66, 0x66, 0x66)

def run(p, text, size=11, bold=False, color=DARK, italic=False):
    r = p.add_run(text)
    r.font.name = FONT; r.font.size = Pt(size)
    r.bold = bold; r.font.color.rgb = color; r.italic = italic
    return r

def title(text, sz=18):
    p = doc.add_paragraph()
    p.alignment = WD_ALIGN_PARAGRAPH.CENTER
    run(p, text, sz, True, BLUE)

def subtitle(text, sz=10):
    p = doc.add_paragraph()
    p.alignment = WD_ALIGN_PARAGRAPH.CENTER
    run(p, text, sz, False, GRAY)

def heading(text, sz=13):
    p = doc.add_paragraph()
    p.paragraph_format.space_before = Pt(12)
    run(p, text, sz, True, BLUE)

def subhead(text):
    p = doc.add_paragraph()
    p.paragraph_format.space_before = Pt(10)
    run(p, text, 11, True, RGBColor(0x33, 0x6d, 0x5c))

def body(text):
    p = doc.add_paragraph()
    p.paragraph_format.space_after = Pt(4)
    run(p, text, 10.5, color=DARK)

def tip(text):
    p = doc.add_paragraph()
    p.paragraph_format.left_indent = Cm(0.5)
    p.paragraph_format.space_after = Pt(1)
    p.paragraph_format.space_before = Pt(0)
    run(p, text, 10, False, GRAY)

def add_table(headers, rows):
    """Create a single compact table with all rows"""
    table = doc.add_table(rows=1 + len(rows), cols=len(headers))
    table.style = 'Table Grid'
    # Remove default spacing in all cells
    table.autofit = True
    
    # Header
    for i, h in enumerate(headers):
        cell = table.cell(0, i)
        cell.text = ""
        p = cell.paragraphs[0]
        p.alignment = WD_ALIGN_PARAGRAPH.CENTER
        p.paragraph_format.space_before = Pt(2)
        p.paragraph_format.space_after = Pt(2)
        r = p.add_run(h)
        r.font.name = FONT; r.font.size = Pt(10); r.bold = True
        r.font.color.rgb = RGBColor(0xff, 0xff, 0xff)
        shading = cell._element.get_or_add_tcPr()
        shd = shading.makeelement(qn('w:shd'), {qn('w:val'): 'clear', qn('w:color'): 'auto', qn('w:fill'): '1a559e'})
        shading.append(shd)
    
    # Data rows
    for ri, row in enumerate(rows):
        for ci, val in enumerate(row):
            cell = table.cell(ri + 1, ci)
            cell.text = ""
            p = cell.paragraphs[0]
            p.alignment = WD_ALIGN_PARAGRAPH.CENTER if ci < len(headers)-1 else WD_ALIGN_PARAGRAPH.LEFT
            p.paragraph_format.space_before = Pt(1)
            p.paragraph_format.space_after = Pt(1)
            r = p.add_run(val)
            r.font.name = FONT; r.font.size = Pt(9.5)
    
    doc.add_paragraph()  # small gap after table

# ================================================================
# COVER
# ================================================================
title("5. SINIF İNGİLİZCE", 18)
subtitle("KKTC KGS Hazırlık · Temel Eğitim İngilizce Öğretim Programı")
doc.add_paragraph()
title("ÜNİTE 1: MY DAY — DERS KİTABI", 16)
subtitle("Grammar · Vocabulary · 3 Deneme · Okuma Parçaları · Cevap Anahtarı")
doc.add_paragraph()
doc.add_paragraph()

# ================================================================
# BÖLÜM 1: GRAMMAR
# ================================================================
heading("BÖLÜM 1: GRAMMAR", 14)
body("Bu bölümde Ünite 1'in üç temel grammar konusunu öğreneceksiniz.")

# --- 1.1 Present Simple "to be" ---
subhead("1.1  Present Simple — \"to be\" (am / is / are)")
body("\"to be\" fiili Türkçedeki \"olmak\" fiilidir. İsim, meslek, yer, yaş ve sıfat belirtirken kullanılır.")
add_table(
    ["Özne", "to be", "Kısaltma", "Örnek"],
    [
        ["I", "am", "I'm", "I am a student."],
        ["You", "are", "You're", "You are 10 years old."],
        ["He", "is", "He's", "He is my brother."],
        ["She", "is", "She's", "She is a teacher."],
        ["It", "is", "It's", "It is a cat."],
        ["We", "are", "We're", "We are friends."],
        ["They", "are", "They're", "They are at school."],
    ]
)

subhead("Olumsuz Cümle (Negative)")
body("Olumsuz yapmak için \"not\" eklenir:")
add_table(
    ["Olumlu", "Olumsuz", "Kısaltma"],
    [
        ["I am", "I am not", "I'm not"],
        ["You are", "You are not", "You aren't"],
        ["He is", "He is not", "He isn't"],
        ["She is", "She is not", "She isn't"],
        ["It is", "It is not", "It isn't"],
        ["We are", "We are not", "We aren't"],
        ["They are", "They are not", "They aren't"],
    ]
)

subhead("Soru Cümlesi (Interrogative)")
body("Soru yaparken am/is/are öznenin başına gelir:")
add_table(
    ["Soru", "Kısa Cevap (+)", "Kısa Cevap (−)"],
    [
        ["Am I late?", "Yes, I am.", "No, I am not."],
        ["Are you ready?", "Yes, you are.", "No, you aren't."],
        ["Is he at home?", "Yes, he is.", "No, he isn't."],
        ["Is she happy?", "Yes, she is.", "No, she isn't."],
        ["Is it a dog?", "Yes, it is.", "No, it isn't."],
        ["Are we late?", "Yes, we are.", "No, we aren't."],
        ["Are they here?", "Yes, they are.", "No, they aren't."],
    ]
)

subhead("Wh- Soruları")
body("What, Where, When, Who, How ile soru yapımı:")
tip("  What is your name?       — Adın ne?")
tip("  Where are you?           — Neredesin?")
tip("  When is the lesson?      — Ders ne zaman?")
tip("  Who is he?               — O kim?")
tip("  How old are you?         — Kaç yaşındasın?")
tip("  What time is it?         — Saat kaç?")

# --- 1.2 Object Pronouns ---
doc.add_page_break()
subhead("1.2  Object Pronouns (Nesne Zamirleri)")
body("Object pronouns, fiilden ya da edattan (in / at / to / for / look at) sonra gelir.")
body("Cümlede nesne görevi görürler.")
add_table(
    ["Subject", "Object", "Türkçe", "Örnek"],
    [
        ["I", "me", "beni/bana", "He sees me."],
        ["You", "you", "seni/sana", "I love you."],
        ["He", "him", "onu/ona (erkek)", "Look at him."],
        ["She", "her", "onu/ona (kadın)", "I know her."],
        ["It", "it", "onu/ona (cansız)", "Give it to me."],
        ["We", "us", "bizi/bize", "They call us."],
        ["They", "them", "onları/onlara", "I play with them."],
    ]
)

subhead("Önemli Kurallar")
tip("• Fiilden sonra object pronoun gelir:")
tip("     I love her. ✅  |  I love she. ❌")
tip("• Edattan (at, to, for, with, before, after) sonra object pronoun gelir:")
tip("     Look at him. ✅  |  Look at he. ❌")
tip("     Give it to me. ✅  |  Give it to I. ❌")
tip("• \"Give something to someone\" yapısı:")
tip("     give it to me / give them to her / give the book to him")

subhead("Sık Yapılan Hatalar")
tip("  I love she.        ❌  →  I love her.          ✅")
tip("  Look at he.        ❌  →  Look at him.          ✅")
tip("  Give it to they.   ❌  →  Give it to them.       ✅")
tip("  He sees I.         ❌  →  He sees me.            ✅")

# --- 1.3 Prepositions of Time ---
doc.add_page_break()
subhead("1.3  Prepositions of Time (Zaman Edatları)")
body("Zaman belirten edatlar bir olayın ne zaman olduğunu anlatır.")
add_table(
    ["Edat", "Kullanım", "Örnek"],
    [
        ["at", "saatlerde", "at 7 o'clock"],
        ["at", "bazı kalıplarda", "at night, at noon, at midnight"],
        ["in", "günün bölümleri", "in the morning, in the afternoon, in the evening"],
        ["on", "günler", "on Monday, on Sunday"],
        ["before", "önce", "before school, before breakfast"],
        ["after", "sonra", "after school, after dinner"],
    ]
)

subhead("before / after Detaylı")
body("before + isim:")
tip("  before school, before breakfast, before dinner")
tip("  before the lesson, before lunch")
doc.add_paragraph()
body("before + cümle (fiil gelir, özne şart!):")
tip("  before I go to bed       — yatmadan önce")
tip("  before he comes home     — o eve gelmeden önce")
tip("  before we have dinner    — akşam yemeğinden önce")
doc.add_paragraph()
body("after + isim:")
tip("  after school, after breakfast, after lunch")
tip("  after the party, after the lesson")
doc.add_paragraph()
body("after + cümle:")
tip("  after I wake up          — uyandıktan sonra")
tip("  after she finishes       — o bitirdikten sonra")
tip("  after they come          — onlar geldikten sonra")
doc.add_paragraph()
subhead("⚠️ Dikkat!")
tip("  before/after'dan sonra cümle gelirse, o cümlenin kendi öznesi olmalıdır!")
tip("  before go to bed       ❌  →  before I go to bed       ✅")
tip("  after eat lunch        ❌  →  after we eat lunch       ✅")

# ================================================================
# BÖLÜM 2: VOCABULARY
# ================================================================
doc.add_page_break()
heading("BÖLÜM 2: VOCABULARY", 14)

subhead("2.1  Daily Routines (Günlük Rutinler)")
routines = [
    ("wake up", "uyanmak"), ("get up", "yataktan kalkmak"),
    ("have breakfast", "kahvaltı yapmak"), ("have lunch", "öğle yemeği yemek"),
    ("have dinner", "akşam yemeği yemek"), ("brush teeth", "diş fırçalamak"),
    ("wash face", "yüz yıkamak"), ("wash hands", "el yıkamak"),
    ("get dressed", "giyinmek"), ("go to school", "okula gitmek"),
    ("go to bed", "yatağa gitmek"), ("come home", "eve gelmek"),
    ("do homework", "ödev yapmak"), ("watch TV", "TV izlemek"),
    ("take a shower", "duş almak"), ("take a bath", "banyo yapmak"),
    ("read a book", "kitap okumak"), ("listen to music", "müzik dinlemek"),
    ("play with friends", "arkadaşlarla oynamak"), ("go to sleep", "uyumaya gitmek"),
    ("have a rest", "dinlenmek"), ("feed the pet", "evcil hayvanı beslemek"),
    ("tidy the room", "odayı toplamak"), ("comb hair", "saç taramak"),
    ("leave home", "evden çıkmak"), ("start school", "okul başlamak"),
    ("finish school", "okul bitmek"), ("do exercise", "egzersiz yapmak"),
]
for eng, trk in routines:
    tip(f"  {eng:<25} →  {trk}")

doc.add_paragraph()
subhead("2.2  Times (Saatler)")
times = [
    ("o'clock", "saat (tam)"), ("half past", "buçuk"),
    ("quarter past", "çeyrek geçe"), ("quarter to", "çeyrek kala"),
    ("a.m.", "öğleden önce"), ("p.m.", "öğleden sonra"),
    ("noon / midday", "öğle"), ("midnight", "gece yarısı"),
    ("morning", "sabah"), ("afternoon", "öğleden sonra"),
    ("evening", "akşam"), ("night", "gece"),
    ("early", "erken"), ("late", "geç"),
]
for eng, trk in times:
    tip(f"  {eng:<25} →  {trk}")

doc.add_paragraph()
subhead("2.3  Body Parts (Vücut Bölümleri)")
body_parts = [
    ("head", "baş"), ("hair", "saç"), ("face", "yüz"),
    ("eyes", "gözler"), ("ears", "kulaklar"), ("nose", "burun"),
    ("mouth", "ağız"), ("teeth", "dişler"), ("tongue", "dil"),
    ("neck", "boyun"), ("shoulders", "omuzlar"), ("arms", "kollar"),
    ("hands", "eller"), ("fingers", "parmaklar"), ("knees", "dizler"),
    ("legs", "bacaklar"), ("feet", "ayaklar"), ("toes", "ayak parmakları"),
    ("back", "sırt"), ("stomach", "karın / mide"),
]
for eng, trk in body_parts:
    tip(f"  {eng:<25} →  {trk}")

total = len(routines) + len(times) + len(body_parts)
doc.add_paragraph()
body(f"Toplam: {len(routines)} günlük rutin + {len(times)} zaman + {len(body_parts)} vücut bölümü = {total} kelime")

doc.save(PATH)
print(f"✅ Part 1 (compact tables): {PATH}")
print(f"   {total} kelime, 4 tablo (bütünleşik)")
