import os
import csv
from PIL import Image, ImageDraw, ImageFont
import unicodedata

# Templates directory
TEMPLATE_DIR = "templates/"
HTML_TEMPLATE_PATH = "email_signature_template.html"
HTML_TEMPLATE_PATH_NO_PHONE = "email_signature_template_no_phone.html"
CSV_FILE_PATH = "input_data.csv"

def normalize_name(name):
    normalized_name = unicodedata.normalize('NFKD', name).encode('ASCII', 'ignore').decode('utf-8')
    return normalized_name.replace(" ", "_").lower()

def normalize_phone_number(phone_number):
    return phone_number.replace('+', '00').replace(' ', '')

def create_directory(directory_path):
    if not os.path.exists(directory_path):
        os.makedirs(directory_path)

def load_template(template_path):
    with open(template_path, "r", encoding="utf-8") as template_file:
        return template_file.read()

def save_to_file(content, file_path):
    with open(file_path, "w", encoding="utf-8") as file:
        file.write(content)

def add_text_to_image(image_path, text, position, font_path, font_size, text_color, output_filename):
    image = Image.open(image_path).convert("RGBA")
    draw = ImageDraw.Draw(image)
    font = ImageFont.truetype(font_path, font_size)
    draw.text(position, text, font=font, fill=text_color)
    image.save(output_filename, "PNG")

def generate_email_signature_template(name, title, phone, email, normalized_name):
    if phone == "":
        template = load_template(HTML_TEMPLATE_PATH_NO_PHONE)
    else:
        template = load_template(HTML_TEMPLATE_PATH)

    placeholders = {
        '@@name@@': name,
        '@@title@@': title,
        '@@normalized_name@@': normalized_name,
        '@@tel_normal@@': phone,
        '@@email@@': email,
        '@@tel_normalized@@': normalize_phone_number(phone)
    }

    for placeholder, value in placeholders.items():
        template = template.replace(placeholder, value)

    directory_path = os.path.join(TEMPLATE_DIR, normalized_name)
    create_directory(directory_path)

    save_to_file(template, os.path.join(directory_path, f"{normalized_name}_template.html"))

def process_csv(file_path):
    with open(file_path, 'r', newline='', encoding='utf-8') as csv_file:
        reader = csv.DictReader(csv_file)
        for row in reader:
            name = row['name']
            title = row['title']
            email = row['email']
            phone = row['phone']

            normalized_name = normalize_name(name)
            text_color = "#828282"

            create_directory(os.path.join(TEMPLATE_DIR, normalized_name))

            add_text_to_image("name_and_title.png", f"{name}", (50, 40), "Saira-Bold.ttf", 37, text_color, os.path.join(TEMPLATE_DIR, normalized_name, f"{normalized_name}_name_and_title.png"))
            add_text_to_image(os.path.join(TEMPLATE_DIR, normalized_name, f"{normalized_name}_name_and_title.png"), title, (50, 110), "Saira-Light.ttf", 30, text_color, os.path.join(TEMPLATE_DIR, normalized_name, f"{normalized_name}_name_and_title.png"))
            add_text_to_image("phone.png", phone, (115, 25), "Saira-Regular.ttf", 25, text_color, os.path.join(TEMPLATE_DIR, normalized_name, f"{normalized_name}_tel.png"))
            add_text_to_image("email.png", email, (115, 15), "Saira-Regular.ttf", 25, text_color, os.path.join(TEMPLATE_DIR, normalized_name, f"{normalized_name}_email.png"))

            generate_email_signature_template(name, title, phone, email, normalized_name)

# Example usage
process_csv(CSV_FILE_PATH)

