#!/usr/bin/env python3
"""Convert exam markdown to PDF using fpdf2."""

from fpdf import FPDF
import os

class ExamPDF(FPDF):
    def header(self):
        pass
    def footer(self):
        self.set_y(-15)
        self.set_font('Helvetica', 'I', 8)
        self.cell(0, 10, f'Sayfa {self.page_no()}/{{nb}}', align='C')

pdf = ExamPDF()
pdf.alias_nb_pages()
pdf.set_auto_page_break(auto=True, margin=20)
pdf.add_page()

# Add a Unicode-capable font (DejaVu)
pdf.add_font('DejaVu', '', '/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', uni=True)
pdf.add_font('DejaVu', 'B', '/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf', uni=True)
pdf.add_font('DejaVuMono', '', '/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf', uni=True)

def section(text):
    pdf.ln(4)
    pdf.set_font('DejaVu', 'B', 13)
    pdf.set_text_color(26, 71, 138)
    pdf.cell(0, 8, text, new_x="LMARGIN", new_y="NEXT")
    pdf.ln(2)

def question(num, text, options, diyalog=False):
    # Check if we need a new page (estimate)
    if pdf.get_y() > 240:
        pdf.add_page()
    
    pdf.set_font('DejaVu', 'B', 10)
    pdf.set_text_color(0, 0, 0)
    
    # Question text - handle multi-line
    pdf.multi_cell(0, 5.5, f'{num}. {text}')
    pdf.ln(1)
    
    pdf.set_font('DejaVu', '', 10)
    for opt in options:
        pdf.set_x(15)
        pdf.cell(0, 5.5, opt, new_x="LMARGIN", new_y="NEXT")
    pdf.ln(3)

def body_text(text):
    pdf.set_font('DejaVu', '', 10)
    pdf.set_text_color(0, 0, 0)
    pdf.multi_cell(0, 5.5, text)
    pdf.ln(2)

def bold_text(text):
    pdf.set_font('DejaVu', 'B', 10)
    pdf.set_text_color(0, 0, 0)
    pdf.multi_cell(0, 5.5, text)
    pdf.ln(2)

# ==================== TITLE ====================
pdf.set_font('DejaVu', 'B', 16)
pdf.set_text_color(26, 71, 138)
pdf.cell(0, 10, '5. SINIF INGILIZCE', align='C', new_x="LMARGIN", new_y="NEXT")
pdf.set_font('DejaVu', 'B', 13)
pdf.cell(0, 8, 'UNITE 7: EATING OUT - DENEME SINAVI', align='C', new_x="LMARGIN", new_y="NEXT")
pdf.set_font('DejaVu', '', 9)
pdf.set_text_color(100, 100, 100)
pdf.cell(0, 6, '25 Soru | Sure: 40 Dakika | Kapsam: Unite 7 + Onceki Uniteler', align='C', new_x="LMARGIN", new_y="NEXT")
pdf.ln(8)

# ==================== BOLUM 1 ====================
pdf.set_draw_color(26, 71, 138)
pdf.line(10, pdf.get_y(), 200, pdf.get_y())
pdf.ln(4)
section('BOLUM 1: GRAMMAR & VOCABULARY (1-16)')

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

for q in questions_data:
    question(*q)

# ==================== BOLUM 2 ====================
pdf.add_page()
section('BOLUM 2: CLOZE TEST (17-20)')
pdf.set_font('DejaVu', '', 10)
pdf.set_text_color(80, 80, 80)
body_text('Asagidaki parcayi okuyunuz. Bosluklari uygun seceneklerle doldurunuz.')
pdf.set_text_color(0, 0, 0)

# Cloze passage in a box
pdf.set_fill_color(240, 244, 248)
pdf.set_draw_color(200, 210, 220)
y_start = pdf.get_y()
pdf.rect(12, y_start, 186, 25, style='D')
pdf.set_xy(15, y_start + 3)
pdf.set_font('DejaVu', '', 10)
pdf.multi_cell(180, 5.5, 
    '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.')
pdf.ln(6)

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

# ==================== BOLUM 3 ====================
pdf.add_page()
section('BOLUM 3: OKUMA-ANLAMA (21-25)')
pdf.set_font('DejaVu', '', 10)
pdf.set_text_color(80, 80, 80)
body_text('Asagidaki metni okuyunuz. Sorulari metne gore cevaplayiniz.')
pdf.set_text_color(0, 0, 0)
pdf.ln(2)

# Reading passage box
pdf.set_fill_color(240, 244, 248)
y_start2 = pdf.get_y()
reading_text = (
    '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!'
)
pdf.rect(12, y_start2, 186, 30, style='D')
pdf.set_xy(15, y_start2 + 3)
pdf.set_font('DejaVu', '', 10)
pdf.multi_cell(180, 5.5, reading_text)
pdf.ln(8)

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

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

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

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

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 ====================
pdf.add_page()
pdf.set_font('DejaVu', 'B', 14)
pdf.set_text_color(26, 71, 138)
pdf.cell(0, 10, 'CEVAP ANAHTARI', align='C', new_x="LMARGIN", new_y="NEXT")
pdf.ln(8)

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'),
]

col1 = answers[:13]
col2 = answers[13:]

pdf.set_font('DejaVu', 'B', 11)
pdf.set_text_color(0, 0, 0)
for (n1, a1), (n2, a2) in zip(col1, col2):
    pdf.cell(90, 8, f'Soru {n1}: {a1}', new_x="RIGHT", new_y="LAST")
    pdf.cell(90, 8, f'Soru {n2}: {a2}', new_x="LMARGIN", new_y="NEXT")

pdf.ln(10)
pdf.set_font('DejaVu', '', 11)
pdf.set_text_color(100, 100, 100)
pdf.cell(0, 8, 'Basarlar!', align='C', new_x="LMARGIN", new_y="NEXT")

# Save
output_path = "/root/Akademik/İngilizce/5. Sınıf/Unit7_Eating_Out_Deneme_Sinavi.pdf"
pdf.output(output_path)
print(f"PDF saved: {output_path}")
print(f"Size: {os.path.getsize(output_path)} bytes")
