#!/usr/bin/env python3
"""Generate Firehouse 2nd Floor Plan V11 - Professional Architectural Drawing"""

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from matplotlib.patches import FancyBboxPatch, Arc, Circle
import numpy as np

# Scale: 1 foot = 1 unit in plot coordinates
# Building: ~56' wide x 50' deep
# Origin at bottom-left of building

fig, ax = plt.subplots(1, 1, figsize=(16, 18))
fig.patch.set_facecolor('white')
ax.set_facecolor('white')

# Building dimensions
BW = 56  # building width
BH = 50  # building height
MARGIN = 8  # margin around building for dimensions/labels

ax.set_xlim(-MARGIN, BW + MARGIN)
ax.set_ylim(-14, BH + MARGIN)
ax.set_aspect('equal')
ax.axis('off')

# --- HELPER FUNCTIONS ---

def draw_rect(x, y, w, h, lw=2, color='black', fill=None, ls='-'):
    rect = patches.Rectangle((x, y), w, h, linewidth=lw, edgecolor=color,
                               facecolor=fill if fill else 'none', linestyle=ls, zorder=2)
    ax.add_patch(rect)

def draw_wall(x1, y1, x2, y2, lw=2, color='black'):
    ax.plot([x1, x2], [y1, y2], color=color, linewidth=lw, solid_capstyle='round', zorder=3)

def label(x, y, text, size=7, bold=True, color='black', ha='center', va='center', rotation=0):
    weight = 'bold' if bold else 'normal'
    ax.text(x, y, text, fontsize=size, fontweight=weight, color=color,
            ha=ha, va=va, rotation=rotation, zorder=5,
            fontfamily='sans-serif')

def dim_label(x, y, text, size=5.5, rotation=0):
    ax.text(x, y, text, fontsize=size, fontweight='normal', color='#444444',
            ha='center', va='center', rotation=rotation, zorder=5, fontstyle='italic',
            fontfamily='sans-serif')

def draw_door_arc(cx, cy, radius, start_angle, end_angle, direction='ccw'):
    """Draw a door swing arc"""
    theta = np.linspace(np.radians(start_angle), np.radians(end_angle), 30)
    xs = cx + radius * np.cos(theta)
    ys = cy + radius * np.sin(theta)
    ax.plot(xs, ys, color='#555555', linewidth=0.8, linestyle='--', zorder=3)
    # Door leaf
    ax.plot([cx, xs[0]], [cy, ys[0]], color='#555555', linewidth=1, zorder=3)
    ax.plot([cx, xs[-1]], [cy, ys[-1]], color='#555555', linewidth=1, zorder=3)

def draw_window(x1, y1, x2, y2, orientation='h'):
    """Draw window marks (parallel lines in wall)"""
    if orientation == 'h':
        mid = (y1 + y2) / 2
        offset = 0.3
        ax.plot([x1, x2], [mid - offset, mid - offset], color='#2277CC', linewidth=1.5, zorder=4)
        ax.plot([x1, x2], [mid + offset, mid + offset], color='#2277CC', linewidth=1.5, zorder=4)
        ax.plot([x1, x2], [mid, mid], color='#88BBEE', linewidth=0.5, zorder=4)
    else:  # vertical
        mid = (x1 + x2) / 2
        offset = 0.3
        ax.plot([mid - offset, mid - offset], [y1, y2], color='#2277CC', linewidth=1.5, zorder=4)
        ax.plot([mid + offset, mid + offset], [y1, y2], color='#2277CC', linewidth=1.5, zorder=4)
        ax.plot([mid, mid], [y1, y2], color='#88BBEE', linewidth=0.5, zorder=4)

def draw_toilet(cx, cy, scale=1.0):
    """Draw toilet symbol - oval bowl + rectangular tank"""
    tank = patches.Rectangle((cx - 0.6*scale, cy + 0.5*scale), 1.2*scale, 0.6*scale,
                              linewidth=0.8, edgecolor='#555', facecolor='white', zorder=4)
    bowl = patches.Ellipse((cx, cy), 1.2*scale, 1.2*scale,
                            linewidth=0.8, edgecolor='#555', facecolor='white', zorder=4)
    ax.add_patch(tank)
    ax.add_patch(bowl)

def draw_sink(cx, cy, scale=1.0, orientation='h'):
    """Draw sink symbol"""
    if orientation == 'h':
        sink = patches.Rectangle((cx - 0.6*scale, cy - 0.4*scale), 1.2*scale, 0.8*scale,
                                  linewidth=0.8, edgecolor='#555', facecolor='white',
                                  zorder=4, joinstyle='round')
        basin = patches.Ellipse((cx, cy), 0.7*scale, 0.5*scale,
                                 linewidth=0.6, edgecolor='#555', facecolor='#e8e8e8', zorder=4)
    else:
        sink = patches.Rectangle((cx - 0.4*scale, cy - 0.6*scale), 0.8*scale, 1.2*scale,
                                  linewidth=0.8, edgecolor='#555', facecolor='white',
                                  zorder=4, joinstyle='round')
        basin = patches.Ellipse((cx, cy), 0.5*scale, 0.7*scale,
                                 linewidth=0.6, edgecolor='#555', facecolor='#e8e8e8', zorder=4)
    ax.add_patch(sink)
    ax.add_patch(basin)

def draw_shower(x, y, w, h):
    """Draw shower area with dashed outline and X pattern"""
    draw_rect(x, y, w, h, lw=0.8, color='#666', ls='--')
    # Diagonal lines for shower floor
    ax.plot([x, x+w], [y, y+h], color='#bbb', linewidth=0.4, zorder=3)
    ax.plot([x+w, x], [y, y+h], color='#bbb', linewidth=0.4, zorder=3)
    label(x + w/2, y + h/2, 'SHOWER', size=5, bold=False, color='#666')

def draw_tub(x, y, w, h):
    """Draw bathtub"""
    outer = patches.Rectangle((x, y), w, h, linewidth=1, edgecolor='#555',
                               facecolor='white', zorder=4)
    inner = patches.FancyBboxPatch((x+0.3, y+0.3), w-0.6, h-0.6,
                                    boxstyle="round,pad=0.3", linewidth=0.6,
                                    edgecolor='#888', facecolor='#f0f0f0', zorder=4)
    ax.add_patch(outer)
    ax.add_patch(inner)

def draw_bed(cx, cy, w, h, pillows=2):
    """Draw bed symbol"""
    # Mattress
    bed = patches.FancyBboxPatch((cx - w/2, cy - h/2), w, h,
                                  boxstyle="round,pad=0.2", linewidth=0.8,
                                  edgecolor='#999', facecolor='#FFF8E7', zorder=3)
    ax.add_patch(bed)
    # Pillows
    pw = (w - 0.6) / pillows
    for i in range(pillows):
        px = cx - w/2 + 0.3 + i * (pw + 0.2)
        py = cy + h/2 - 1.8
        pillow = patches.FancyBboxPatch((px, py), pw - 0.2, 1.2,
                                         boxstyle="round,pad=0.15", linewidth=0.6,
                                         edgecolor='#bbb', facecolor='#fffff0', zorder=4)
        ax.add_patch(pillow)

def draw_washer_dryer(x, y, label_text='W'):
    """Draw washer or dryer circle"""
    circle = patches.Circle((x, y), 1.5, linewidth=1, edgecolor='#555',
                             facecolor='white', zorder=4)
    ax.add_patch(circle)
    label(x, y, label_text, size=7, bold=True, color='#555')

def draw_stairs(x, y, w, h, direction='down', n_steps=8):
    """Draw stair symbol"""
    draw_rect(x, y, w, h, lw=1.5, color='black')
    step_h = h / n_steps
    for i in range(n_steps):
        sy = y + i * step_h
        ax.plot([x, x + w], [sy, sy], color='#555', linewidth=0.6, zorder=3)
    # Arrow
    mid_x = x + w/2
    ax.annotate('', xy=(mid_x, y), xytext=(mid_x, y + h),
                arrowprops=dict(arrowstyle='->', color='black', lw=1.2), zorder=4)

# ============================================================
# EXTERIOR WALLS (thick lines)
# ============================================================
draw_rect(0, 0, BW, BH, lw=4, color='black')

# Central dividing wall (vertical, full height)
CENTRAL_X = 26
draw_wall(CENTRAL_X, 0, CENTRAL_X, BH, lw=3, color='#333')

# ============================================================
# LEFT SIDE - OFFICE SIDE (3 Bedrooms)
# ============================================================

# --- Bedroom 2: 18x12, upper left ---
BR2_X, BR2_Y = 0, 38
BR2_W, BR2_H = 18, 12
draw_rect(BR2_X, BR2_Y, BR2_W, BR2_H, lw=2)
# Fill bedroom light cream
fill = patches.Rectangle((BR2_X, BR2_Y), BR2_W, BR2_H, facecolor='#FFFDE7', edgecolor='none', zorder=1)
ax.add_patch(fill)
draw_bed(9, 43, 6, 8, pillows=2)
label(9, 40, 'BEDROOM 2', size=8)
dim_label(9, 38.8, "18'-0\" × 12'-0\"", size=5.5)
# Window on north wall
draw_window(5, BH-0.3, 13, BH+0.3, 'h')

# --- Closet upper left ---
CL1_X, CL1_Y = 0, 46
CL1_W, CL1_H = 4, 4
draw_rect(CL1_X, CL1_Y, CL1_W, CL1_H, lw=1.5)
label(2, 48, 'CL', size=6)
dim_label(2, 46.6, "4'×4'", size=4.5)

# --- Jack & Jill Bath: 9x8 ---
JJ_X, JJ_Y = 0, 28
JJ_W, JJ_H = 9, 10
draw_rect(JJ_X, JJ_Y, JJ_W, JJ_H, lw=2)
fill = patches.Rectangle((JJ_X, JJ_Y), JJ_W, JJ_H, facecolor='#E8F0FE', edgecolor='none', zorder=1)
ax.add_patch(fill)
label(4.5, 34, 'JACK & JILL', size=6.5)
label(4.5, 32.8, 'BATHROOM', size=6.5)
dim_label(4.5, 31.5, "9'-0\" × 8'-0\"", size=5)
# Fixtures
draw_sink(2, 36.5, 0.8, 'h')
draw_sink(5, 36.5, 0.8, 'h')
draw_toilet(7.5, 34, 0.7)
draw_shower(1, 28.5, 4, 3)
# Door arc into J&J from bedroom 2
draw_door_arc(9, 34, 2.5, 90, 180)
# Door arc from hallway
draw_door_arc(9, 31, 2.5, 180, 270)

# --- Laundry: 9x8 ---
LDR_X, LDR_Y = 9, 28
LDR_W, LDR_H = 9, 10
draw_rect(LDR_X, LDR_Y, LDR_W, LDR_H, lw=2)
fill = patches.Rectangle((LDR_X, LDR_Y), LDR_W, LDR_H, facecolor='#F5F5F5', edgecolor='none', zorder=1)
ax.add_patch(fill)
label(13.5, 33, 'LAUNDRY', size=7)
dim_label(13.5, 31.5, "9'-0\" × 8'-0\"", size=5)
draw_washer_dryer(12, 35.5, 'W')
draw_washer_dryer(15.5, 35.5, 'D')
# Door arc
draw_door_arc(13, 28, 2.5, 0, 90)

# --- Lower Closet ---
CL2_X, CL2_Y = 0, 24
CL2_W, CL2_H = 4, 4
draw_rect(CL2_X, CL2_Y, CL2_W, CL2_H, lw=1.5)
label(2, 26, 'CL', size=6)
dim_label(2, 24.6, "4'×3'", size=4.5)

# --- Bedroom 1: 18x12, mid left ---
BR1_X, BR1_Y = 4, 14
BR1_W, BR1_H = 14, 14
draw_rect(BR1_X, BR1_Y, BR1_W, BR1_H, lw=2)
fill = patches.Rectangle((BR1_X, BR1_Y), BR1_W, BR1_H, facecolor='#FFFDE7', edgecolor='none', zorder=1)
ax.add_patch(fill)
draw_bed(11, 21.5, 6, 8, pillows=2)
label(11, 19, 'BEDROOM 1', size=8)
dim_label(11, 17.5, "18'-0\" × 12'-0\"", size=5.5)
# Window on west wall
draw_window(-0.3, 18, 0.3, 24, 'v')
# Door arc
draw_door_arc(4, 24, 2.5, 0, 90)

# --- Bath 1: 9x6, lower left ---
B1_X, B1_Y = 0, 6
B1_W, B1_H = 6, 8
draw_rect(B1_X, B1_Y, B1_W, B1_H, lw=2)
fill = patches.Rectangle((B1_X, B1_Y), B1_W, B1_H, facecolor='#E8F0FE', edgecolor='none', zorder=1)
ax.add_patch(fill)
label(3, 11, 'BATH 1', size=6.5)
draw_toilet(1.5, 12, 0.7)
draw_sink(4.5, 12.5, 0.7, 'v')
draw_shower(0.5, 6.5, 3, 3)
# Door arc
draw_door_arc(6, 12, 2.5, 0, 90)

# --- Bedroom 3: 12x16, bottom center (wall pushed toward BR3) ---
BR3_X, BR3_Y = 6, 0
BR3_W, BR3_H = 14, 10
draw_rect(BR3_X, BR3_Y, BR3_W, BR3_H, lw=2)
fill = patches.Rectangle((BR3_X, BR3_Y), BR3_W, BR3_H, facecolor='#FFFDE7', edgecolor='none', zorder=1)
ax.add_patch(fill)
draw_bed(13, 4.5, 6, 7, pillows=2)
label(13, 3, 'BEDROOM 3', size=8)
dim_label(13, 1.5, "12'-0\" × 10'-0\"", size=5.5)
# 2 windows south wall
draw_window(8, -0.3, 12, 0.3, 'h')
draw_window(14, -0.3, 18, 0.3, 'h')
# Door arc
draw_door_arc(10, 10, 2.5, 270, 360)

# --- Exterior Deck ---
DECK_X, DECK_Y = 0, 0
DECK_W, DECK_H = 6, 6
draw_rect(DECK_X, DECK_Y, DECK_W, DECK_H, lw=1.5, color='#777', ls='--')
label(3, 3, 'EXTERIOR\nDECK', size=6, color='#555')

# --- Hall (left side vertical corridor) ---
HALL_X = 20
draw_wall(HALL_X, 0, HALL_X, 38, lw=1.5, color='#555')
# Horizontal wall between hall and BR3 (pushed toward BR3)
draw_wall(6, 10, CENTRAL_X, 10, lw=1.5, color='#555')
label(23, 18, 'H\nA\nL\nL', size=6, color='#555')

# ============================================================
# RIGHT SIDE - DORMS SIDE (Master Suite)
# ============================================================

# --- Master Bedroom: 24x24, upper right ---
MB_X = CENTRAL_X
MB_X2 = BW
MB_W = MB_X2 - CENTRAL_X  # 30
MB_H = 26  # upper portion
MB_Y = BH - MB_H  # 24

draw_rect(CENTRAL_X, MB_Y, MB_W, MB_H, lw=2)
fill = patches.Rectangle((CENTRAL_X, MB_Y), MB_W, MB_H, facecolor='#FFFDE7', edgecolor='none', zorder=1)
ax.add_patch(fill)

# King bed in master
draw_bed(46, 43, 7, 9, pillows=2)
label(41, 36, 'MASTER\nBEDROOM', size=10)
dim_label(41, 33.5, "24'-0\" × 24'-0\"", size=6)

# Windows - 2 on north wall
draw_window(30, BH-0.3, 38, BH+0.3, 'h')
draw_window(42, BH-0.3, 50, BH+0.3, 'h')
# 1 on east wall
draw_window(BW-0.3, 36, BW+0.3, 44, 'v')

# Sitting Area label (in the entry/hall area between closets and master bedroom)
label(30, 26, 'SITTING\nAREA', size=7, color='#666')

# Sitting Area label in the small rectangle between BR3, His closet, and stairwell
label(23, 6, 'SITTING\nAREA', size=5.5, color='#666')

# King label
label(46, 48.5, 'KING', size=5, bold=False, color='#888')

# --- His Walk-in Closet: 9x14 (extended toward stairwell) ---
HIS_X, HIS_Y = CENTRAL_X, 4
HIS_W, HIS_H = 8, 16
draw_rect(HIS_X, HIS_Y, HIS_W, HIS_H, lw=2)
fill = patches.Rectangle((HIS_X, HIS_Y), HIS_W, HIS_H, facecolor='#F5F5F5', edgecolor='none', zorder=1)
ax.add_patch(fill)
label(30, 13, 'HIS\nWALK-IN\nCLOSET', size=6)
dim_label(30, 7, "9'-0\" × 14'-0\"", size=5)
# Door arc
draw_door_arc(34, 20, 2.5, 0, 90)

# --- Her Walk-in Closet: 8x16 (extended toward stairwell) ---
HER_X, HER_Y = 34, 4
HER_W, HER_H = 8, 16
draw_rect(HER_X, HER_Y, HER_W, HER_H, lw=2)
fill = patches.Rectangle((HER_X, HER_Y), HER_W, HER_H, facecolor='#F5F5F5', edgecolor='none', zorder=1)
ax.add_patch(fill)
label(38, 13, 'HER\nWALK-IN\nCLOSET', size=6)
dim_label(38, 7, "8'-0\" × 16'-0\"", size=5)
# Red arrow showing flow
ax.annotate('', xy=(38, 20.5), xytext=(34, 20.5),
            arrowprops=dict(arrowstyle='->', color='red', lw=1.5), zorder=5)

# --- Master Bathroom: 14x18, right side ---
MBATH_X, MBATH_Y = 42, 4
MBATH_W, MBATH_H = 14, 20
draw_rect(MBATH_X, MBATH_Y, MBATH_W, MBATH_H, lw=2)
fill = patches.Rectangle((MBATH_X, MBATH_Y), MBATH_W, MBATH_H, facecolor='#E8F0FE', edgecolor='none', zorder=1)
ax.add_patch(fill)
label(49, 16, 'MASTER\nBATHROOM', size=7.5)
dim_label(49, 13.5, "14'-0\" × 18'-0\"", size=5.5)

# Double vanity
draw_sink(46, 22.5, 0.8, 'h')
draw_sink(49, 22.5, 0.8, 'h')
label(47.5, 23.8, 'DOUBLE VANITY', size=4.5, bold=False, color='#666')

# Linen closet
draw_rect(42.5, 17, 3, 4, lw=1, color='#777')
label(44, 19, 'LINEN', size=5, bold=False, color='#666')

# W.C. (water closet)
draw_rect(47, 8, 4, 5, lw=1.5, color='#555')
draw_toilet(49, 10, 0.8)
label(49, 12.2, 'W.C.', size=5, color='#555')

# Shower
draw_shower(52, 7, 3.5, 6)

# Soaking tub
draw_tub(43, 7, 3.5, 5)
label(44.75, 5.5, 'SOAKING\nTUB', size=4.5, bold=False, color='#666')

# --- Stairwell Landing: full width across bottom of dorms side ---
STL_X, STL_Y = CENTRAL_X, 0
STL_W, STL_H = BW - CENTRAL_X, 4  # full width from central wall to east wall
draw_rect(STL_X, STL_Y, STL_W, STL_H, lw=2)
fill = patches.Rectangle((STL_X, STL_Y), STL_W, STL_H, facecolor='#F0F0F0', edgecolor='none', zorder=1)
ax.add_patch(fill)
label(41, 2, 'STAIRWELL LANDING', size=7, color='#CC3333')

# Stair step lines all the way across
for i in range(8):
    sx = STL_X + i * (STL_W / 8)
    # no — horizontal step lines across
n_steps = 7
step_w = STL_W / n_steps
for i in range(n_steps):
    lx = STL_X + i * step_w
    ax.plot([lx, lx], [STL_Y, STL_Y + STL_H], color='#555', linewidth=0.6, zorder=3)

# Main stair arrow
ax.annotate('', xy=(STL_X, 2), xytext=(STL_X + STL_W, 2),
            arrowprops=dict(arrowstyle='->', color='black', lw=1.2), zorder=4)
label(BW - 3, 2, 'MAIN\nSTAIR\nDN', size=5.5)

# ============================================================
# CENTER FEATURES
# ============================================================

# --- Stair DN to Kitchen: upper center ---
SK_X, SK_Y = 20, 40
SK_W, SK_H = 6, 8
draw_stairs(SK_X, SK_Y, SK_W, SK_H, direction='down', n_steps=8)
label(23, 45, 'STAIR DN\nTO KITCHEN', size=5.5)

# --- Entry from hall arrow ---
ax.annotate('', xy=(30, 33), xytext=(26, 33),
            arrowprops=dict(arrowstyle='->', color='#1155CC', lw=2), zorder=5)
label(26.5, 34.5, 'FROM\nHALL', size=5, bold=False, color='#1155CC')

# --- Entry / T-intersection arrows (red) ---
label(CENTRAL_X + 6, 23, 'ENTRY', size=5.5, bold=True, color='#CC3333')
ax.annotate('', xy=(CENTRAL_X + 3, 22), xytext=(CENTRAL_X + 3, 24.5),
            arrowprops=dict(arrowstyle='->', color='red', lw=1.5), zorder=5)
ax.annotate('', xy=(CENTRAL_X + 8, 22), xytext=(CENTRAL_X + 5, 22),
            arrowprops=dict(arrowstyle='->', color='red', lw=1.5), zorder=5)

# ============================================================
# DIMENSIONS (outside building)
# ============================================================

# Top dimension - full width
ax.annotate('', xy=(0, BH + 3), xytext=(CENTRAL_X, BH + 3),
            arrowprops=dict(arrowstyle='<->', color='black', lw=1), zorder=5)
dim_label(CENTRAL_X / 2, BH + 4, "26'-0\"", size=6)

ax.annotate('', xy=(CENTRAL_X, BH + 3), xytext=(BW, BH + 3),
            arrowprops=dict(arrowstyle='<->', color='black', lw=1), zorder=5)
dim_label((CENTRAL_X + BW) / 2, BH + 4, "30'-0\"", size=6)

# Bottom dimension - full width
ax.annotate('', xy=(0, -2.5), xytext=(BW, -2.5),
            arrowprops=dict(arrowstyle='<->', color='black', lw=1), zorder=5)
dim_label(BW / 2, -3.5, "56'-0\"", size=6)

# Left side dimension
ax.annotate('', xy=(-3.5, 0), xytext=(-3.5, BH),
            arrowprops=dict(arrowstyle='<->', color='black', lw=1), zorder=5)
dim_label(-5, BH / 2, "50'-0\"", size=6, rotation=90)

# ============================================================
# SIDE LABELS
# ============================================================
label(13, BH + 6, '<-- OFFICE SIDE (3 BEDROOMS) -->', size=8, color='#CC3333')
label(41, BH + 6, '<-- DORMS SIDE (MASTER SUITE) -->', size=8, color='#CC3333')

# ============================================================
# CENTRAL WALL NOTE
# ============================================================
dim_label(BW / 2, -5.5, '~ CENTRAL LOAD-BEARING WALL ~', size=6)
# Arrow pointing to central wall
ax.annotate('', xy=(CENTRAL_X, -4.5), xytext=(CENTRAL_X - 3, -5.5),
            arrowprops=dict(arrowstyle='->', color='#555', lw=1), zorder=5)

# Note about interior dividing wall
label(BW / 2, -1.5, '" INTERIOR DIVIDING WALL (not load-bearing) "', size=5.5, bold=False, color='#555')

# ============================================================
# RED ARROWS note (top right)
# ============================================================
label(44, BH + 7.2, 'RED ARROWS: Entry → T-intersection → His (left) or Her (right) → Bathroom',
      size=4.5, bold=False, color='#CC3333')

# ============================================================
# NORTH ARROW
# ============================================================
na_x, na_y = BW + 5, BH + 4
ax.annotate('N', xy=(na_x, na_y + 3), fontsize=12, fontweight='bold', ha='center', va='bottom')
ax.annotate('', xy=(na_x, na_y + 3), xytext=(na_x, na_y),
            arrowprops=dict(arrowstyle='->', color='black', lw=2.5), zorder=5)

# ============================================================
# SCALE BAR
# ============================================================
sb_x, sb_y = -5, -10
for i in range(3):
    color = 'black' if i % 2 == 0 else 'white'
    rect = patches.Rectangle((sb_x + i * 5, sb_y), 5, 0.8,
                               linewidth=1, edgecolor='black', facecolor=color, zorder=4)
    ax.add_patch(rect)
label(sb_x, sb_y - 1, "0'", size=5, bold=False)
label(sb_x + 5, sb_y - 1, "5'", size=5, bold=False)
label(sb_x + 10, sb_y - 1, "10'-0\"", size=5, bold=False)

# ============================================================
# TITLE BLOCK
# ============================================================
TB_Y = -8
# Title block box
tb_x, tb_y = 15, -13
tb_w, tb_h = 40, 6
draw_rect(tb_x, tb_y, tb_w, tb_h, lw=2, color='black')
fill = patches.Rectangle((tb_x, tb_y), tb_w, tb_h, facecolor='white', edgecolor='none', zorder=1)
ax.add_patch(fill)

label(tb_x + tb_w/2, tb_y + 4.8, 'PROPOSED SECOND FLOOR PLAN — V13', size=11)
label(tb_x + tb_w/2, tb_y + 3.3, 'FIREHOUSE — 18 NEW ST, CROSSWICKS, NJ 08515', size=8)
label(tb_x + tb_w/2, tb_y + 2, 'Block 300, Lot 12  |  Old School Firehouse LLC', size=5.5, bold=False, color='#555')
dim_label(tb_x + tb_w/2, tb_y + 1, 'Scale: 1/4" = 1\'-0"  |  Date: March 29, 2026', size=5.5)

# PROPOSED NOT FOR CONSTRUCTION stamp
stamp = patches.FancyBboxPatch((tb_x + 10, tb_y + 0.1), 20, 0.7,
                                boxstyle="round,pad=0.1", linewidth=1.5,
                                edgecolor='red', facecolor='#FFE0E0', zorder=5)
ax.add_patch(stamp)
label(tb_x + 20, tb_y + 0.45, 'PROPOSED — NOT FOR CONSTRUCTION', size=5.5, color='red')

# Drawing number
dim_label(tb_x + tb_w - 3, tb_y + 0.45, 'Drawing A-201  |  Sheet 1 of 1', size=4.5)

# ============================================================
# LEGEND
# ============================================================
leg_x, leg_y = -5, -8
label(leg_x, leg_y, 'LEGEND:', size=6)

# Exterior wall
ax.plot([leg_x, leg_x + 4], [leg_y - 1.2, leg_y - 1.2], color='black', linewidth=4, zorder=4)
label(leg_x + 6, leg_y - 1.2, 'Exterior wall', size=5, bold=False, ha='left')

# Interior dividing wall
ax.plot([leg_x, leg_x + 4], [leg_y - 2.2, leg_y - 2.2], color='black', linewidth=2.5, zorder=4)
label(leg_x + 6, leg_y - 2.2, 'Interior dividing wall', size=5, bold=False, ha='left')

# Interior partition
ax.plot([leg_x, leg_x + 4], [leg_y - 3.2, leg_y - 3.2], color='black', linewidth=1.2, zorder=4)
label(leg_x + 6, leg_y - 3.2, 'Interior partition wall', size=5, bold=False, ha='left')

# Circulation arrow
ax.annotate('', xy=(leg_x + 4, leg_y - 4.2), xytext=(leg_x, leg_y - 4.2),
            arrowprops=dict(arrowstyle='->', color='red', lw=1.5), zorder=5)
label(leg_x + 6, leg_y - 4.2, 'Circulation / entry flow', size=5, bold=False, ha='left')

# ============================================================
# SAVE
# ============================================================
plt.tight_layout(pad=1)

output_base = '/Users/joemac/.openclaw/workspace/projects/firehouse/firehouse-2nd-floor-v13'
fig.savefig(f'{output_base}.png', dpi=300, bbox_inches='tight',
            facecolor='white', edgecolor='none')
fig.savefig(f'{output_base}.pdf', bbox_inches='tight',
            facecolor='white', edgecolor='none')

print(f"✅ Saved PNG: {output_base}.png")
print(f"✅ Saved PDF: {output_base}.pdf")
plt.close()
