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

# Templates directory
TEMPLATE_DIR = "templates/"
TEMPLATE_URL = "https://abzinnovation.com/email-signatures/templates"
IMAGES_URL = "https://abzinnovation.com/email-signatures/images"
HTML_TEMPLATE_PATH = "email_new.html"
HTML_TEMPLATE_PATH_NO_PHONE = "email_new_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 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)

    templates = {
        "template": template,
        "name_and_title": load_template("name_and_title.html"),
        "tel": load_template("phone.html"),
        "email": load_template("mail.html"),
        "abz_link": load_template("abzinnovation-com.html"),
    }

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

    for id, html_template in templates.items():
        for placeholder, value in placeholders.items():
            html_template = html_template.replace(placeholder, value)

        options = {
            'width': '400'
        }

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

        if id == "template":
            save_to_file(html_template, os.path.join(directory_path, f"{normalized_name}_template.html"))
        else:
            imgkit.from_string(html_template, os.path.join(directory_path, f"{normalized_name}_{id}.png"), options)


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 = "#282929"
            link_color = "#ec5118"

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

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

# Example usage
process_csv(CSV_FILE_PATH)

