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

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

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

    let face-d = 152.4 * s // Depth = 152.4 mm
    let face-w = 35.7 * s // Width = 35.7 mm

    // Slot parameters
    let slot-w = 2.5 * s // Slot width = 2.5 mm
    let slot-l = 10.0 * s // Slot length = 10.0 mm
    let slot-r = slot-w / 2 // Full corner radius = 1.25 mm
    let row-pitch = 5.0 * s // 5 rows across width
    let col-pitch = 12.0 * s // 12 columns across depth

    // Calculate array offsets to center slots on face
    let row-start = (face-w - (4 * row-pitch)) / 2
    let col-start = (face-d - (11 * col-pitch + slot-l)) / 2

    // ==========================================
    // 1. TOP / BOTTOM FACE VENTILATION PATTERN (152.4 mm x 35.7 mm)
    // ==========================================
    let x-top-start = -0.5
    let y-top-start = 5.2
    let x-top-end = x-top-start + face-d
    let y-top-end = y-top-start + face-w
    let x-top-mid = (x-top-start + x-top-end) / 2
    let y-top-mid = (y-top-start + y-top-end) / 2

    // View Title
    content((x-top-mid, y-top-end + 0.7), text(
      size: 9pt,
      weight: "bold",
    )[TOP / BOTTOM FACE VENTILATION PATTERN])
    content((x-top-mid, y-top-end + 0.35), text(
      size: 7pt,
      fill: luma(90),
    )[(60 Slots per face: $5$ rows $times 12$ columns)])

    // Main Plate Outline
    rect(
      (x-top-start, y-top-start),
      (x-top-end, y-top-end),
      fill: luma(250),
      stroke: 1.2pt + black,
    )

    // 5 Rows x 12 Columns Slot Array with Full Corner Radius
    for r in range(0, 5) {
      let sy = y-top-start + row-start + r * row-pitch
      for c in range(0, 12) {
        let sx = x-top-start + col-start + c * col-pitch
        rect(
          (sx, sy - slot-w / 2),
          (sx + slot-l, sy + slot-w / 2),
          radius: slot-r,
          fill: white,
          stroke: 0.5pt + black,
        )
      }
    }

    // Depth Dimension (152.4 mm)
    line(
      (x-top-start, y-top-start),
      (x-top-start, y-top-start - 0.5),
      stroke: 0.4pt + luma(140),
    )
    line(
      (x-top-end, y-top-start),
      (x-top-end, y-top-start - 0.5),
      stroke: 0.4pt + luma(140),
    )
    line(
      (x-top-start, y-top-start - 0.45),
      (x-top-end, y-top-start - 0.45),
      mark: (start: "stealth", end: "stealth"),
      mark-size: 0.10,
      stroke: 0.6pt + black,
    )
    content((x-top-mid, y-top-start - 0.75), text(
      size: 7.5pt,
    )[$qty(152.4, "mm")$])

    // Width Dimension (35.7 mm)
    line(
      (x-top-end, y-top-start),
      (x-top-end + 0.5, y-top-start),
      stroke: 0.4pt + luma(140),
    )
    line(
      (x-top-end, y-top-end),
      (x-top-end + 0.5, y-top-end),
      stroke: 0.4pt + luma(140),
    )
    line(
      (x-top-end + 0.45, y-top-start),
      (x-top-end + 0.45, y-top-end),
      mark: (start: "stealth", end: "stealth"),
      mark-size: 0.10,
      stroke: 0.6pt + black,
    )
    content(
      (x-top-end + 0.75, y-top-mid),
      text(size: 7.5pt)[$qty(35.7, "mm")$],
      angle: -90deg,
    )

    // Column Pitch Indicator (12.0 mm)
    let c0-x = x-top-start + col-start + slot-l / 2
    let c1-x = c0-x + col-pitch
    line((c0-x, y-top-start), (c0-x, y-top-start - 1.2), stroke: (
      paint: luma(140),
      thickness: 0.4pt,
      dash: "dashed",
    ))
    line((c1-x, y-top-start), (c1-x, y-top-start - 1.2), stroke: (
      paint: luma(140),
      thickness: 0.4pt,
      dash: "dashed",
    ))
    line(
      (c0-x, y-top-start - 1.15),
      (c1-x, y-top-start - 1.15),
      mark: (start: "stealth", end: "stealth"),
      mark-size: 0.08,
      stroke: 0.5pt + black,
    )
    content(((c0-x + c1-x) / 2, y-top-start - 1.35), text(
      size: 6.5pt,
    )[Column pitch: $qty(12.0, "mm")$ ($12$ cols)])

    // Row Pitch Indicator (5.0 mm)
    let r0-y = y-top-start + row-start
    let r1-y = r0-y + row-pitch
    line((x-top-start, r0-y), (x-top-start - 0.8, r0-y), stroke: (
      paint: luma(140),
      thickness: 0.4pt,
      dash: "dashed",
    ))
    line((x-top-start, r1-y), (x-top-start - 0.8, r1-y), stroke: (
      paint: luma(140),
      thickness: 0.4pt,
      dash: "dashed",
    ))
    line(
      (x-top-start - 0.75, r0-y),
      (x-top-start - 0.75, r1-y),
      mark: (start: "stealth", end: "stealth"),
      mark-size: 0.08,
      stroke: 0.5pt + black,
    )
    content(
      (x-top-start - 0.85, (r0-y + r1-y) / 2),
      text(size: 6.5pt)[Row pitch: $qty(5.0, "mm")$ ($5$ rows)],
      anchor: "east",
    )

    // ==========================================
    // 2. SINGLE SLOT GEOMETRY DETAIL (Enlarged View)
    // ==========================================
    let x-detail-center = -1.0
    let y-detail-center = 1.8
    let detail-l = 2.8 // Magnified length (10.0 mm)
    let detail-w = 0.7 // Magnified width (2.5 mm)
    let detail-r = detail-w / 2 // Full radius (1.25 mm)

    let sx1 = x-detail-center - detail-l / 2
    let sx2 = x-detail-center + detail-l / 2
    let sy1 = y-detail-center - detail-w / 2
    let sy2 = y-detail-center + detail-w / 2

    // View Title
    content((x-detail-center, y-detail-center + 1.2), text(
      size: 8.5pt,
      weight: "bold",
    )[SINGLE SLOT GEOMETRY DETAIL])
    content((x-detail-center, y-detail-center + 0.85), text(
      size: 7pt,
      fill: luma(90),
    )[(Enlarged view: $qty(2.5, "mm") times qty(10.0, "mm")$ slot)])

    // Centerlines
    line(
      (sx1 - 0.4, y-detail-center),
      (sx2 + 0.4, y-detail-center),
      stroke: (paint: luma(120), thickness: 0.4pt, dash: "dash-dotted"),
    )
    line(
      (x-detail-center, sy1 - 0.4),
      (x-detail-center, sy2 + 0.4),
      stroke: (paint: luma(120), thickness: 0.4pt, dash: "dash-dotted"),
    )

    // Magnified Rounded Stadium Slot Contour
    rect(
      (sx1, sy1),
      (sx2, sy2),
      radius: detail-r,
      fill: white,
      stroke: 1.2pt + black,
    )

    // Slot Length Dimension (10.0 mm)
    line((sx1, sy1), (sx1, sy1 - 0.5), stroke: 0.4pt + luma(140))
    line((sx2, sy1), (sx2, sy1 - 0.5), stroke: 0.4pt + luma(140))
    line(
      (sx1, sy1 - 0.45),
      (sx2, sy1 - 0.45),
      mark: (start: "stealth", end: "stealth"),
      mark-size: 0.08,
      stroke: 0.6pt + black,
    )
    content((x-detail-center, sy1 - 0.7), text(
      size: 7pt,
    )[$qty(10.0, "mm")$ (parallel to depth)])

    // Slot Width Dimension (2.5 mm)
    line((sx2, sy1), (sx2 + 0.5, sy1), stroke: 0.4pt + luma(140))
    line((sx2, sy2), (sx2 + 0.5, sy2), stroke: 0.4pt + luma(140))
    line(
      (sx2 + 0.45, sy1),
      (sx2 + 0.45, sy2),
      mark: (start: "stealth", end: "stealth"),
      mark-size: 0.08,
      stroke: 0.6pt + black,
    )
    content(
      (sx2 + 0.55, y-detail-center),
      text(size: 7pt)[$qty(2.5, "mm")$],
      anchor: "west",
    )

    // Full Corner Radius Callout (R = 1.25 mm)
    let arc-px = sx1 + detail-r * 0.29
    let arc-py = y-detail-center + detail-r * 0.71
    line(
      (arc-px - 0.8, arc-py + 0.3),
      (arc-px, arc-py),
      mark: (end: "stealth"),
      mark-size: 0.08,
      stroke: 0.6pt + black,
    )
    content(
      (arc-px - 0.85, arc-py + 0.35),
      text(size: 6.5pt)[$R = qty(1.25, "mm")$],
      anchor: "east",
    )

    // ==========================================
    // 3. CONVECTIVE COOLING & EMI SHIELDING CONCEPT
    // ==========================================
    let x-phys-center = 6.5
    let y-phys-center = 1.8
    let phys-w = 3.6
    let phys-t = 0.12

    // View Title
    content((x-phys-center, y-phys-center + 1.2), text(
      size: 8.5pt,
      weight: "bold",
    )[CONVECTIVE COOLING & EMI SHIELDING])
    content((x-phys-center, y-phys-center + 0.85), text(
      size: 7pt,
      fill: luma(90),
    )[(Cross-sectional thermal & RF attenuation concept)])

    // Top Face Metal Plate with 4 Slot Openings
    let top-y = y-phys-center + 0
    rect(
      (x-phys-center - phys-w / 2, top-y),
      (x-phys-center + phys-w / 2, top-y + phys-t),
      fill: luma(220),
      stroke: 0.8pt + black,
    )

    // Bottom Face Metal Plate with 4 Slot Openings
    let bot-y = y-phys-center - 1.2
    rect(
      (x-phys-center - phys-w / 2, bot-y - phys-t),
      (x-phys-center + phys-w / 2, bot-y),
      fill: luma(220),
      stroke: 0.8pt + black,
    )

    // Slot Apertures Cut into Plates
    let ap-offsets = (-1.2, -0.4, 0.4, 1.2)
    for dx in ap-offsets {
      rect(
        (x-phys-center + dx - 0.15, top-y - 0.02),
        (x-phys-center + dx + 0.15, top-y + phys-t + 0.02),
        fill: white,
        stroke: none,
      )
      rect(
        (x-phys-center + dx - 0.15, bot-y - phys-t - 0.02),
        (x-phys-center + dx + 0.15, bot-y + 0.02),
        fill: white,
        stroke: none,
      )

      // Convective Natural Airflow Lines (Dashed Vertical Lines)
      line(
        (x-phys-center + dx, bot-y - 0.4),
        (x-phys-center + dx, top-y + 0.4),
        stroke: (paint: black, thickness: 0.8pt, dash: "dashed"),
        mark: (end: "stealth"),
        mark-size: 0.08,
      )
    }

    // Plate Labels
    content(
      (x-phys-center + phys-w / 2 + 0.15, top-y + phys-t / 2),
      text(size: 6.5pt, weight: "bold")[Top face],
      anchor: "west",
    )
    content(
      (x-phys-center + phys-w / 2 + 0.15, bot-y - phys-t / 2),
      text(size: 6.5pt, weight: "bold")[Bottom face],
      anchor: "west",
    )

    // Convective Airflow Center Annotation
    content((x-phys-center, y-phys-center - 0.6), text(
      size: 7pt,
      weight: "bold",
    )[Natural convection airflow])

    // Incident RF Wave Attenuation Annotation (Wave Bouncing off Metal Face)
    let wave-x0 = x-phys-center - 1.5
    line(
      (wave-x0, top-y - 0.2),
      (wave-x0 + 0.15, top-y - 0.4),
      (wave-x0 + 0.3, top-y - 0.2),
      (wave-x0 + 0.45, top-y - 0.4),
      (wave-x0 + 0.6, top-y - 0.25),
      stroke: 0.8pt + black,
    )
    line(
      (wave-x0 + 0.6, top-y - 0.25),
      (wave-x0 + 0.3, top-y - 0.05),
      stroke: 0.8pt + black,
      mark: (end: "stealth"),
      mark-size: 0.07,
    )
    content(
      (wave-x0 - 0.1, top-y - 0.1),
      text(size: 6pt, weight: "bold")[Incident RF wave],
      anchor: "east",
    )
    content(
      (wave-x0 - 0.1, top-y - 0.35),
      text(size: 5.5pt, fill: luma(100))[(Attenuated $> qty(20, "dB")$)],
      anchor: "east",
    )
  }),
  caption: [Cassette ventilation system details detailing full $5 times 12$ slot top elevation array, magnified single slot geometry with $R = qty(1.25, "mm")$ corner radius, and cross-sectional natural convective cooling / RF electromagnetic shielding concept],
) <figure-cassette-ventilation-diagram>
