// 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.050 drawing units
    let s = 0.050

    let h-total = 177.8 * s // 8.890 mm
    let h-body = 165.1 * s // 8.255 mm
    let h-lip = 6.35 * s // 0.3175 mm (6.35 mm top/bottom lips)
    let w = 38.0 * s // 1.900 mm (panel width)
    let f = 8.0 * s // 0.400 mm (8 mm L-channel flange overlap)

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

    // Helper: Draw M3 clearance hole (3.2 mm diameter)
    let draw-m3-hole(cx, cy) = {
      circle((cx, cy), radius: 1.6 * s, fill: white, stroke: 0.8pt + black)
      line((cx - 2.5 * s, cy), (cx + 2.5 * s, cy), stroke: 0.3pt + luma(120))
      line((cx, cy - 2.5 * s), (cx, cy + 2.5 * s), stroke: 0.3pt + luma(120))
    }

    // Helper: Draw M3 countersunk hole (concentric circles + crosshair)
    let draw-csink(cx, cy) = {
      circle((cx, cy), radius: 3.0 * s, fill: white, stroke: 0.8pt + black)
      circle((cx, cy), radius: 1.6 * s, fill: luma(200), stroke: 0.5pt + black)
      line((cx - 4.0 * s, cy), (cx + 4.0 * s, cy), stroke: 0.3pt + luma(120))
      line((cx, cy - 4.0 * s), (cx, cy + 4.0 * s), stroke: 0.3pt + luma(120))
    }

    // ==========================================
    // 1. FRONT ELEVATION (38.0 x 177.8 mm)
    // ==========================================
    let x-front-start = 0
    let x-front-end = x-front-start + w
    let x-front-mid = (x-front-start + x-front-end) / 2

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

    // Main Panel Outer Boundary
    rect(
      (x-front-start, y-bot),
      (x-front-end, y-top),
      fill: luma(245),
      stroke: 1.2pt + black,
    )

    // Lip Limit Boundary Indicators (Dashed horizontal lines at 6.35 mm from top/bottom)
    line(
      (x-front-start, y-lip-bot),
      (x-front-end, y-lip-bot),
      stroke: (paint: luma(110), thickness: 0.7pt, dash: "dashed"),
    )
    line(
      (x-front-start, y-lip-top),
      (x-front-end, y-lip-top),
      stroke: (paint: luma(110), thickness: 0.7pt, dash: "dashed"),
    )

    // Centered M3 Panel Mounting Holes in Top and Bottom Lips
    draw-m3-hole(x-front-mid, h-lip / 2)
    draw-m3-hole(x-front-mid, h-total - h-lip / 2)

    // L-Channel Attachment Countersunk Holes (X = 4.0 mm, Y = 30.0 mm and 135.0 mm from body start)
    let y-hole-lower = y-lip-bot + 30.0 * s
    let y-hole-upper = y-lip-bot + 135.0 * s
    let x-hole-lchannel = x-front-start + 4.0 * s

    draw-csink(x-hole-lchannel, y-hole-lower)
    draw-csink(x-hole-lchannel, y-hole-upper)

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

    // Dimension: Total Height (177.8 mm / 4U)
    line(
      (x-front-start, y-bot),
      (x-front-start - 0.6, y-bot),
      stroke: 0.4pt + luma(140),
    )
    line(
      (x-front-start, y-top),
      (x-front-start - 0.6, y-top),
      stroke: 0.4pt + luma(140),
    )
    line(
      (x-front-start - 0.55, y-bot),
      (x-front-start - 0.55, y-top),
      mark: (start: "stealth", end: "stealth"),
      mark-size: 0.10,
      stroke: 0.6pt + black,
    )
    content(
      (x-front-start - 0.85, (y-bot + y-top) / 2),
      text(size: 7pt)[$qty(177.8, "mm")$ ($qty(4, "U")$)],
      angle: 90deg,
    )

    // Dimension: Lip Height (6.35 mm)
    line(
      (x-front-end, y-top),
      (x-front-end + 0.6, y-top),
      stroke: 0.4pt + luma(140),
    )
    line(
      (x-front-end, y-lip-top),
      (x-front-end + 0.6, y-lip-top),
      stroke: 0.4pt + luma(140),
    )
    line(
      (x-front-end + 0.55, y-lip-top),
      (x-front-end + 0.55, y-top),
      mark: (start: "stealth", end: "stealth"),
      mark-size: 0.08,
      stroke: 0.6pt + black,
    )
    content(
      (x-front-end + 1.2, (y-lip-top + y-top) / 2),
      text(size: 6.5pt)[$qty(6.35, "mm")$],
    )

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

    // ==========================================
    // 2. REAR ELEVATION (INNER FACE: 38.0 x 177.8 mm)
    // ==========================================
    let x-rear-start = 4.5
    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 (INNER FACE)])

    // Painted Central Surface Area
    rect(
      (x-rear-start, y-bot),
      (x-rear-end, y-top),
      fill: luma(225),
      stroke: 1.2pt + black,
    )

    // Masked Unpainted Area 1: Top Lip (6.35 mm full width)
    rect(
      (x-rear-start, y-lip-top),
      (x-rear-end, y-top),
      fill: luma(250),
      stroke: (paint: luma(120), thickness: 0.6pt, dash: "dashed"),
    )

    // Masked Unpainted Area 2: Bottom Lip (6.35 mm full width)
    rect(
      (x-rear-start, y-bot),
      (x-rear-end, y-lip-bot),
      fill: luma(250),
      stroke: (paint: luma(120), thickness: 0.6pt, dash: "dashed"),
    )

    // Masked Unpainted Area 3: L-Channel Front Flange Contact Strip (8 mm along right side in rear view)
    rect(
      (x-rear-end - f, y-lip-bot),
      (x-rear-end, y-lip-top),
      fill: luma(250),
      stroke: (paint: luma(120), thickness: 0.6pt, dash: "dashed"),
    )

    // Center Painted Label
    content(
      (x-rear-start + (w - f) / 2, (y-lip-bot + y-lip-top) / 2),
      text(size: 7.5pt, fill: luma(100))[Painted Surface],
    )

    // M3 Lip Holes in Rear View
    draw-m3-hole(x-rear-mid, h-lip / 2)
    draw-m3-hole(x-rear-mid, h-total - h-lip / 2)

    // L-Channel Attachment Holes in Rear View (X = 38.0 - 4.0 = 34.0 mm in rear view)
    let x-hole-rear = x-rear-end - 4.0 * s
    draw-m3-hole(x-hole-rear, y-hole-lower)
    draw-m3-hole(x-hole-rear, y-hole-upper)

    // Masked Contact Area Callout Annotations
    line(
      (x-rear-mid, y-top - h-lip / 2),
      (x-rear-end + 0.5, y-top + 0.3),
      stroke: 0.5pt + black,
    )
    content(
      (x-rear-end + 0.55, y-top + 0.3),
      text(size: 6.5pt)[Masked unpainted lip (chassis grounding)],
      anchor: "south-west",
    )

    line(
      (x-rear-end - f / 2, y-hole-upper + 10.0 * s),
      (x-rear-end + 0.5, y-hole-upper + 10.0 * s),
      stroke: 0.5pt + black,
    )
    content(
      (x-rear-end + 0.55, y-hole-upper + 10.0 * s),
      text(size: 6.5pt)[Masked 8 mm flange contact strip],
      anchor: "west",
    )
  }),
  caption: [Cassette Front Panel technical drawings showing Front Elevation (with countersunk L-channel attachment holes and lip mounting holes) and Rear Elevation (inner face detailing masked unpainted EMI contact areas)],
) <figure-cassette-front-panel-diagram>
