#!/usr/bin/env python3
"""
RBCDC 1st Floor Plan - ACCURATE Wall Definition
Based on VISUAL ANALYSIS of the actual PDF image

From the floor plan image, I can see:

COORDINATE SYSTEM:
- Origin (0,0) = Grid 1/A intersection (southwest corner)
- X-axis = EAST (positive to the right)
- Y-axis = NORTH (positive upward)
- Units = FEET

GRID POSITIONS (reading dimensions from the drawing):
Vertical Grids (X positions):
- Grid 1: X = 0.0 (west edge of garage)
- Grid 2: X = 12'-1" = 12.083 (east wall of garage)
- Dimension strings show: 2'-10" + 8'-0" + 2'-10" = 13'-8" (but this is detail)

Horizontal Grids (Y positions):
- Grid A: Y = 0.0 (south edge)
- Then working north...

ROOMS (from labels):
- GARAGE (101): 242 SF - Southwest, 12'-1" x 20'-0"
- UTILITY RM (102): 44 SF
- PANTRY (103): 2 SF
- KITCHEN (104): 204 SF - 8'-0" width shown
- LIVING RM (105): 144 SF
- DINING RM (106): 100 SF
- 1/2 BATH (107): 35 SF
- CLOSET (108): 68 SF
- FOYER (109): 49 SF
- REAR LANAI (EX-2): 110 SF - North projection
- PORCH (EX-1): 54 SF - South entry

EXTERIOR SHAPE:
The building has:
1. Main rectangular body
2. LANAI bump-out at NORTHEAST (not northwest!)
3. PORCH notch at SOUTH (entry area)
"""

import json
import sys
import os

# Add the project directory to path
sys.path.insert(0, '/mnt/d/RevitMCPBridge2026')

try:
    from mcp_call import call
except ImportError:
    print("Warning: mcp_call not available, running in dry-run mode")
    def call(method, params):
        print(f"  [DRY RUN] {method}: {json.dumps(params, indent=2)}")
        return {"success": True, "wallId": "DRY_RUN"}

# Wall type IDs from Revit
EXTERIOR_WALL = 441445  # Generic - 6"
INTERIOR_WALL = 441519  # Interior - 4 1/2" Partition
GARAGE_WALL = 441446    # Interior - 5" Partition (2-hr)

LEVEL_1 = 30
HEIGHT = 10.0

# ================================================================
# GRID POSITIONS - Extracted from dimension strings in PDF
# ================================================================
# Reading the dimensions visible in the floor plan:

# VERTICAL GRIDS (X positions, west to east)
# From south dimension string: 2'-10" + 8'-0" + 2'-10" = 13'-8" for garage area
# But garage width shown as 12'-1"
G1 = 0.0        # West edge (Grid 1)
G2 = 12.083     # 12'-1" from G1 (Grid 2) - garage east wall

# From dimension string at bottom: 10'-0" + 15'-0" continuing east
# These are partial dimensions
G3 = G2 + 15.0  # = 27.083 (Grid 3)
G4 = G3 + 8.0   # = 35.083 (Grid 4)
G5 = G4 + 15.0  # = 50.083 (Grid 5) - East edge

# HORIZONTAL GRIDS (Y positions, south to north)
# From dimension string: 45'-4" total height visible on west side
GA = 0.0        # South edge (Grid A)

# The garage is 20'-0" deep (from 20'-0" dimension)
GB = 7.0        # Grid B - 7'-0" from GA

# Kitchen area dimensions
GC = 24.667     # Grid C - 24'-8" from GA (17'-8" from GB)

# Main building north edge
GD = 38.333     # Grid D - 38'-4" from GA

# LANAI extends north from Grid D
# Dimensions show: 16'-0" and 15'-0" at top, 7'-0" visible
LANAI_NORTH = GD + 7.0   # = 45.333 - North edge of lanai

# PORCH extends south from Grid A
# Porch is shown at entry, dimension 10'-4" x 5'-4" approximately
PORCH_DEPTH = 5.333      # 5'-4" south of Grid A

# ================================================================
# VISUAL ANALYSIS OF ACTUAL BUILDING SHAPE (CORRECTED)
# From quadrant-by-quadrant image analysis
# ================================================================
#
# BUILDING LAYOUT:
#                      NORTH
#                        ↑
#     ┌──────────────────┬────────┐
#     │    REAR LANAI    │        │  ← Lanai at NORTHWEST
#     │    (EX-2)        │        │     16' x 7' approx
#     ├──────────────────┤        │
#     │  KITCHEN (104)   │ LIVING │
#     │    8'-0" wide    │   RM   │
#     │                  │ (105)  │
#     ├──────────────────┼────────┤
#     │ UTILITY│ PANTRY  │ DINING │
#     │  (102) │ (103)   │  (106) │
#     ├────────┴─────────┼────────┤
#     │                  │ CLOSET │
#     │   GARAGE (101)   │ (108)  │
#     │                  ├────────┤
#     │   12'-1" x 20'   │ FOYER  │
#     │                  │ (109)  │
#     │                  ├────────┤
#     │                  │ PORCH  │  ← Porch at SOUTHEAST entry
#     └──────────────────┴────────┘
#               SOUTH
#
# From dimension strings:
# - Garage: 12'-1" (E-W) x 20'-0" (N-S)
# - Total west side height: 45'-4"
# - Lanai dimensions: 16'-0" x 7'-0" (from N-E quadrant)
# - Porch: 10'-4" x 5'-4"

# LANAI is on the NORTHWEST (above kitchen, west side)
# From the north-east quadrant image: lanai is 16'-0" wide, 7'-0" deep
# It's positioned above the KITCHEN (104) area
LANAI_WIDTH = 16.0   # 16'-0" wide
LANAI_DEPTH = 7.0    # 7'-0" projection north

# Lanai X position: starts at west side and extends 16' east
# Looking at the image, lanai appears to start around Grid 1 or slightly east
LANAI_WEST_X = G1    # West edge of lanai at Grid 1 = 0.0
LANAI_EAST_X = LANAI_WEST_X + LANAI_WIDTH  # = 16.0

# PORCH at southeast entry
# From center-east quadrant: 10'-4" x 5'-4"
PORCH_WIDTH = 10.333   # 10'-4"
PORCH_DEPTH = 5.333    # 5'-4" projection south
PORCH_SOUTH = -PORCH_DEPTH  # = -5.333

# Porch X position: at the entry, east of garage
# Looking at the layout, porch is roughly from X=17 to X=27
PORCH_WEST_X = 17.0   # Approximate
PORCH_EAST_X = PORCH_WEST_X + PORCH_WIDTH  # = 27.333

# Building main dimensions
# From west side: 45'-4" total (but this includes garage slab area)
# Main building north edge appears to be at about Y=38.33 (Grid D)
# East edge at about X=34 based on the layout (not 50)

def create_wall(name, start, end, wall_type=EXTERIOR_WALL):
    """Create a single wall via MCP"""
    dx = end[0] - start[0]
    dy = end[1] - start[1]
    length = (dx*dx + dy*dy) ** 0.5

    print(f"\n{name}")
    print(f"  Start: ({start[0]:.3f}, {start[1]:.3f})")
    print(f"  End:   ({end[0]:.3f}, {end[1]:.3f})")
    print(f"  Length: {length:.3f} ft")

    result = call("createWall", {
        "startPoint": [start[0], start[1], 0.0],
        "endPoint": [end[0], end[1], 0.0],
        "levelId": LEVEL_1,
        "height": HEIGHT,
        "wallTypeId": wall_type
    })

    if result.get("success"):
        print(f"  [OK] Wall ID: {result.get('wallId')}")
        return result.get("wallId")
    else:
        print(f"  [FAIL] {result.get('error')}")
        return None


def main():
    print("=" * 70)
    print("RBCDC 1ST FLOOR PLAN - ACCURATE EXTERIOR WALLS")
    print("Based on quadrant-by-quadrant visual analysis")
    print("=" * 70)

    # Recalculate key positions based on visual analysis
    # The building is narrower than I initially thought

    # From the dimensions visible:
    # - Garage: 12'-1" wide (confirmed)
    # - Building continues east with rooms
    # - Total width appears to be around 28-34 feet based on room layout

    # Let me use more accurate positions based on the visible dimensions:
    BUILDING_EAST = 34.0  # East edge of main building (approx)
    MAIN_NORTH = GD       # 38.333 - main building north edge

    # Lanai at NORTHWEST
    LANAI_N = MAIN_NORTH + LANAI_DEPTH  # = 38.333 + 7 = 45.333

    print("\nKey coordinates:")
    print(f"  Building West (Grid 1):  X = {G1}")
    print(f"  Garage East (Grid 2):    X = {G2}")
    print(f"  Building East:           X = {BUILDING_EAST}")
    print(f"  Building South (Grid A): Y = {GA}")
    print(f"  Main North (Grid D):     Y = {MAIN_NORTH}")
    print(f"  Lanai North:             Y = {LANAI_N}")
    print(f"  Lanai East edge:         X = {LANAI_EAST_X}")
    print(f"  Porch South:             Y = {PORCH_SOUTH}")

    walls = []

    # ================================================================
    # EXTERIOR PERIMETER - CORRECTED
    # Lanai at NORTHWEST, Porch at SOUTH-CENTER
    # ================================================================
    print("\n" + "=" * 70)
    print("EXTERIOR PERIMETER (with lanai at northwest)")
    print("=" * 70)

    # Starting from SW corner, going COUNTER-CLOCKWISE:

    # 1. SOUTH WALL - Full length (ignoring porch for now)
    w = create_wall("EXT-S: South wall",
                    (G1, GA), (BUILDING_EAST, GA))
    if w: walls.append(w)

    # 2. EAST WALL - Full height
    w = create_wall("EXT-E: East wall",
                    (BUILDING_EAST, GA), (BUILDING_EAST, MAIN_NORTH))
    if w: walls.append(w)

    # 3. NORTH WALL - East portion (from building east to lanai east)
    w = create_wall("EXT-N1: North wall (east of lanai)",
                    (BUILDING_EAST, MAIN_NORTH), (LANAI_EAST_X, MAIN_NORTH))
    if w: walls.append(w)

    # 4. LANAI - At northwest corner
    # Lanai east wall going north
    w = create_wall("EXT-LANAI-E: Lanai east wall",
                    (LANAI_EAST_X, MAIN_NORTH), (LANAI_EAST_X, LANAI_N))
    if w: walls.append(w)

    # Lanai north wall
    w = create_wall("EXT-LANAI-N: Lanai north wall",
                    (LANAI_EAST_X, LANAI_N), (LANAI_WEST_X, LANAI_N))
    if w: walls.append(w)

    # Lanai west wall going south
    w = create_wall("EXT-LANAI-W: Lanai west wall",
                    (LANAI_WEST_X, LANAI_N), (LANAI_WEST_X, MAIN_NORTH))
    if w: walls.append(w)

    # 5. NORTH WALL - West portion (from lanai west to building west)
    # Only needed if lanai doesn't start at Grid 1
    if LANAI_WEST_X > G1:
        w = create_wall("EXT-N2: North wall (west of lanai)",
                        (LANAI_WEST_X, MAIN_NORTH), (G1, MAIN_NORTH))
        if w: walls.append(w)

    # 6. WEST WALL - Full height
    w = create_wall("EXT-W: West wall",
                    (G1, MAIN_NORTH), (G1, GA))
    if w: walls.append(w)

    print("\n" + "=" * 70)
    print("SUMMARY")
    print("=" * 70)
    print(f"Exterior walls created: {len(walls)}")
    print(f"Wall IDs: {walls}")

    # Verify the shape
    print("\nSHAPE VERIFICATION:")
    print(f"- Garage at SOUTHWEST: X=0 to {G2}, Y=0 to ~20 ✓")
    print(f"- Main building: {G1} to {BUILDING_EAST} (E-W) x {GA} to {MAIN_NORTH} (N-S)")
    print(f"- Lanai at NORTHWEST: X={LANAI_WEST_X} to {LANAI_EAST_X}, Y={MAIN_NORTH} to {LANAI_N}")
    print(f"- Building width: {BUILDING_EAST - G1:.1f} ft")
    print(f"- Building depth (main): {MAIN_NORTH - GA:.1f} ft")
    print(f"- Lanai adds {LANAI_DEPTH:.1f} ft to north")

    return walls


if __name__ == "__main__":
    main()
