"""Detailed articulated contemporary studio couch daybed."""
import math
import os
import sys

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import v4lib as v4

v4.reset()

# Upholstery, wood, and hardware palette.
linen = v4.fabric("oatmeal_performance_linen", (0.68, 0.61, 0.51), weave_scale=210)
linen_light = v4.fabric("cushion_linen", (0.76, 0.69, 0.59), weave_scale=240)
linen_side = v4.fabric("mattress_linen", (0.58, 0.52, 0.44), weave_scale=190)
oak = v4.wood("natural_white_oak", light=(0.73, 0.59, 0.39), dark=(0.42, 0.28, 0.15), grain_scale=5)
oak_end = v4.wood("oak_endgrain", light=(0.65, 0.48, 0.28), dark=(0.36, 0.22, 0.11), grain_scale=9)
steel = v4.metal("graphite_powder_coat", color=(0.16, 0.18, 0.19), roughness=0.36, brushed=False)
zinc = v4.metal("satin_zinc_hardware", color=(0.62, 0.66, 0.67), roughness=0.31, brushed=True)
thread = v4.mat("warm_taupe_thread", (0.39, 0.33, 0.27), roughness=0.9)
rubber = v4.rubber("dark_grey_tire", (0.055, 0.06, 0.06))
felt = v4.rubber("foot_glide", (0.12, 0.12, 0.11))
label_mat = v4.mat("woven_label_ink", (0.20, 0.17, 0.13), roughness=0.8)

# Main sofa structure: 2.02 m wide, with a low 0.50 m seat deck.
base = v4.body("sofa_base")
W = 2.02
side_x = 0.90
deck_z = 0.455

# Oak inner frame beneath the deck. Rail members meet edge-to-edge.
v4.box("frame_front_rail", (1.72, 0.055, 0.095), (0, -0.335, 0.405), bevel=0.008, material=oak, body=base)
v4.box("frame_rear_rail", (1.72, 0.055, 0.095), (0, 0.335, 0.405), bevel=0.008, material=oak, body=base)
for sx in (-1, 1):
    v4.box(f"frame_side_rail_{sx}", (0.055, 0.615, 0.095), (sx * 0.833, 0, 0.405), bevel=0.007, material=oak, body=base)
    # Three stout cross-members carry the plywood deck and guide hardware.
for y in (-0.20, 0.0, 0.20):
    v4.box(f"deck_crossmember_{int((y+0.2)*100)}", (1.64, 0.042, 0.045), (0, y, 0.438), bevel=0.004, material=oak_end, body=base)
v4.box("plywood_seat_deck", (1.78, 0.70, 0.026), (0, 0, deck_z), bevel=0.008, material=oak, body=base)

# Four gently tapered solid oak legs and dark floor-saving glides.
for i, (sx, sy) in enumerate(((-1, -1), (1, -1), (-1, 1), (1, 1))):
    x, y = sx * side_x, sy * 0.305
    v4.lathe(f"turned_oak_foot_{i}", [(0, 0.006), (0.031, 0.006), (0.033, 0.020),
             (0.041, 0.075), (0.050, 0.31), (0.056, 0.38), (0, 0.38)],
             loc=(x, y, 0), material=oak_end, body=base)
    v4.cylinder(f"foot_glide_{i}", 0.032, 0.006, (x, y, 0.003), bevel=0.001,
                material=felt, body=base)
    # Small dark ferrule at the top of each tapered leg.
    v4.cylinder(f"leg_ferrule_{i}", 0.053, 0.018, (x, y, 0.365), bevel=0.002,
                material=steel, body=base)

# Low, softly squared upholstered arms, sitting directly on the side structure.
for sx, label in ((-1, "left"), (1, "right")):
    x = sx * 0.925
    v4.box(f"arm_inner_support_{label}", (0.115, 0.73, 0.32), (x, -0.005, 0.565), bevel=0.028,
           material=oak, body=base)
    v4.box(f"upholstered_arm_{label}", (0.19, 0.79, 0.39), (x, -0.005, 0.675), bevel=0.075,
           material=linen, body=base)
    v4.box(f"arm_cap_{label}", (0.17, 0.70, 0.026), (x, -0.005, 0.873), bevel=0.012,
           material=linen_light, body=base)
    # Subtle piping traces the arm's front face perimeter.
    xf = x - sx * 0.096
    v4.tube(f"arm_front_welt_{label}", [(xf, -0.345, 0.52), (xf, 0.335, 0.52),
              (xf, 0.335, 0.82), (xf, -0.345, 0.82)], 0.002, material=linen_light, body=base, cyclic=True)

# Two seat cushions with rounded corners, tailored seams, and a narrow center gap.
for sx, label in ((-1, "left"), (1, "right")):
    cx = sx * 0.438
    v4.box(f"seat_cushion_{label}", (0.858, 0.704, 0.185), (cx, -0.012, 0.560), bevel=0.042,
           material=linen_light, body=base)
    # Welt around cushion side just above the deck.
    z = 0.502
    x0, x1 = cx - 0.405, cx + 0.405
    y0, y1 = -0.343, 0.319
    v4.tube(f"seat_welt_{label}", [(x0, y0, z), (x1, y0, z), (x1, y1, z), (x0, y1, z)],
            0.002, material=thread, body=base, cyclic=True)
    # Two restrained top stitch lines per seat pad.
    for stitch_i, xx in enumerate((cx - 0.25, cx + 0.25)):
        v4.tube(f"seat_top_stitch_{label}_{stitch_i}", [(xx, -0.30, 0.653), (xx, 0.27, 0.653)],
                0.0012, material=thread, body=base)

# The hinge pin line sits inside the frame's rear upholstery seam.
hinge_y, hinge_z = 0.515, 0.650
# Two fixed hinge pedestals rise from the rear frame rail and reach the true pivot line.
for sx in (-1, 1):
    x = sx * 0.70
    v4.box(f"rear_hinge_post_{sx}", (0.045, 0.045, 0.22), (x, 0.355, 0.535), bevel=0.004,
           material=steel, body=base)
    v4.rod(f"rear_hinge_reach_{sx}", (x, 0.355, 0.56), (x, hinge_y, hinge_z), 0.010,
           material=steel, body=base)

# Decorative maker tag at the front edge of the fixed frame.
v4.box("maker_tag", (0.12, 0.002, 0.035), (0, -0.364, 0.405), bevel=0.003, material=linen_side, body=base)
v4.text("maker_mark", "N O R D", 0.013, loc=(0, -0.366, 0.405), rot=(math.pi / 2, 0, 0),
        extrude_depth=0.00025, material=label_mat, body=base)

# Drop-in/reclining upholstered backrest on a true crosswise hinge.
back = v4.body("backrest")
v4.box("backrest_upholstered_core", (1.57, 0.175, 0.50), (0, 0.4275, 0.880), bevel=0.065,
       material=linen, body=back)
# Rear inset upholstery panel adds a tailored back surface without obscuring the cushion silhouette.
v4.box("backrest_rear_panel", (1.43, 0.014, 0.370), (0, 0.5215, 0.890), bevel=0.026,
       material=linen_side, body=back)
# Soft front welt around the padded panel.
front_y = 0.338
v4.tube("backrest_front_welt", [(-0.715, front_y, 0.650), (0.715, front_y, 0.650),
          (0.715, front_y, 1.070), (-0.715, front_y, 1.070)], 0.0025, material=linen_light,
        body=back, cyclic=True)
# Four shallow vertical tailoring channels on the sofa-facing surface.
for channel_i, x in enumerate((-0.56, -0.28, 0.0, 0.28, 0.56)):
    v4.tube(f"backrest_channel_{channel_i}", [(x, 0.341, 0.720), (x, 0.341, 1.030)],
            0.0018, material=thread, body=back)
# The padded lower edge seats against the rear seam of the fixed cushion at the hinge line.
for sx in (-1, 1):
    v4.cylinder(f"backrest_hinge_knuckle_{sx}", 0.018, 0.075, (sx * 0.70, hinge_y, hinge_z),
                rot=(0, math.pi / 2, 0), material=zinc, body=back)
v4.joint("backrest_hinge", "revolute", base, back, origin=(0, hinge_y, hinge_z), axis=(1, 0, 0),
         limits=(-math.radians(95), 0))

# Pull-out lower trundle. It stores below the deck and travels toward the front (-Y).
trundle = v4.body("trundle")
tr_z = 0.185
# A shallow one-piece steel tray gives the moving bed a rigid, low-profile support.
v4.box("trundle_platform", (1.57, 0.73, 0.09), (0, 0, 0.185), bevel=0.006,
       material=steel, body=trundle)
# Thin lower mattress, with side band and a simple quilted top treatment.
v4.box("trundle_mattress", (1.48, 0.70, 0.105), (0, -0.005, 0.278), bevel=0.035,
       material=linen_side, body=trundle)
v4.tube("trundle_mattress_welt", [(-0.70, -0.345, 0.267), (0.70, -0.345, 0.267),
          (0.70, 0.335, 0.267), (-0.70, 0.335, 0.267)], 0.0018, material=linen_light,
        body=trundle, cyclic=True)
for quilt_i, x in enumerate((-0.48, -0.16, 0.16, 0.48)):
    v4.tube(f"trundle_quilt_line_{quilt_i}", [(x, -0.29, 0.331), (x, 0.28, 0.331)],
            0.001, material=thread, body=trundle)

# Main-body slide guides and paired rollers; the visible rails flank the trundle with clearance.
for sx in (-1, 1):
    x = sx * 0.795
    v4.box(f"fixed_slide_bracket_{sx}", (0.035, 0.055, 0.18), (x, 0, 0.3325), bevel=0.005,
           material=steel, body=base)
    v4.box(f"fixed_slide_guide_{sx}", (0.018, 0.61, 0.045), (x, 0, 0.245), bevel=0.004,
           material=steel, body=base)
v4.joint("trundle_slide", "prismatic", base, trundle, origin=(0, 0, tr_z), axis=(0, -1, 0), limits=(0, 0.86))

# Four swivel caster yokes and individually spinning rubber wheels.
for i, (sx, sy) in enumerate(((-1, -1), (1, -1), (-1, 1), (1, 1))):
    cx, cy = sx * 0.712, sy * 0.322
    caster = v4.body(f"caster_{i}")
    # Swivel kingpin cap, mounting plate, and fork cheeks.
    v4.cylinder(f"caster_mount_plate_{i}", 0.035, 0.016, (cx, cy, 0.132), bevel=0.002,
                material=zinc, body=caster)
    v4.cylinder(f"caster_kingpin_{i}", 0.009, 0.025, (cx, cy, 0.117), bevel=0.001,
                material=steel, body=caster)
    wheel_y = cy - sy * 0.035
    wheel_z = 0.052
    for side in (-1, 1):
        x = cx + side * 0.028
        v4.rod(f"caster_fork_{i}_{side}", (x, cy, 0.123), (x, wheel_y, wheel_z), 0.004,
               material=steel, body=caster)
        v4.cylinder(f"caster_axle_bolt_{i}_{side}", 0.008, 0.004, (cx + side * 0.024, wheel_y, wheel_z),
                    rot=(0, math.pi / 2, 0), segments=24, bevel=0.001, material=zinc, body=caster)
    v4.joint(f"caster_swivel_{i}", "continuous", trundle, caster, origin=(cx, cy, 0.140), axis=(0, 0, 1))

    wheel = v4.body(f"wheel_{i}")
    v4.cylinder(f"rubber_wheel_{i}", 0.047, 0.040, (cx, wheel_y, wheel_z), rot=(0, math.pi / 2, 0),
                segments=48, bevel=0.004, material=rubber, body=wheel)
    # Recessed zinc hubs on both wheel cheeks.
    for side in (-1, 1):
        v4.cylinder(f"wheel_hub_{i}_{side}", 0.018, 0.002, (cx + side * 0.021, wheel_y, wheel_z),
                    rot=(0, math.pi / 2, 0), segments=32, bevel=0.001, material=zinc, body=wheel)
        v4.cylinder(f"hub_cap_{i}_{side}", 0.007, 0.003, (cx + side * 0.023, wheel_y, wheel_z),
                    rot=(0, math.pi / 2, 0), segments=24, bevel=0.001, material=steel, body=wheel)
    v4.joint(f"wheel_axle_{i}", "continuous", caster, wheel,
             origin=(cx, wheel_y, wheel_z), axis=(1, 0, 0))

v4.save(os.path.join(os.getcwd(), "asset.blend"))
print("saved asset.blend")
