#!/usr/bin/env python3
"""
Generate Blueprint Cut Sheet: 1:12 Scale Backyard Garden / Tool Shed (6" x 8")
Designed as a Workshop Practice & Tool Calibration Project on a 2' x 4' Plywood Sheet.
Teaches 1/8" grooves, 1/4" rabbets, gable angles, and opening piercing with zero risk.
"""

import os
from PIL import Image, ImageDraw, ImageFont

IMG_WIDTH = 1440
IMG_HEIGHT = 920

OUTPUT_DIRS = [
    r"c:\code2026\house\images",
    r"C:\Users\Hack\.gemini\antigravity\brain\be34cacd-367e-4ed7-819c-8f861ebb0996\images"
]

for d in OUTPUT_DIRS:
    os.makedirs(d, exist_ok=True)

try:
    font_title = ImageFont.truetype("arialbd.ttf", 24)
    font_sub = ImageFont.truetype("arial.ttf", 14)
    font_badge = ImageFont.truetype("arialbd.ttf", 15)
    font_dim = ImageFont.truetype("arialbd.ttf", 12)
    font_dim_sub = ImageFont.truetype("arial.ttf", 11)
    font_label = ImageFont.truetype("arialbd.ttf", 13)
    font_notes = ImageFont.truetype("arial.ttf", 12)
    font_notes_bd = ImageFont.truetype("arialbd.ttf", 13)
    font_cutout_title = ImageFont.truetype("arialbd.ttf", 12)
    font_cutout_sub = ImageFont.truetype("arial.ttf", 11)
except Exception:
    font_title = font_sub = font_badge = font_dim = font_dim_sub = font_label = font_notes = font_notes_bd = font_cutout_title = font_cutout_sub = ImageFont.load_default()

BG_COLOR = (12, 24, 44)
CARD_BG = (17, 34, 61)
BORDER_COLOR = (30, 58, 95)
PANEL_FILL = (23, 49, 84)
PANEL_OUTLINE = (56, 189, 248)
GROOVE_FILL = (245, 158, 11)
GROOVE_BORDER = (251, 191, 36)
RABBET_FILL = (217, 119, 6)
CUTOUT_BG = (45, 18, 25)
CUTOUT_BORDER = (239, 68, 68)
TEXT_WHITE = (248, 250, 252)
TEXT_MUTED = (148, 163, 184)
TEXT_AMBER = (251, 191, 36)
TEXT_EMERALD = (52, 211, 153)
DIM_COLOR = (56, 189, 248)

def draw_hatched_rect(draw, box, fill_color=GROOVE_FILL, border_color=GROOVE_BORDER, step=5):
    x1, y1, x2, y2 = box
    draw.rectangle([x1, y1, x2, y2], fill=fill_color, outline=border_color, width=2)
    width = int(x2 - x1)
    height = int(y2 - y1)
    for offset in range(-height, width, step):
        px1 = max(x1, min(x2, x1 + offset))
        py1 = y1 if (x1 + offset) >= x1 else y1 - (x1 + offset - x1)
        px2 = max(x1, min(x2, x1 + offset + height))
        py2 = min(y2, y1 + height)
        if px2 > px1 and py2 > py1:
            draw.line([(px1, py1), (px2, py2)], fill=border_color, width=1)

def gen_shed_blueprint():
    im = Image.new("RGBA", (IMG_WIDTH, IMG_HEIGHT), BG_COLOR)
    draw = ImageDraw.Draw(im)

    # Header Card
    draw.rectangle([(20, 20), (IMG_WIDTH - 20, 100)], fill=CARD_BG, outline=BORDER_COLOR, width=2)
    badge_w = 175
    draw.rectangle([(35, 35), (35 + badge_w, 85)], fill=TEXT_EMERALD)
    draw.text((35 + badge_w / 2, 60), "PRACTICE PROJECT", fill=(15, 23, 42), font=font_badge, anchor="mm")

    title_x = 35 + badge_w + 20
    draw.text((title_x, 40), "1:12 Scale Garden Tool Shed (6\" × 8\") — Test Cut & Joinery Practice Blueprint", fill=TEXT_WHITE, font=font_title)
    draw.text((title_x, 68), "Practice 1/8\" Dados, 1/4\" Edge Rabbets, 45° Gables & Jigsaw Openings on 2' × 4' Plywood (Zero-Risk Training)", fill=TEXT_AMBER, font=font_sub)

    # We will display 4 views / elements:
    # 1. Floor Deck S1 (8" x 6") with test dados
    # 2. Left Gable Wall S2 (6" x 9-1/2") with 3" peak and dados
    # 3. Front Wall S3 (8" x 6-1/2") with shed door and window
    # 4. Calibration & Test Scrap Board (testing 1/8" depth and 1/4" fit)

    scale = 28.0 # 1 inch = 28 pixels

    # -------------------------------------------------------------
    # ELEMENT 1: FLOOR DECK S1 (8" W x 6" D)
    # -------------------------------------------------------------
    ox1, oy1 = 60, 150
    w1, h1 = 8.0 * scale, 6.0 * scale
    draw.text((ox1 + w1/2, oy1 - 25), "PART S1: SHED FLOOR DECK (8\" × 6\")", fill=TEXT_AMBER, font=font_badge, anchor="mm")
    draw.rectangle([(ox1, oy1), (ox1 + w1, oy1 + h1)], fill=PANEL_FILL, outline=PANEL_OUTLINE, width=2)

    # End rabbets: 1/4" W x 1/8" D on left and right ends (receives gable walls)
    draw_hatched_rect(draw, (ox1, oy1, ox1 + 0.25*scale, oy1 + h1), RABBET_FILL, GROOVE_BORDER)
    draw_hatched_rect(draw, (ox1 + w1 - 0.25*scale, oy1, ox1 + w1, oy1 + h1), RABBET_FILL, GROOVE_BORDER)

    # Practice 1/8" Dado across the center: Workbench / Shelf Dado at X = 4"
    gx1 = ox1 + 3.875 * scale
    gx2 = ox1 + 4.125 * scale
    draw_hatched_rect(draw, (gx1, oy1, gx2, oy1 + h1), GROOVE_FILL, GROOVE_BORDER)

    draw.text((ox1 + w1/2, oy1 + h1/2 - 10), "8\" × 6\" Finished Deck", fill=TEXT_WHITE, font=font_label, anchor="mm")
    draw.text((ox1 + w1/2, oy1 + h1/2 + 12), "Practice Dado at Center (X = 4\")", fill=TEXT_AMBER, font=font_dim_sub, anchor="mm")
    draw.text((ox1 + 10, oy1 + h1 - 15), "1/4\" End Rabbet", fill=TEXT_AMBER, font=font_dim_sub, anchor="lm")
    draw.text((ox1 + w1 - 10, oy1 + h1 - 15), "1/4\" End Rabbet", fill=TEXT_AMBER, font=font_dim_sub, anchor="rm")

    # Dimensions
    draw.line([(ox1, oy1 - 8), (ox1 + w1, oy1 - 8)], fill=DIM_COLOR, width=1)
    draw.text((ox1 + w1/2, oy1 - 12), "8.00\" (8\") Width", fill=TEXT_WHITE, font=font_dim, anchor="mb")
    draw.line([(ox1 - 8, oy1), (ox1 - 8, oy1 + h1)], fill=DIM_COLOR, width=1)
    draw.text((ox1 - 12, oy1 + h1/2), "6.00\" (6\")", fill=TEXT_WHITE, font=font_dim, anchor="rm")

    # -------------------------------------------------------------
    # ELEMENT 2: GABLE SIDE WALL S2 (6" W x 9-1/2" H)
    # -------------------------------------------------------------
    ox2, oy2 = 360, 150
    w2 = 6.0 * scale
    h_eave2 = 6.5 * scale
    h_peak2 = 3.0 * scale
    tot_h2 = h_eave2 + h_peak2

    draw.text((ox2 + w2/2, oy2 - 25), "PART S2: GABLE SIDE WALL (6\" × 9-1/2\")", fill=TEXT_EMERALD, font=font_badge, anchor="mm")

    # Draw gable polygon
    pts2 = [
        (ox2, oy2 + tot_h2),                 # bottom left
        (ox2 + w2, oy2 + tot_h2),            # bottom right
        (ox2 + w2, oy2 + h_peak2),           # eave right
        (ox2 + w2/2, oy2),                   # peak center (3" rise)
        (ox2, oy2 + h_peak2),                # eave left
    ]
    draw.polygon(pts2, fill=PANEL_FILL, outline=PANEL_OUTLINE)

    # Vertical corner rabbet along front edge (1/4" W x 1/8" D)
    draw_hatched_rect(draw, (ox2, oy2 + h_peak2, ox2 + 0.25*scale, oy2 + tot_h2), RABBET_FILL, GROOVE_BORDER)

    # Bottom deck dado (at Y = 0 to 1/4" above baseline)
    draw_hatched_rect(draw, (ox2, oy2 + tot_h2 - 0.25*scale, ox2 + w2, oy2 + tot_h2), RABBET_FILL, GROOVE_BORDER)

    # Shelf Dado at Y = 2-1/2" (2.50" above floor)
    sy1 = oy2 + tot_h2 - 2.50 * scale - 0.25 * scale
    sy2 = oy2 + tot_h2 - 2.50 * scale
    draw_hatched_rect(draw, (ox2, sy1, ox2 + w2, sy2), GROOVE_FILL, GROOVE_BORDER)

    draw.text((ox2 + w2/2, oy2 + tot_h2 - 4.5*scale), "Gable Peak (45° Pitch)", fill=TEXT_WHITE, font=font_label, anchor="mm")
    draw.text((ox2 + w2/2, sy1 - 10), "1/8\" Shelf Dado (Y = 2-1/2\")", fill=TEXT_AMBER, font=font_dim_sub, anchor="mm")
    draw.text((ox2 + 10, oy2 + h_peak2 + 30), "Vertical 1/4\" Rabbet", fill=TEXT_AMBER, font=font_dim_sub, anchor="lm")

    # Dimensions
    draw.line([(ox2 + w2 + 10, oy2), (ox2 + w2 + 10, oy2 + tot_h2)], fill=DIM_COLOR, width=1)
    draw.text((ox2 + w2 + 15, oy2 + tot_h2/2), "9.50\" (9-1/2\") Peak H", fill=TEXT_WHITE, font=font_dim, anchor="lm")
    draw.line([(ox2, oy2 + tot_h2 + 15), (ox2 + w2, oy2 + tot_h2 + 15)], fill=DIM_COLOR, width=1)
    draw.text((ox2 + w2/2, oy2 + tot_h2 + 20), "6.00\" Width", fill=TEXT_WHITE, font=font_dim, anchor="mt")

    # -------------------------------------------------------------
    # ELEMENT 3: FRONT WALL S3 (8" W x 6-1/2" H) WITH CUTOUTS
    # -------------------------------------------------------------
    ox3, oy3 = 620, 150
    w3, h3 = 8.0 * scale, 6.5 * scale

    draw.text((ox3 + w3/2, oy3 - 25), "PART S3: FRONT WALL & CUTOUTS (8\" × 6-1/2\")", fill=TEXT_AMBER, font=font_badge, anchor="mm")
    draw.rectangle([(ox3, oy3), (ox3 + w3, oy3 + h3)], fill=PANEL_FILL, outline=PANEL_OUTLINE, width=2)

    # Shed Door Cutout: 2-1/2" W x 5-1/2" H (seated flush at bottom)
    door_x1 = ox3 + 1.25 * scale
    door_x2 = door_x1 + 2.50 * scale
    door_y2 = oy3 + h3
    door_y1 = door_y2 - 5.50 * scale
    draw.rectangle([(door_x1, door_y1), (door_x2, door_y2)], fill=CUTOUT_BG, outline=CUTOUT_BORDER, width=2)
    draw.text(((door_x1+door_x2)/2, (door_y1+door_y2)/2 - 8), "SHED DOOR", fill=TEXT_WHITE, font=font_cutout_title, anchor="mm")
    draw.text(((door_x1+door_x2)/2, (door_y1+door_y2)/2 + 10), "2-1/2\" × 5-1/2\"", fill=TEXT_AMBER, font=font_cutout_sub, anchor="mm")

    # Shed Window Cutout: 2" W x 2" H (centered on right half)
    win_x1 = ox3 + 4.75 * scale
    win_x2 = win_x1 + 2.00 * scale
    win_y1 = oy3 + 1.50 * scale
    win_y2 = win_y1 + 2.00 * scale
    draw.rectangle([(win_x1, win_y1), (win_x2, win_y2)], fill=CUTOUT_BG, outline=CUTOUT_BORDER, width=2)
    draw.text(((win_x1+win_x2)/2, (win_y1+win_y2)/2 - 8), "WINDOW", fill=TEXT_WHITE, font=font_cutout_title, anchor="mm")
    draw.text(((win_x1+win_x2)/2, (win_y1+win_y2)/2 + 10), "2\" × 2\"", fill=TEXT_AMBER, font=font_cutout_sub, anchor="mm")

    draw.text((ox3 + w3/2, oy3 + 20), "Practice Plunge & Scroll Saw Cuts", fill=TEXT_MUTED, font=font_dim_sub, anchor="mm")

    # Dimensions
    draw.line([(ox3, oy3 + h3 + 15), (ox3 + w3, oy3 + h3 + 15)], fill=DIM_COLOR, width=1)
    draw.text((ox3 + w3/2, oy3 + h3 + 20), "8.00\" Width", fill=TEXT_WHITE, font=font_dim, anchor="mt")

    # -------------------------------------------------------------
    # ELEMENT 4: CALIBRATION & TEST-CUT DRILL BOARD (ON SCRAP)
    # -------------------------------------------------------------
    ox4, oy4 = 920, 140
    w4, h4 = 480, 480
    draw.rectangle([(ox4, oy4), (ox4 + w4, oy4 + h4)], fill=CARD_BG, outline=BORDER_COLOR, width=2)

    draw.text((ox4 + w4/2, oy4 + 25), "TOOL CALIBRATION DRILLS (WORKBENCH SCRAP)", fill=TEXT_EMERALD, font=font_label, anchor="mm")
    draw.line([(ox4 + 20, oy4 + 40), (ox4 + w4 - 20, oy4 + 40)], fill=BORDER_COLOR, width=1)

    drills = [
        ("DRILL 1: DIAL IN 1/8\" (0.125\") BLADE DEPTH", 
         "• Raise saw blade / router bit to approximately 1/8\".\n"
         "• Cut a 4\" test groove across scrap.\n"
         "• Check with depth gauge or ruler: must be dead-on 1/8\"!\n"
         "• Rule: Leaves exactly 1/8\" of core plywood remaining."),

        ("DRILL 2: TWO-PASS 1/4\" (0.250\") DADO WIDTH",
         "• Make Pass 1 at Fence = F.\n"
         "• Nudge fence by blade kerf (Pass 2).\n"
         "• Test-fit offcut of actual 1/4\" plywood:\n"
         "  -> It must slide with light thumb pressure and stand\n"
         "     upright on its own without wobbling or binding!"),

        ("DRILL 3: 1/4\" EDGE RABBET WITH SACRIFICIAL FENCE",
         "• Clamp scrap board to table saw rip fence.\n"
         "• Bury 1/8\" blade 1/8\" deep into wood, leaving 1/4\" exposed.\n"
         "• Run edge against fence using push block in single pass.\n"
         "• Yields perfect 1/4\" W × 1/8\" D corner shoulder!"),

        ("DRILL 4: WINDOW/DOOR PLUNGE & CLEAN CORNERS",
         "• Drill 3/8\" pilot holes inside waste corners.\n"
         "• Cut with jigsaw / scroll saw 1/16\" inside line.\n"
         "• File/sand clean to pencil line for razor-sharp 90° corners.")
    ]

    cur_y = oy4 + 55
    for d_title, d_desc in drills:
        draw.text((ox4 + 20, cur_y), d_title, fill=TEXT_AMBER, font=font_notes_bd)
        cur_y += 18
        for line in d_desc.split('\n'):
            draw.text((ox4 + 30, cur_y), line, fill=TEXT_WHITE, font=font_notes)
            cur_y += 16
        cur_y += 10

    # -------------------------------------------------------------
    # FOOTER NOTES CARD
    # -------------------------------------------------------------
    foot_y = IMG_HEIGHT - 95
    draw.rectangle([(20, foot_y), (IMG_WIDTH - 20, IMG_HEIGHT - 15)], fill=CARD_BG, outline=BORDER_COLOR, width=2)
    draw.text((40, foot_y + 15), "PRACTICE SHED SHOP INSTRUCTIONS (CUT FROM A SINGLE 2' × 4' PLYWOOD SHEET):", fill=TEXT_EMERALD, font=font_notes_bd)
    draw.text((40, foot_y + 36), "1. Stock Take-Off: All shed parts take only 16\" × 24\" of your 2' × 4' panel. You have a massive 32\" × 24\" area left for zero-risk test cuts!", fill=TEXT_WHITE, font=font_notes)
    draw.text((40, foot_y + 56), "2. Master Test Rule: NEVER cut main house pieces until your scrap test joints slide together smoothly and stand square on their own!", fill=TEXT_MUTED, font=font_notes)

    # Save
    for d in OUTPUT_DIRS:
        p = os.path.join(d, "shed_practice_cut_blueprint.png")
        im.save(p)
        print(f"Saved: {p}")

if __name__ == "__main__":
    gen_shed_blueprint()
