#!/usr/bin/env python3
"""
Generate Blueprint Cut Sheet 11: 2' x 4' (24" x 48") Project Panel Nesting & Cut Diagram.
Demonstrates that ALL 12 house carcass, roof, and partition panels fit on SIX (6) 2' x 4'
handy project panels of 1/4" Baltic Birch plywood with rear roofline brought back & 20% roof height reduction.
"""

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", 23)
    font_sub = ImageFont.truetype("arial.ttf", 13)
    font_badge = ImageFont.truetype("arialbd.ttf", 15)
    font_sheet_title = ImageFont.truetype("arialbd.ttf", 13)
    font_dim = ImageFont.truetype("arialbd.ttf", 11)
    font_dim_sub = ImageFont.truetype("arial.ttf", 10)
    font_label = ImageFont.truetype("arialbd.ttf", 12)
    font_notes = ImageFont.truetype("arial.ttf", 12)
    font_notes_bd = ImageFont.truetype("arialbd.ttf", 12)
except Exception:
    font_title = font_sub = font_badge = font_sheet_title = font_dim = font_dim_sub = font_label = font_notes = font_notes_bd = ImageFont.load_default()

# Colors (Clean High-Contrast White Architectural Drafting Theme)
BG_COLOR = (255, 255, 255)         # Pure White Canvas
GRID_COLOR = (241, 245, 249)       # Faint Grid (Slate 100)
CARD_BG = (248, 250, 252)          # Slate 50
BORDER_COLOR = (148, 163, 184)     # Slate 400
SHEET_BG = (255, 255, 255)         # Sheet Face
SHEET_BORDER = (71, 85, 105)       # Slate 600
PANEL_FILL = (241, 245, 249)       # Birch Drafting Tint
PANEL_OUTLINE = (15, 23, 42)       # Slate 900
SCRAP_FILL = (248, 250, 252)       # Scrap Tint
SCRAP_BORDER = (203, 213, 225)     # Slate 300
KERF_COLOR = (220, 38, 38)         # Red 600 Saw Cut
TEXT_DARK = (15, 23, 42)           # Dark Slate 900
TEXT_MUTED = (71, 85, 105)         # Slate 600
TEXT_AMBER = (180, 83, 9)          # Deep Amber 700
TEXT_EMERALD = (4, 120, 87)        # Deep Emerald 700
DIM_COLOR = (2, 132, 199)          # Blue 600
TEXT_LIGHT = (255, 255, 255)

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

    # Grid lines
    for x in range(0, IMG_WIDTH, 40):
        draw.line([(x, 0), (x, IMG_HEIGHT)], fill=GRID_COLOR, width=1)
    for y in range(0, IMG_HEIGHT, 40):
        draw.line([(0, y), (IMG_WIDTH, y)], fill=GRID_COLOR, width=1)

    # Outer border
    draw.rectangle([(20, 20), (IMG_WIDTH - 20, IMG_HEIGHT - 20)], outline=BORDER_COLOR, width=2)

    # Header Card
    draw.rectangle([(20, 20), (IMG_WIDTH - 20, 95)], fill=CARD_BG, outline=BORDER_COLOR, width=2)
    badge_w = 95
    badge_h = 42
    draw.rectangle([(35, 36), (35 + badge_w, 36 + badge_h)], fill=(37, 99, 235), outline=(29, 78, 216), width=2)
    draw.text((35 + badge_w / 2, 36 + badge_h / 2), "CUT 11", fill=TEXT_LIGHT, font=font_badge, anchor="mm")

    title_x = 35 + badge_w + 20
    draw.text((title_x, 38), "Master 2' × 4' Project Panel Nesting & Cut Diagram (6 Sheets)", fill=TEXT_DARK, font=font_title)
    draw.text((title_x, 66), "All 12 House Panels on SIX (6) 1/4\" Baltic Birch Handy Panels (24\" × 48\") • Framed Rear Roofline • 8.40\" Roof Rise (20% Reduced)", fill=TEXT_MUTED, font=font_sub)

    # Sheet layout geometry: 3 columns x 2 rows
    # Each sheet is 48" W x 24" H
    scale = 8.4 # 48" * 8.4 = 403.2 px W, 24" * 8.4 = 201.6 px H
    sw_px = 48.0 * scale
    sh_px = 24.0 * scale

    cols_x = [50, 495, 940]
    rows_y = [135, 380]

    def draw_panel(ox, oy, x_in, y_in, w_in, h_in, label_id, label_name, dim_str, is_scrap=False, fill_col=PANEL_FILL):
        px1 = ox + x_in * scale
        py1 = oy + y_in * scale
        px2 = px1 + w_in * scale
        py2 = py1 + h_in * scale

        if is_scrap:
            draw.rectangle([(px1, py1), (px2, py2)], fill=SCRAP_FILL, outline=SCRAP_BORDER, width=1)
            if w_in >= 7 and h_in >= 4:
                draw.text(((px1 + px2)/2, (py1 + py2)/2 - 6), label_id, fill=(100, 116, 139), font=font_dim, anchor="mm")
                draw.text(((px1 + px2)/2, (py1 + py2)/2 + 7), dim_str, fill=(148, 163, 184), font=font_dim_sub, anchor="mm")
            return

        draw.rectangle([(px1, py1), (px2, py2)], fill=fill_col, outline=PANEL_OUTLINE, width=2)
        mid_x = (px1 + px2) / 2
        mid_y = (py1 + py2) / 2

        if h_in < 9.0:
            draw.text((mid_x, mid_y - 12), label_id, fill=TEXT_AMBER, font=font_dim, anchor="mm")
            draw.text((mid_x, mid_y), label_name, fill=TEXT_DARK, font=font_dim_sub, anchor="mm")
            draw.text((mid_x, mid_y + 12), dim_str, fill=DIM_COLOR, font=font_dim_sub, anchor="mm")
        else:
            draw.text((mid_x, mid_y - 14), label_id, fill=TEXT_AMBER, font=font_badge, anchor="mm")
            draw.text((mid_x, mid_y + 1), label_name, fill=TEXT_DARK, font=font_label, anchor="mm")
            draw.text((mid_x, mid_y + 16), dim_str, fill=DIM_COLOR, font=font_dim, anchor="mm")

    # -------------------------------------------------------------------------
    # ROW 1: SHEETS 1, 2, 3
    # -------------------------------------------------------------------------

    # --- SHEET 1 (Col 0, Row 0): Front Roof E1 + Partitions D1-A & D1-B ---
    s1_x, s1_y = cols_x[0], rows_y[0]
    draw.text((s1_x + sw_px/2, s1_y - 16), "SHEET 1 of 6: Front Roof E1 & 1st Fl Partitions D1", fill=TEXT_DARK, font=font_sheet_title, anchor="mb")
    draw.rectangle([(s1_x, s1_y), (s1_x + sw_px, s1_y + sh_px)], fill=SHEET_BG, outline=SHEET_BORDER, width=2)
    # E1: 34 x 12.04
    draw_panel(s1_x, s1_y, 0.0, 0.0, 34.0, 12.04, "E1", "Front Roof Slope", "34\" × 12-1/16\" (12.04\")")
    # Offcut next to E1
    draw_panel(s1_x, s1_y, 34.125, 0.0, 13.875, 12.04, "ROOF OFFCUT", "", "13-7/8\"×12-1/16\"", is_scrap=True)
    # D1-A: 12 x 8.25
    draw_panel(s1_x, s1_y, 0.0, 12.165, 12.0, 8.25, "D1-A", "Living / Kitchen", "12\" × 8-1/4\"")
    # D1-B: 12 x 8.25
    draw_panel(s1_x, s1_y, 12.125, 12.165, 12.0, 8.25, "D1-B", "Kitchen / Foyer", "12\" × 8-1/4\"")
    # Offcut next to D1
    draw_panel(s1_x, s1_y, 24.25, 12.165, 23.75, 8.25, "RESERVE STRIP", "", "23-3/4\"×8-1/4\"", is_scrap=True)
    # Bottom margin scrap: 48" x 3.46"
    draw_panel(s1_x, s1_y, 0.0, 20.54, 48.0, 3.46, "SCRAP STRIP", "", "48\" × 3-1/2\"", is_scrap=True)
    # Kerf cut lines
    draw.line([(s1_x, s1_y + 12.10*scale), (s1_x + sw_px, s1_y + 12.10*scale)], fill=KERF_COLOR, width=1)
    draw.line([(s1_x + 34.06*scale, s1_y), (s1_x + 34.06*scale, s1_y + 12.04*scale)], fill=KERF_COLOR, width=1)

    # --- SHEET 2 (Col 1, Row 0): Rear Roof E2 + Partitions D2-A & D2-B ---
    s2_x, s2_y = cols_x[1], rows_y[0]
    draw.text((s2_x + sw_px/2, s2_y - 16), "SHEET 2 of 6: Rear Roof E2 & 2nd Fl Partitions D2", fill=TEXT_DARK, font=font_sheet_title, anchor="mb")
    draw.rectangle([(s2_x, s2_y), (s2_x + sw_px, s2_y + sh_px)], fill=SHEET_BG, outline=SHEET_BORDER, width=2)
    # E2: 34 x 12.04
    draw_panel(s2_x, s2_y, 0.0, 0.0, 34.0, 12.04, "E2", "Rear Roof Slope (Cutout)", "34\" × 12-1/16\" (12.04\")")
    # Offcut next to E2
    draw_panel(s2_x, s2_y, 34.125, 0.0, 13.875, 12.04, "ROOF OFFCUT", "", "13-7/8\"×12-1/16\"", is_scrap=True)
    # D2-A: 12 x 8.25
    draw_panel(s2_x, s2_y, 0.0, 12.165, 12.0, 8.25, "D2-A", "Bath / Women", "12\" × 8-1/4\"")
    # D2-B: 12 x 8.25
    draw_panel(s2_x, s2_y, 12.125, 12.165, 12.0, 8.25, "D2-B", "Women / Mans", "12\" × 8-1/4\"")
    # Offcut next to D2
    draw_panel(s2_x, s2_y, 24.25, 12.165, 23.75, 8.25, "RESERVE STRIP", "", "23-3/4\"×8-1/4\"", is_scrap=True)
    # Bottom margin scrap
    draw_panel(s2_x, s2_y, 0.0, 20.54, 48.0, 3.46, "SCRAP STRIP", "", "48\" × 3-1/2\"", is_scrap=True)
    draw.line([(s2_x, s2_y + 12.10*scale), (s2_x + sw_px, s2_y + 12.10*scale)], fill=KERF_COLOR, width=1)
    draw.line([(s2_x + 34.06*scale, s2_y), (s2_x + 34.06*scale, s2_y + 12.04*scale)], fill=KERF_COLOR, width=1)

    # --- SHEET 3 (Col 2, Row 0): Front Facade C1 & Craft Panel ---
    s3_x, s3_y = cols_x[2], rows_y[0]
    draw.text((s3_x + sw_px/2, s3_y - 16), "SHEET 3 of 6: Front Facade C1 & Solid Craft Reserve", fill=TEXT_DARK, font=font_sheet_title, anchor="mb")
    draw.rectangle([(s3_x, s3_y), (s3_x + sw_px, s3_y + sh_px)], fill=SHEET_BG, outline=SHEET_BORDER, width=2)
    # C1: 32 x 16.50
    draw_panel(s3_x, s3_y, 0.0, 0.0, 32.0, 16.50, "C1", "Front Facade Wall Plate", "32\" × 16-1/2\" (16.50\")")
    # Solid end panel: 15.875" x 24"
    draw_panel(s3_x, s3_y, 32.125, 0.0, 15.875, 24.0, "SOLID CRAFT PANEL", "(For Risers & Trim)", "15-7/8\" × 24\" (381 sq in)", is_scrap=True)
    # Bottom strip: 32" x 7.375"
    draw_panel(s3_x, s3_y, 0.0, 16.625, 32.0, 7.375, "CLEAT & TRIM RESERVE", "", "32\" × 7-3/8\"", is_scrap=True)
    draw.line([(s3_x + 32.06*scale, s3_y), (s3_x + 32.06*scale, s3_y + sh_px)], fill=KERF_COLOR, width=1)
    draw.line([(s3_x, s3_y + 16.56*scale), (s3_x + 32.0*scale, s3_y + 16.56*scale)], fill=KERF_COLOR, width=1)

    # -------------------------------------------------------------------------
    # ROW 2: SHEETS 4, 5, 6
    # -------------------------------------------------------------------------

    # --- SHEET 4 (Col 0, Row 1): Floor Decks A1 (Base) & A2 (2nd Fl) ---
    s4_x, s4_y = cols_x[0], rows_y[1]
    draw.text((s4_x + sw_px/2, s4_y - 16), "SHEET 4 of 6: Floor Decks A1 (Base) & A2 (2nd Fl)", fill=TEXT_DARK, font=font_sheet_title, anchor="mb")
    draw.rectangle([(s4_x, s4_y), (s4_x + sw_px, s4_y + sh_px)], fill=SHEET_BG, outline=SHEET_BORDER, width=2)
    # A1: 32 x 12
    draw_panel(s4_x, s4_y, 0.0, 0.0, 32.0, 12.0, "A1", "Base Deck Plate", "32\" × 12\" (12.00\")")
    # A2: 32 x 11.875
    draw_panel(s4_x, s4_y, 0.0, 12.125, 32.0, 11.875, "A2", "Second Floor Deck", "32\" × 12\" (11-7/8\" net)")
    # Solid end offcut: 15.875" x 24"
    draw_panel(s4_x, s4_y, 32.125, 0.0, 15.875, 24.0, "SOLID CRAFT PANEL", "(Stairs & Landings)", "15-7/8\" × 24\" (381 sq in)", is_scrap=True)
    draw.line([(s4_x + 32.06*scale, s4_y), (s4_x + 32.06*scale, s4_y + sh_px)], fill=KERF_COLOR, width=1)
    draw.line([(s4_x, s4_y + 12.06*scale), (s4_x + 32.0*scale, s4_y + 12.06*scale)], fill=KERF_COLOR, width=1)

    # --- SHEET 5 (Col 1, Row 1): Gable Walls B1 (Left) & B2 (Right) ---
    s5_x, s5_y = cols_x[1], rows_y[1]
    draw.text((s5_x + sw_px/2, s5_y - 16), "SHEET 5 of 6: Gable Walls B1 (Left) & B2 (Right)", fill=TEXT_DARK, font=font_sheet_title, anchor="mb")
    draw.rectangle([(s5_x, s5_y), (s5_x + sw_px, s5_y + sh_px)], fill=SHEET_BG, outline=SHEET_BORDER, width=2)
    # B1: 24.90 x 12
    draw_panel(s5_x, s5_y, 0.0, 0.0, 24.90, 12.0, "B1", "Left Gable Wall", "12\" × 24.90\" (24-7/8\")")
    # B2: 24.90 x 11.875
    draw_panel(s5_x, s5_y, 0.0, 12.125, 24.90, 11.875, "B2", "Right Gable Wall", "12\" × 24.90\" (11-7/8\" net)")
    # Massive end offcut: 22.975" x 24" (551 sq in!)
    draw_panel(s5_x, s5_y, 25.025, 0.0, 22.975, 24.0, "MASSIVE RESERVE", "(Plinth, Hearth & Jigs)", "23\" × 24\" (551 sq in)", is_scrap=True)
    draw.line([(s5_x + 24.96*scale, s5_y), (s5_x + 24.96*scale, s5_y + sh_px)], fill=KERF_COLOR, width=1)
    draw.line([(s5_x, s5_y + 12.06*scale), (s5_x + 24.90*scale, s5_y + 12.06*scale)], fill=KERF_COLOR, width=1)

    # --- SHEET 6 (Col 2, Row 1): Attic Deck A3 & Master Craft Reserve ---
    s6_x, s6_y = cols_x[2], rows_y[1]
    draw.text((s6_x + sw_px/2, s6_y - 16), "SHEET 6 of 6: Attic Deck A3 & Master Craft Reserve", fill=TEXT_DARK, font=font_sheet_title, anchor="mb")
    draw.rectangle([(s6_x, s6_y), (s6_x + sw_px, s6_y + sh_px)], fill=SHEET_BG, outline=SHEET_BORDER, width=2)
    # A3: 32 x 12
    draw_panel(s6_x, s6_y, 0.0, 0.0, 32.0, 12.0, "A3", "Attic Floor Deck", "32\" × 12\" (12.00\")")
    # Full 32" x 11.875" pristine panel
    draw_panel(s6_x, s6_y, 0.0, 12.125, 32.0, 11.875, "PRISTINE CRAFT PANEL", "(Plinth / Furniture Crafting)", "32\" × 11-7/8\" (380 sq in)", is_scrap=True)
    # Solid end offcut: 15.875" x 24"
    draw_panel(s6_x, s6_y, 32.125, 0.0, 15.875, 24.0, "SOLID CRAFT PANEL", "(Test Dados & Fixtures)", "15-7/8\" × 24\" (381 sq in)", is_scrap=True)
    draw.line([(s6_x + 32.06*scale, s6_y), (s6_x + 32.06*scale, s6_y + sh_px)], fill=KERF_COLOR, width=1)
    draw.line([(s6_x, s6_y + 12.06*scale), (s6_x + 32.0*scale, s6_y + 12.06*scale)], fill=KERF_COLOR, width=1)

    # -------------------------------------------------------------------------
    # SHOP TAKE-OFF TABLE & SUMMARY (Y = 595 to 805)
    # -------------------------------------------------------------------------
    table_y = 595
    draw.rectangle([(35, table_y), (IMG_WIDTH - 35, table_y + 205)], fill=CARD_BG, outline=BORDER_COLOR, width=2)

    draw.text((55, table_y + 12), "2' × 4' PROJECT PANEL (1/4\" BALTIC BIRCH) SHOP TAKE-OFF & CUTTING SEQUENCE (6 SHEETS):", fill=TEXT_AMBER, font=font_badge)

    lines = [
        ("Sheet 1 (24\" × 48\"):", "E1 Front Roof Slope (34\" × 12-1/16\")  +  D1-A Living/Kitchen Div (12\" × 8-1/4\")  +  D1-B Kitchen/Foyer Div (12\" × 8-1/4\")", "Leaves 13-7/8\"×12\" & 23-3/4\"×8-1/4\" offcuts"),
        ("Sheet 2 (24\" × 48\"):", "E2 Rear Roof Slope (34\" × 12-1/16\")  +  D2-A Bath/Women Div (12\" × 8-1/4\")  +  D2-B Women/Mans Div (12\" × 8-1/4\")", "Leaves 13-7/8\"×12\" & 23-3/4\"×8-1/4\" offcuts"),
        ("Sheet 3 (24\" × 48\"):", "C1 Front Facade (32\" × 16-1/2\")  +  Solid 15-7/8\" × 24\" Craft Panel  +  32\" × 7-3/8\" Trim Strip", "Leaves large furniture craft panel for stairs & trim"),
        ("Sheet 4 (24\" × 48\"):", "A1 Base Platform Deck (32\" × 12\")  +  A2 Second Floor Deck (32\" × 12\" nominal / 11-7/8\" net)", "Leaves solid 15-7/8\" × 24\" craft panel"),
        ("Sheet 5 (24\" × 48\"):", "B1 Left Gable Wall (12\" × 24.90\")  +  B2 Right Gable Wall (12\" × 24.90\" net)", "Leaves massive 23\" × 24\" reserve panel (551 sq in!)"),
        ("Sheet 6 (24\" × 48\"):", "A3 Attic Floor Deck (32\" × 12\")  +  Full 32\" × 11-7/8\" & 15-7/8\" × 24\" pristine craft panels", "Provides test dado scrap, stair stringers & plinth framing"),
    ]

    cur_y = table_y + 36
    for sheet_lbl, parts_str, notes_str in lines:
        draw.text((55, cur_y), sheet_lbl, fill=TEXT_DARK, font=font_notes_bd)
        draw.text((230, cur_y), parts_str, fill=TEXT_DARK, font=font_notes)
        draw.text((1070, cur_y), f"• {notes_str}", fill=TEXT_MUTED, font=font_notes)
        cur_y += 24

    draw.line([(55, cur_y + 4), (IMG_WIDTH - 55, cur_y + 4)], fill=BORDER_COLOR, width=1)
    draw.text((55, cur_y + 11), "ROOF LINE DESIGN: Rear slope E2 restores the classic gable silhouette from the backview, with a 27\" × 8.40\" cutout giving direct attic reach!", fill=TEXT_EMERALD, font=font_notes_bd)

    # -------------------------------------------------------------------------
    # FOOTER BAR (Y = 815 to 895)
    # -------------------------------------------------------------------------
    foot_y = 815
    draw.rectangle([(20, foot_y), (IMG_WIDTH - 20, 895)], fill=CARD_BG, outline=BORDER_COLOR, width=2)
    draw.text((40, foot_y + 12), "MATERIAL SUMMARY & CAR TRUNK CONVENIENCE:", fill=TEXT_AMBER, font=font_notes_bd)
    draw.text((40, foot_y + 32), "1. Purchase Six (6) Handy Project Panels of 1/4\" Baltic Birch (nominal 2 ft × 4 ft / 24\" × 48\"). Total stock = 48.00 sq ft; Net house carcass = 24.22 sq ft.", fill=TEXT_DARK, font=font_notes)
    draw.text((40, foot_y + 52), "2. Yields 23.78 sq ft of premium birch offcuts for stair treads, fireplace mantels, base plinth framing, and test router dados. All panels fit in any standard vehicle.", fill=TEXT_MUTED, font=font_notes)

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

if __name__ == "__main__":
    gen_cut_11_six_sheets()
