import os
import subprocess
import base64

ASSETS = [
    ("/Users/ogt/momo_pro_system/web/static/images/logo.png", "WOOO_Main_Logo"),
    ("/Users/ogt/momo_pro_system/web/static/images/logo_v4_glass.png", "WOOO_Glass_Logo"),
    ("/Users/ogt/momo_pro_system/web/static/images/logo_v4_gradient.png", "WOOO_Gradient_Logo"),
]
OUTPUT_DIR = "/Users/ogt/momo_pro_system/export_assets"
os.makedirs(OUTPUT_DIR, exist_ok=True)

for input_path, base_name in ASSETS:
    print(f"Processing {base_name}...")
    try:
        # TIFF (Keep transparency)
        subprocess.run(["convert", input_path, "-background", "none", os.path.join(OUTPUT_DIR, f"{base_name}.tiff")], check=True)
        
        # JPG (White background, flattened)
        subprocess.run(["convert", input_path, "-background", "white", "-flatten", os.path.join(OUTPUT_DIR, f"{base_name}.jpg")], check=True)
        
        # EPS (Raster embedded)
        subprocess.run(["convert", input_path, "-background", "none", os.path.join(OUTPUT_DIR, f"{base_name}.eps")], check=True)
        
        # AI (PDF renamed to AI for compatibility)
        pdf_path = os.path.join(OUTPUT_DIR, f"{base_name}.pdf")
        ai_path = os.path.join(OUTPUT_DIR, f"{base_name}.ai")
        subprocess.run(["convert", input_path, "-background", "none", pdf_path], check=True)
        subprocess.run(["cp", pdf_path, ai_path], check=True)

        # SVG (Embed base64)
        with open(input_path, "rb") as img_file:
            b64_data = base64.b64encode(img_file.read()).decode("utf-8")
            
        # Get dimensions using identify
        dim_res = subprocess.check_output(["identify", "-format", "%w %h", input_path]).decode().strip().split()
        if len(dim_res) >= 2:
            w, h = dim_res[0], dim_res[1]
            svg_content = f'''<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg width="{w}" height="{h}" viewBox="0 0 {w} {h}" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
    <image width="{w}" height="{h}" xlink:href="data:image/png;base64,{b64_data}"/>
</svg>'''
            with open(os.path.join(OUTPUT_DIR, f"{base_name}.svg"), "w") as svg_file:
                svg_file.write(svg_content)
        
        print(f"Successfully converted {base_name}")
        
    except Exception as e:
        print(f"Error processing {base_name}: {e}")

print("All conversions completed.")
