// 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.060 drawing units
    let s = 0.060

    let h = 165.1 * s // 4.953 mm
    let d = 152.4 * s // 4.572 mm
    let w = 35.7 * s // 1.071 mm
    let f = 8.0 * s // 0.240 mm (8 mm flange overlap)
    let t = 1.2 * s // Sheet metal thickness (1.2 mm)

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

    // ==========================================
    // 1. FRONT ELEVATION (U-Profile End View: 35.7 x 165.1 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])

    // U-Channel Sheet Metal Cross-Section Outline
    // Top Limb
    rect(
      (x-front-start, y-top - t),
      (x-front-end, y-top),
      fill: luma(210),
      stroke: 0.8pt + black,
    )
    // Right Side Wall
    rect(
      (x-front-end - t, y-bot),
      (x-front-end, y-top),
      fill: luma(230),
      stroke: 0.8pt + black,
    )
    // Bottom Limb
    rect(
      (x-front-start, y-bot),
      (x-front-end, y-bot + t),
      fill: luma(210),
      stroke: 0.8pt + black,
    )

    // Inner Cavity Hatch/Indicator Line
    line(
      (x-front-start, y-bot + t),
      (x-front-end - t, y-bot + t),
      (x-front-end - t, y-top - t),
      (x-front-start, y-top - t),
      stroke: (paint: luma(140), thickness: 0.5pt, dash: "dotted"),
    )

    // Front Width Dimension (35.7 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.6),
      (x-front-end, y-bot - 0.6),
      mark: (start: "stealth", end: "stealth"),
      mark-size: 0.10,
      stroke: 0.6pt + black,
    )
    content((x-front-mid, y-bot - 0.9), text(size: 7pt)[$qty(35.7, "mm")$])

    // Front Height Dimension (165.1 mm)
    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.6, y-bot),
      (x-front-start - 0.6, y-top),
      mark: (start: "stealth", end: "stealth"),
      mark-size: 0.10,
      stroke: 0.6pt + black,
    )
    content(
      (x-front-start - 0.9, (y-bot + y-top) / 2),
      text(size: 7pt)[$qty(165.1, "mm")$],
      angle: 90deg,
    )

    // ==========================================
    // 2. RIGHT ELEVATION (Inner Face View: 152.4 x 165.1 mm)
    // ==========================================
    let x-right-start = 3.5
    let x-right-end = x-right-start + d
    let x-right-mid = (x-right-start + x-right-end) / 2

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

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

    // Masked Unpainted Perimeter Area (8mm border where it mates with L-channel flanges)
    rect(
      (x-right-start + f, y-bot + f),
      (x-right-end - f, y-top - f),
      fill: luma(230),
      stroke: (paint: luma(130), thickness: 0.7pt, dash: "dashed"),
    )
    content(
      (x-right-mid, (y-bot + y-top) / 2),
      text(size: 7.5pt, fill: luma(100))[Painted Central Surface],
    )

    // Countersunk Through-Holes (Crosshair + concentric circles for 90-deg countersink)
    let draw-csink(cx, cy) = {
      circle((cx, cy), radius: 0.10, fill: white, stroke: 0.6pt + black)
      circle((cx, cy), radius: 0.05, fill: luma(180), stroke: 0.4pt + black)
      line((cx - 0.13, cy), (cx + 0.13, cy), stroke: 0.3pt + luma(120))
      line((cx, cy - 0.13), (cx, cy + 0.13), stroke: 0.3pt + luma(120))
    }

    // Top & Bottom Edge Countersunk Hole Pattern (matching top/bottom flanges)
    draw-csink(x-right-start + 30.0 * s, y-top - f / 2)
    draw-csink(x-right-start + 122.4 * s, y-top - f / 2)
    draw-csink(x-right-start + 30.0 * s, y-bot + f / 2)
    draw-csink(x-right-start + 122.4 * s, y-bot + f / 2)

    // Front & Rear Edge Countersunk Hole Pattern
    draw-csink(x-right-start + f / 2, y-bot + 30.0 * s)
    draw-csink(x-right-start + f / 2, y-top - 30.0 * s)
    draw-csink(x-right-end - f / 2, y-bot + 30.0 * s)
    draw-csink(x-right-end - f / 2, y-top - 30.0 * s)

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

    // Masked Border Callout Annotation
    line(
      (x-right-start + f / 2, y-top - 15.0 * s),
      (x-right-start - 0.5, y-top - 1.0),
      stroke: 0.5pt + black,
    )
    content(
      (x-right-start - 0.5, y-top),
      text(size: 6.5pt)[Masked unpainted perimeter \
        (8 mm EMI contact area)],
      anchor: "south-east",
      angle: 90deg,
    )
  }),
  caption: [Cassette U-Channel cover technical drawings showing Front Elevation (profile) and Right Elevation (inner face showing masked unpainted EMI perimeter and countersunk hole grid)],
) <figure-cassette-uchannel-diagram>
