import math
from PIL import Image, ImageDraw

def generate_icon(size):
    img = Image.new("RGBA", (size, size), (10, 15, 29, 255))
    draw = ImageDraw.Draw(img)
    
    # Outer dark martial circle
    padding = size * 0.08
    cx, cy = size / 2, size / 2
    r = (size - 2 * padding) / 2
    
    # Draw golden octagram / bagua ring
    draw.ellipse([cx - r, cy - r, cx + r, cy + r], outline=(245, 158, 11, 230), width=max(2, int(size * 0.025)))
    
    inner_r = r * 0.85
    draw.ellipse([cx - inner_r, cy - inner_r, cx + inner_r, cy + inner_r], outline=(59, 130, 246, 200), width=max(1, int(size * 0.015)))
    
    # Draw astral meridian nodes around ring (8 nodes)
    for i in range(8):
        angle = i * (math.pi / 4)
        nx = cx + inner_r * math.cos(angle)
        ny = cy + inner_r * math.sin(angle)
        node_r = max(3, int(size * 0.035))
        draw.ellipse([nx - node_r, ny - node_r, nx + node_r, ny + node_r], fill=(236, 72, 153, 240), outline=(255, 255, 255, 255), width=max(1, int(size * 0.01)))

    # Draw Central Ancient Sword / Blade
    sword_top = cy - r * 0.7
    sword_bottom = cy + r * 0.7
    sword_w = max(4, int(size * 0.05))
    
    # Blade
    draw.polygon([
        (cx, sword_top),
        (cx + sword_w, cy + r * 0.3),
        (cx - sword_w, cy + r * 0.3)
    ], fill=(56, 189, 248, 255))
    
    # Blade highlight
    draw.line([(cx, sword_top), (cx, cy + r * 0.3)], fill=(255, 255, 255, 255), width=max(1, int(size * 0.015)))
    
    # Crossguard
    guard_w = max(6, int(size * 0.16))
    guard_h = max(2, int(size * 0.03))
    draw.rectangle([cx - guard_w, cy + r * 0.3, cx + guard_w, cy + r * 0.3 + guard_h], fill=(245, 158, 11, 255))
    
    # Hilt
    hilt_w = max(2, int(size * 0.03))
    draw.rectangle([cx - hilt_w, cy + r * 0.3 + guard_h, cx + hilt_w, sword_bottom - size * 0.04], fill=(148, 163, 184, 255))
    
    # Pommel
    pommel_r = max(3, int(size * 0.03))
    draw.ellipse([cx - pommel_r, sword_bottom - pommel_r, cx + pommel_r, sword_bottom + pommel_r], fill=(245, 158, 11, 255))
    
    return img

if __name__ == "__main__":
    icon_192 = generate_icon(192)
    icon_192.save("client/webapp/icon-192.png")
    print("Saved client/webapp/icon-192.png")
    
    icon_512 = generate_icon(512)
    icon_512.save("client/webapp/icon-512.png")
    print("Saved client/webapp/icon-512.png")
    
    favicon = generate_icon(64)
    favicon.save("client/webapp/favicon.ico", format="ICO")
    print("Saved client/webapp/favicon.ico")
