import yaml
from wand.image import Image
from wand.drawing import Drawing
from wand.color import Color

with open("design.yaml", "r") as f:
    config = yaml.safe_load(f)

canvas_cfg = config['canvas']
shapes = config['shapes']

# Create canvas
img = Image(width=canvas_cfg['width'], height=canvas_cfg['height'], background=Color(canvas_cfg['background']))

draw = Drawing()

for shape in shapes:
    t = shape['type']
    if t == 'rectangle':
        x, y = shape['position']
        w, h = shape['size']
        draw.fill_color = Color(shape['fill'])
        draw.stroke_color = Color(shape['stroke'])
        draw.stroke_width = shape.get('stroke_width', 1)
        draw.rectangle(left=x, top=y, width=w, height=h)

    elif t == 'circle':
        cx, cy = shape['center']
        r = shape['radius']
        draw.fill_color = Color(shape['fill'])
        draw.stroke_color = Color(shape['stroke'])
        draw.stroke_width = shape.get('stroke_width', 1)
        draw.circle((cx, cy), (cx + r, cy))

    elif t == 'line':
        sx, sy = shape['start']
        ex, ey = shape['end']
        draw.stroke_color = Color(shape['stroke'])
        draw.stroke_width = shape.get('stroke_width', 1)
        draw.line((sx, sy), (ex, ey))

    elif t == 'text':
        x, y = shape['position']
        draw.font_size = shape['font_size']
        draw.fill_color = Color(shape['fill'])
        draw.text(x, y, shape['content'])

draw(img)
img.save(filename='output.png')
print("✅ Image saved as output.png")
