// SAME Analog Modular Ecosystem
//
// / SPDX-FileCopyrightText: 2026 Denis Chevalier <perso@denischevalier.fr>
// / SPDX-License-Identifier: CC-BY-SA-4.0
// / SPDX-License-Identifier: CERN-OHL-S-2.0+
// / SPDX-License-Identifier: GPL-3.0-or-later
//
// The prose, explanatory text, rendered figures, tables, and mathematical
// content of this specification are licensed under CC BY-SA 4.0. If a later
// version of CC BY-SA is published, the author grants permission to distribute
// this work under that later version as well.
//
// Hardware designs contained herein (schematics, PCB layouts, mechanical
// drawings, and CAD models) are licensed under CERN-OHL-S-2.0+.
//
// All executable code, helper libraries (`lib/*`), metrology assertions, and
// embedded verification scripts throughout the source documents are licensed
// under the GNU General Public License v3.0 or later (GPL-3.0-or-later).

#import "../lib/unify.typ": qty
#import "@preview/cetz:0.5.2"

#figure(
  cetz.canvas({
    import cetz.draw: *

    // Scale factor: 1 mm = 0.030 drawing units
    let s = 0.030

    let h = 165.1 * s // 4.953
    let d = 152.4 * s // 4.572
    let w = 35.7 * s // 1.071
    let f = 8.0 * s // 0.240 (8 mm flange width)

    let db25-y = 55.0 * s // 1.650
    let bnc-y = 130.0 * s // 3.900
    let db25-x = 17.85 * s // 0.5355 (centered in 35.7 mm width)

    let y-bot = 0
    let y-top = h

    // ==========================================
    // 1. LEFT ELEVATION (Left Wall: 152.4 x 165.1 mm)
    // ==========================================
    let x-left-start = 0
    let x-left-end = x-left-start + d
    let x-left-mid = (x-left-start + x-left-end) / 2

    // View Title
    content((x-left-mid, y-top + 0.8), text(
      size: 8.5pt,
      weight: "bold",
    )[LEFT ELEVATION])

    // Main Left Wall Sheet
    rect(
      (x-left-start, y-bot),
      (x-left-end, y-top),
      fill: luma(245),
      stroke: 1.2pt + black,
    )

    // Folded Flanges (Top, Bottom, Front folded 90 deg inward - shown as dashed lines)
    // Top Flange
    line(
      (x-left-start, y-top - f),
      (x-left-end, y-top - f),
      stroke: (paint: luma(120), thickness: 0.7pt, dash: "dashed"),
    )
    // Bottom Flange
    line(
      (x-left-start, y-bot + f),
      (x-left-end, y-bot + f),
      stroke: (paint: luma(120), thickness: 0.7pt, dash: "dashed"),
    )
    // Front Flange (at x-left-start)
    line(
      (x-left-start + f, y-bot),
      (x-left-start + f, y-top),
      stroke: (paint: luma(120), thickness: 0.7pt, dash: "dashed"),
    )

    // PEM Nut Press-Fit Inserts on Flanges (Representative circles)
    circle(
      (x-left-start + f / 2, y-bot + 12 * s),
      radius: 0.06,
      fill: white,
      stroke: 0.6pt + black,
    )
    circle(
      (x-left-start + f / 2, y-top - 12 * s),
      radius: 0.06,
      fill: white,
      stroke: 0.6pt + black,
    )
    circle(
      (x-left-mid, y-bot + f / 2),
      radius: 0.06,
      fill: white,
      stroke: 0.6pt + black,
    )
    circle(
      (x-left-mid, y-top - f / 2),
      radius: 0.06,
      fill: white,
      stroke: 0.6pt + black,
    )

    // Dimensions: Depth & Height
    line(
      (x-left-start, y-bot),
      (x-left-start, y-bot - 0.6),
      stroke: 0.4pt + luma(140),
    )
    line(
      (x-left-end, y-bot),
      (x-left-end, y-bot - 0.6),
      stroke: 0.4pt + luma(140),
    )
    line(
      (x-left-start, y-bot - 0.6),
      (x-left-end, y-bot - 0.6),
      mark: (start: "stealth", end: "stealth"),
      mark-size: 0.10,
      stroke: 0.6pt + black,
    )
    content((x-left-mid, y-bot - 0.9), text(size: 7pt)[$qty(152.4, "mm")$])

    line(
      (x-left-start, y-bot),
      (x-left-start - 0.6, y-bot),
      stroke: 0.4pt + luma(140),
    )
    line(
      (x-left-start, y-top),
      (x-left-start - 0.6, y-top),
      stroke: 0.4pt + luma(140),
    )
    line(
      (x-left-start - 0.6, y-bot),
      (x-left-start - 0.6, y-top),
      mark: (start: "stealth", end: "stealth"),
      mark-size: 0.10,
      stroke: 0.6pt + black,
    )
    content(
      (x-left-start - 0.9, (y-bot + y-top) / 2),
      text(size: 7pt)[$qty(165.1, "mm")$],
      angle: 90deg,
    )

    // ==========================================
    // 2. REAR ELEVATION (Rear Closure: 35.7 x 165.1 mm)
    // ==========================================
    let x-rear-start = 5.8
    let x-rear-end = x-rear-start + w
    let x-rear-mid = (x-rear-start + x-rear-end) / 2

    // View Title
    content((x-rear-mid, y-top + 0.8), text(
      size: 8.5pt,
      weight: "bold",
    )[REAR ELEVATION])

    // Main Rear Closure Plate
    rect(
      (x-rear-start, y-bot),
      (x-rear-end, y-top),
      fill: luma(235),
      stroke: 1.2pt + black,
    )

    // Flanges (Top, Bottom, Right folded 90 deg inward)
    line((x-rear-start, y-top - f), (x-rear-end, y-top - f), stroke: (
      paint: luma(120),
      thickness: 0.7pt,
      dash: "dashed",
    ))
    line((x-rear-start, y-bot + f), (x-rear-end, y-bot + f), stroke: (
      paint: luma(120),
      thickness: 0.7pt,
      dash: "dashed",
    ))
    line((x-rear-end - f, y-bot), (x-rear-end - f, y-top), stroke: (
      paint: luma(120),
      thickness: 0.7pt,
      dash: "dashed",
    ))

    // DB-25 Cutout at Y = 55.0 mm (Centered)
    let db25-w = 26.0 * s
    let db25-h = 11.0 * s
    rect(
      (x-rear-start + db25-x - db25-w / 2, db25-y - db25-h / 2),
      (x-rear-start + db25-x + db25-w / 2, db25-y + db25-h / 2),
      fill: white,
      stroke: 0.9pt + black,
    )
    content((x-rear-start + db25-x, db25-y), text(
      size: 5.5pt,
      weight: "bold",
    )[DB-25])

    // BNC Cutout at Y = 130.0 mm (Centered, Facultative)
    let bnc-r = 6.0 * s
    circle(
      (x-rear-start + db25-x, bnc-y),
      radius: bnc-r,
      fill: white,
      stroke: 0.9pt + black,
    )
    content((x-rear-start + db25-x, bnc-y), text(
      size: 5pt,
      weight: "bold",
    )[BNC])

    // Cutout Centerline Indicators & Heights
    line((x-rear-start - 0.2, db25-y), (x-rear-end + 0.6, db25-y), stroke: (
      paint: luma(160),
      thickness: 0.4pt,
      dash: "dash-dotted",
    ))
    line((x-rear-start - 0.2, bnc-y), (x-rear-end + 0.6, bnc-y), stroke: (
      paint: luma(160),
      thickness: 0.4pt,
      dash: "dash-dotted",
    ))

    // Width Dimension (35.7 mm)
    line(
      (x-rear-start, y-bot),
      (x-rear-start, y-bot - 0.6),
      stroke: 0.4pt + luma(140),
    )
    line(
      (x-rear-end, y-bot),
      (x-rear-end, y-bot - 0.6),
      stroke: 0.4pt + luma(140),
    )
    line(
      (x-rear-start, y-bot - 0.6),
      (x-rear-end, y-bot - 0.6),
      mark: (start: "stealth", end: "stealth"),
      mark-size: 0.10,
      stroke: 0.6pt + black,
    )
    content((x-rear-mid, y-bot - 0.9), text(size: 7pt)[$qty(35.7, "mm")$])

    // Height Dimensions: DB-25 (55 mm) & BNC (130 mm)
    line(
      (x-rear-end, y-bot),
      (x-rear-end + 0.6, y-bot),
      stroke: 0.4pt + luma(140),
    )
    line(
      (x-rear-end + 0.6, y-bot),
      (x-rear-end + 0.6, db25-y),
      mark: (start: "stealth", end: "stealth"),
      mark-size: 0.10,
      stroke: 0.6pt + black,
    )
    content(
      (x-rear-end + 0.9, db25-y / 2),
      text(size: 6.5pt)[$qty(55.0, "mm")$],
      angle: -90deg,
    )

    line(
      (x-rear-end + 0.6, db25-y),
      (x-rear-end + 0.6, bnc-y),
      mark: (start: "stealth", end: "stealth"),
      mark-size: 0.10,
      stroke: 0.6pt + black,
    )
    content(
      (x-rear-end + 0.9, (db25-y + bnc-y) / 2),
      text(size: 6.5pt)[$qty(130.0, "mm")$],
      angle: -90deg,
    )

    // ==========================================
    // 3. ISOMETRIC VIEW (3D Sheet Metal Structure)
    // ==========================================
    let iso-x0 = 12.5
    let iso-y0 = -1

    // Helper function for isometric projection
    // X = Width (0..35.7 mm), Y = Height (0..165.1 mm), Z = Depth (0..152.4 mm)
    let p(x-mm, y-mm, z-mm) = {
      let px = iso-x0 + (x-mm * 0.866 - z-mm * 0.866) * s
      let py = iso-y0 + (y-mm + x-mm * 0.5 + z-mm * 0.5) * s
      return (px, py)
    }

    // View Title
    content((iso-x0, y-top + 0.8), text(
      size: 8.5pt,
      weight: "bold",
    )[ISOMETRIC VIEW])

    // Left Wall Outer Face (Z=0..152.4, Y=0..165.1, X=0)
    line(
      p(0, 0, 0),
      p(0, 0, 152.4),
      p(0, 165.1, 152.4),
      p(0, 165.1, 0),
      close: true,
      fill: luma(240),
      stroke: 1pt + black,
    )

    // Rear Closure Outer Face (X=0..35.7, Y=0..165.1, Z=0)
    line(
      p(0, 0, 0),
      p(35.7, 0, 0),
      p(35.7, 165.1, 0),
      p(0, 165.1, 0),
      close: true,
      fill: luma(225),
      stroke: 1pt + black,
    )

    // Folded Inward Flanges (8 mm width)
    // Left Wall Top Flange
    line(
      p(0, 165.1, 0),
      p(0, 165.1, 152.4),
      p(8.0, 165.1, 152.4),
      p(8.0, 165.1, 0),
      close: true,
      fill: luma(210),
      stroke: 0.8pt + black,
    )

    // Left Wall Front Flange
    line(
      p(0, 0, 152.4),
      p(0, 165.1, 152.4),
      p(8.0, 165.1, 152.4),
      p(8.0, 0, 152.4),
      close: true,
      fill: luma(215),
      stroke: 0.8pt + black,
    )

    // Rear Wall Bottom Flange
    line(
      p(0, 0, 0),
      p(0, 0, 8.0),
      p(35.7, 0, 8.0),
      p(35.7, 0, 0.0),
      close: true,
      fill: luma(200),
      stroke: 0.8pt + black,
    )

    // Left Wall Bottom Flange
    line(
      p(0, 0, 0),
      p(0, 0, 152.4),
      p(8.0, 0, 152.4),
      p(8.0, 0, 8),
      close: true,
      fill: luma(210),
      stroke: 0.8pt + black,
    )

    // Rear Wall Right Flange
    line(
      p(35.7, 0, 0),
      p(35.7, 165.1, 0),
      p(35.7, 165.1, 8.0),
      p(35.7, 0, 8.0),
      close: true,
      fill: luma(200),
      stroke: 0.8pt + black,
    )

    // Rear Wall Top Flange
    line(
      p(0, 165.1, 0),
      p(0, 165.1, 8.0),
      p(35.7, 165.1, 8.0),
      p(35.7, 165.1, 0.0),
      close: true,
      fill: luma(200),
      stroke: 0.8pt + black,
    )

    // DB-25 Cutout on Rear Face (Z=0)
    let db-w2 = 13.0
    let db-h2 = 5.5
    line(
      p(17.85 - db-w2, 55.0 - db-h2, 0),
      p(17.85 + db-w2, 55.0 - db-h2, 0),
      p(17.85 + db-w2, 55.0 + db-h2, 0),
      p(17.85 - db-w2, 55.0 + db-h2, 0),
      close: true,
      fill: white,
      stroke: 0.8pt + black,
    )

    // BNC Cutout on Rear Face (Z=0)
    line(
      p(17.85 - 4.5, 130.0 - 4.5, 0),
      p(17.85 + 4.5, 130.0 - 4.5, 0),
      p(17.85 + 4.5, 130.0 + 4.5, 0),
      p(17.85 - 4.5, 130.0 + 4.5, 0),
      close: true,
      fill: white,
      stroke: 0.8pt + black,
    )

    // PEM Nut Threaded Insert Points (Visual markers on top/front flanges)
    circle(p(4.0, 165.1, 30.0), radius: 0.05, fill: black)
    circle(p(4.0, 165.1, 120.0), radius: 0.05, fill: black)
    circle(p(4.0, 0, 30.0), radius: 0.05, fill: black)
    circle(p(4.0, 0, 120.0), radius: 0.05, fill: black)
    circle(p(4.0, 30.0, 152.4), radius: 0.05, fill: black)
    circle(p(4.0, 135.0, 152.4), radius: 0.05, fill: black)
    circle(p(35.7 - 4.0, 30.0, 0), radius: 0.05, fill: black)
    circle(p(35.7 - 4.0, 135.0, 0), radius: 0.05, fill: black)
    circle(p(17.85, 165.1, 4.0), radius: 0.05, fill: black)
    circle(p(17.85, 0, 4.0), radius: 0.05, fill: black)

    // Masked Unpainted Area Callout Annotation
    line(
      p(4.0, 165.1, 75.0),
      (iso-x0 + 1.2, y-top),
      stroke: 0.5pt + black,
    )
    content(
      (iso-x0 + 1.25, y-top - 0.3),
      text(size: 6.5pt)[Unpainted EMI flange \
        contact area (PEM nuts \
        fitted)],
      anchor: "south-west",
    )
  }),
  caption: [Cassette L-Channel structural spine technical drawings showing orthographic projections (left and rear elevations) and 3D isometric sheet metal form],
) <figure-cassette-lchannel-diagram>
