import math
from PIL import Image, ImageDraw

def draw_gear(draw, cx, cy, r_out, r_in, teeth_count, teeth_h, teeth_w, color):
    # Draw outer body
    draw.ellipse([cx - r_out, cy - r_out, cx + r_out, cy + r_out], fill=color)
    
    # Draw teeth
    for i in range(teeth_count):
        angle = i * (2 * math.pi / teeth_count)
        
        # Center of tooth base on the outer radius
        x0 = cx + r_out * math.cos(angle)
        y0 = cy + r_out * math.sin(angle)
        
        # Tangent vector for width
        tx = -math.sin(angle)
        ty = math.cos(angle)
        
        # Normal vector for height
        nx = math.cos(angle)
        ny = math.sin(angle)
        
        # 4 points of the tooth (trapezoid style: narrower at the tip)
        p1 = (x0 - (teeth_w/2) * tx, y0 - (teeth_w/2) * ty)
        p2 = (x0 + (teeth_w/2) * tx, y0 + (teeth_w/2) * ty)
        
        p3 = (x0 + teeth_h * nx + (teeth_w/3) * tx, y0 + teeth_h * ny + (teeth_w/3) * ty)
        p4 = (x0 + teeth_h * nx - (teeth_w/3) * tx, y0 + teeth_h * ny - (teeth_w/3) * ty)
        
        draw.polygon([p1, p2, p3, p4], fill=color)
        
    # Draw inner cutout (hole)
    draw.ellipse([cx - r_in, cy - r_in, cx + r_in, cy + r_in], fill=(255, 255, 255, 255))

im = Image.open('scratch/logo_s_only.png')
draw = ImageDraw.Draw(im)
color = (29, 32, 47, 255)

# Draw gear at cx=765, cy=205
draw_gear(draw, cx=765, cy=205, r_out=45, r_in=18, teeth_count=8, teeth_h=15, teeth_w=18, color=color)

im.save('scratch/admin_logo_test.png')
print("Saved admin logo to scratch/admin_logo_test.png")
