#!/usr/bin/env python3
"""Convert the exam markdown to a proper Word document."""

from docx import Document
from docx.shared import Pt, Inches, Cm, RGBColor
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.enum.table import WD_TABLE_ALIGNMENT
import re

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)

# -- Styles
style = doc.styles['Normal']
font = style.font
font.name = 'Calibri'
font.size = Pt(11)
style.paragraph_format.space_after = Pt(4)
style.paragraph_format.line_spacing = 1.15

# -- Title
title = doc.add_paragraph()
title.alignment = WD_ALIGN_PARAGRAPH.CENTER
run = title.add_run('5. SINIF İNGİLİZCE')
run.bold = True
run.font.size = Pt(16)
run.font.color.rgb = RGBColor(0x1a, 0x47, 0x8a)

subtitle = doc.add_paragraph()
subtitle.alignment = WD_ALIGN_PARAGRAPH.CENTER
run = subtitle.add_run('ÜNİTE 7: EATING OUT - DENEME SINAVI')
run.bold = True
run.font.size = Pt(14)
run.font.color.rgb = RGBColor(0x2c, 0x5f, 0x9e)

info = doc.add_paragraph()
info.alignment = WD_ALIGN_PARAGRAPH.CENTER
run = info.add_run('25 Soru  |  Süre: 40 Dakika  |  Kapsam: Ünite 7 + Önceki Üniteler')
run.font.size = Pt(10)
run.font.color.rgb = RGBColor(0x66, 0x66, 0x66)

doc.add_paragraph()  # spacer

# -- Helper: add question
def add_question(num, question_text, options):
    p = doc.add_paragraph()
    run = p.add_run(f'{num}. {question_text}')
    run.bold = True
    run.font.size = Pt(11)
    
    for opt in options:
        p2 = doc.add_paragraph()
        p2.paragraph_format.left_indent = Cm(1)
        p2.paragraph_format.space_before = Pt(1)
        p2.paragraph_format.space_after = Pt(1)
        run = p2.add_run(opt)
        run.font.size = Pt(11)

# -- Helper: add section header
def add_section_header(text):
    doc.add_paragraph()  # spacer
    p = doc.add_paragraph()
    run = p.add_run(text)
    run.bold = True
    run.font.size = Pt(12)
    run.font.color.rgb = RGBColor(0x1a, 0x47, 0x8a)
    p.paragraph_format.space_before = Pt(8)
    p.paragraph_format.space_after = Pt(6)

# ========== BÖLÜM 1 ==========
add_section_header('BÖLÜM 1: GRAMMAR & VOCABULARY (1-16)')

add_question(1, 'There _______ some milk in the fridge.', 
    ['A) is', 'B) are', 'C) am', 'D) be'])
add_question(2, 'A: Would you like _______ orange juice?\nB: Yes, please.',
    ['A) some', 'B) any', 'C) a', 'D) many'])
add_question(3, "There aren't _______ eggs in the kitchen.",
    ['A) some', 'B) a', 'C) any', 'D) an'])
add_question(4, 'I usually have breakfast _______ 7:30 in the morning.',
    ['A) in', 'B) at', 'C) on', 'D) by'])
add_question(5, 'We need _______ sugar for the cake. Can you buy it?',
    ['A) a lot of', 'B) many', 'C) a few', 'D) an'])
add_question(6, 'A: _______ does the pizza taste?\nB: It tastes delicious!',
    ['A) What', 'B) How', 'C) Where', 'D) When'])
add_question(7, 'My mother _______ to the market every Saturday.',
    ['A) go', 'B) goes', 'C) going', 'D) gone'])
add_question(8, 'A: What would you like to eat?\nB: I want _______ spaghetti with meatballs.',
    ['A) any', 'B) some', 'C) a few', 'D) many'])
add_question(9, "I like ice cream _______ I don't like chocolate.",
    ['A) and', 'B) or', 'C) but', 'D) so'])
add_question(10, 'There _______ a lot of people in the restaurant.',
    ['A) is', 'B) am', 'C) are', 'D) be'])
add_question(11, 'A: How _______ eggs do we need?\nB: We need six eggs.',
    ['A) much', 'B) many', 'C) some', 'D) any'])
add_question(12, 'We _______ wash our hands before eating.',
    ['A) can', 'B) have to', 'C) are', 'D) like'])
add_question(13, "A: _______ I have a glass of water, please?\nB: Sure, here you are.",
    ['A) Am', 'B) Have', 'C) Can', 'D) Do'])
add_question(14, "I _______ like spicy food. It's too hot for me.",
    ["A) don't", "B) doesn't", "C) isn't", "D) aren't"])
add_question(15, 'She always _______ her breakfast at 8 o\'clock.',
    ['A) have', 'B) has', 'C) is having', 'D) had'])
add_question(16, "Waiters _______ wear uniforms at work.",
    ['A) have to', 'B) has to', 'C) having to', 'D) had to'])

# ========== BÖLÜM 2 ==========
add_section_header("BÖLÜM 2: CLOZE TEST (17-20)")

cloze_intro = doc.add_paragraph()
cloze_intro.paragraph_format.space_after = Pt(6)
run = cloze_intro.add_run("Aşağıdaki parçayı okuyunuz. Boşlukları uygun seçeneklerle doldurunuz.")
run.font.size = Pt(10)
run.italic = True

cloze_text = doc.add_paragraph()
cloze_text.paragraph_format.left_indent = Cm(0.5)
cloze_text.paragraph_format.space_after = Pt(8)
run = cloze_text.add_run(
    'Emily and her family are (17) _______ a restaurant. '
    'The restaurant is very nice. There (18) _______ many tables and chairs. '
    'Emily wants to eat pasta (19) _______ her mother wants to eat salad. '
    'They also order (20) _______ orange juice.'
)
run.font.size = Pt(11)
run.italic = True

add_question(17, '',
    ['A) in', 'B) on', 'C) at', 'D) under'])
add_question(18, '',
    ['A) is', 'B) are', 'C) am', 'D) be'])
add_question(19, '',
    ['A) but', 'B) or', 'C) and', 'D) because'])
add_question(20, '',
    ['A) any', 'B) a few', 'C) some', 'D) many'])

# ========== BÖLÜM 3 ==========
add_section_header("BÖLÜM 3: OKUMA-ANLAMA (21-25)")

read_intro = doc.add_paragraph()
read_intro.paragraph_format.space_after = Pt(4)
run = read_intro.add_run("Aşağıdaki metni okuyunuz. Soruları metne göre cevaplayınız.")
run.font.size = Pt(10)
run.italic = True

# Reading passage in a box (using a table with 1 cell)
table = doc.add_table(rows=1, cols=1)
table.alignment = WD_TABLE_ALIGNMENT.CENTER
cell = table.cell(0, 0)
cell.text = ""
p = cell.paragraphs[0]
p.paragraph_format.left_indent = Cm(0.3)
p.paragraph_format.right_indent = Cm(0.3)
run = p.add_run(
    'Hello! My name is Karen. I am 11 years old. I live in Kyrenia with my family. '
    'I like eating healthy food. For breakfast, I usually eat cereal with milk and '
    'drink a glass of orange juice. I don\'t like eggs. For lunch, I have soup or '
    'salad. My favourite food is pizza but I eat it only on Sundays. My mother always '
    'cooks delicious meals. She uses a lot of vegetables because she wants us to be '
    'healthy. My father likes cooking, too. He makes the best spaghetti in the world!'
)
run.font.size = Pt(11)
run.italic = True

# Set table shading
from docx.oxml.ns import qn
shading = cell._element.get_or_add_tcPr()
shading_elm = shading.makeelement(qn('w:shd'), {
    qn('w:val'): 'clear',
    qn('w:color'): 'auto',
    qn('w:fill'): 'F0F4F8',
})
shading.append(shading_elm)

doc.add_paragraph()  # spacer

add_question(21, 'What does Karen usually have for breakfast?',
    ['A) Pizza and orange juice',
     'B) Cereal with milk and orange juice',
     'C) Eggs and soup',
     'D) Spaghetti and salad'])

add_question(22, "What is Karen's favourite food?",
    ['A) Spaghetti', 'B) Salad', 'C) Pizza', 'D) Soup'])

add_question(23, "Why does Karen's mother use a lot of vegetables?",
    ["A) Because she doesn't like meat",
     "B) Because Karen doesn't like eggs",
     'C) Because she wants them to be healthy',
     'D) Because vegetables are cheap'])

add_question(24, 'When does Karen eat pizza?',
    ['A) Every day', 'B) On Saturdays',
     'C) Only on Sundays', 'D) For breakfast'])

add_question(25, 'Who makes the best spaghetti in the world according to Karen?',
    ['A) Her mother', 'B) Her father',
     'C) Karen herself', 'D) The waiter'])

# ========== ANSWER KEY ==========
doc.add_page_break()
p = doc.add_paragraph()
p.alignment = WD_ALIGN_PARAGRAPH.CENTER
run = p.add_run('CEVAP ANAHTARI')
run.bold = True
run.font.size = Pt(14)
run.font.color.rgb = RGBColor(0x1a, 0x47, 0x8a)

doc.add_paragraph()

answers = [
    ('1', 'A'), ('2', 'A'), ('3', 'C'), ('4', 'B'), ('5', 'A'),
    ('6', 'B'), ('7', 'B'), ('8', 'B'), ('9', 'C'), ('10', 'C'),
    ('11', 'B'), ('12', 'B'), ('13', 'C'), ('14', 'A'), ('15', 'B'),
    ('16', 'A'), ('17', 'C'), ('18', 'B'), ('19', 'A'), ('20', 'C'),
    ('21', 'B'), ('22', 'C'), ('23', 'C'), ('24', 'C'), ('25', 'B'),
]

# Create two columns for answer key
col1 = answers[:13]
col2 = answers[13:]

for (n1, a1), (n2, a2) in zip(col1, col2 + [('', '')]):
    p = doc.add_paragraph()
    p.paragraph_format.space_after = Pt(2)
    
    run = p.add_run(f'Soru {n1}: {a1}')
    run.font.size = Pt(11)
    run.bold = True
    
    run = p.add_run(f'        Soru {n2}: {a2}')
    run.font.size = Pt(11)
    run.bold = True

doc.add_paragraph()
p = doc.add_paragraph()
p.alignment = WD_ALIGN_PARAGRAPH.CENTER
run = p.add_run('Başarılar!')
run.font.size = Pt(12)
run.font.color.rgb = RGBColor(0x66, 0x66, 0x66)

# Save
output_path = "/root/Akademik/İngilizce/5. Sınıf/Unit7_Eating_Out_Deneme_Sinavi.docx"
doc.save(output_path)
print(f"Saved: {output_path}")
