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

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

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

    let panel-w = 38.0 * s // 1.824 units
    let panel-h = 177.8 * s // 8.5344 units
    let lip-h = 6.35 * s // Lip height top/bottom

    // Grid Origin & Pitch
    let orig-x = 9.525 * s // Column 1 (A)
    let col2-x = 28.575 * s // Column 2 (B)
    let col-pitch = 19.05 * s // 0.75 inch pitch

    let orig-y-top = 22.225 * s // Row 1 Y offset from top
    let row-pitch = 19.05 * s // 0.75 inch pitch

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

    // Row Y-coordinates from bottom Y=0
    let row-y = (
      y-panel-top - orig-y-top - 0 * row-pitch, // Row 1
      y-panel-top - orig-y-top - 1 * row-pitch, // Row 2
      y-panel-top - orig-y-top - 2 * row-pitch, // Row 3
      y-panel-top - orig-y-top - 3 * row-pitch, // Row 4
      y-panel-top - orig-y-top - 4 * row-pitch, // Row 5
      y-panel-top - orig-y-top - 5 * row-pitch, // Row 6
      y-panel-top - orig-y-top - 6 * row-pitch, // Row 7
      y-panel-top - orig-y-top - 7 * row-pitch, // Row 8
    )

    // ==========================================
    // 1. FULL PANEL GRID MATRIX (A1 to B8)
    // ==========================================
    let x-grid-start = 0

    // View Title
    content((x-grid-start + panel-w / 2, y-panel-top + 1.5), text(
      size: 8.5pt,
      weight: "bold",
    )[1. 2 $times$ 8 GRID MATRIX LAYOUT])
    content((x-grid-start + panel-w / 2, y-panel-top + 1.15), text(
      size: 7pt,
      fill: luma(90),
    )[($38.0 upright("mm") times 177.8 upright("mm")$ Panel)])

    // Panel Outer Boundary
    rect(
      (x-grid-start, y-panel-bot),
      (x-grid-start + panel-w, y-panel-top),
      fill: luma(250),
      stroke: 1.2pt + black,
    )

    // Top/Bottom Lip Boundaries
    line(
      (x-grid-start, y-panel-top - lip-h),
      (x-grid-start + panel-w, y-panel-top - lip-h),
      stroke: (paint: luma(140), thickness: 0.5pt, dash: "dashed"),
    )
    line(
      (x-grid-start, y-panel-bot + lip-h),
      (x-grid-start + panel-w, y-panel-bot + lip-h),
      stroke: (paint: luma(140), thickness: 0.5pt, dash: "dashed"),
    )

    // Grid Column Lines (A & B)
    line(
      (x-grid-start + orig-x, y-panel-bot + lip-h),
      (x-grid-start + orig-x, y-panel-top - lip-h),
      stroke: (paint: luma(130), thickness: 0.5pt, dash: "dash-dotted"),
    )
    line(
      (x-grid-start + col2-x, y-panel-bot + lip-h),
      (x-grid-start + col2-x, y-panel-top - lip-h),
      stroke: (paint: luma(130), thickness: 0.5pt, dash: "dash-dotted"),
    )

    // Grid Row Lines (1 to 8)
    for ry in row-y {
      line(
        (x-grid-start, ry),
        (x-grid-start + panel-w, ry),
        stroke: (paint: luma(130), thickness: 0.5pt, dash: "dash-dotted"),
      )
    }

    // Grid Node Markers & Labels (A1..A8, B1..B8)
    let col-names = ("A", "B")
    let col-xs = (x-grid-start + orig-x, x-grid-start + col2-x)

    for (c-idx, cx) in col-xs.enumerate() {
      let c-letter = col-names.at(c-idx)
      for (r-idx, ry) in row-y.enumerate() {
        let node-name = c-letter + str(r-idx + 1)
        circle((cx, ry), radius: 0.08, fill: white, stroke: 0.8pt + black)
        content((cx, ry + 0.18), text(size: 5pt, weight: "bold")[#node-name])
      }
    }

    // Dimension: Origin X (9.525 mm)
    line(
      (x-grid-start, y-panel-top),
      (x-grid-start, y-panel-top + 0.4),
      stroke: 0.4pt + luma(140),
    )
    line(
      (x-grid-start + orig-x, y-panel-top),
      (x-grid-start + orig-x, y-panel-top + 0.4),
      stroke: 0.4pt + luma(140),
    )
    line(
      (x-grid-start, y-panel-top + 0.35),
      (x-grid-start + orig-x, y-panel-top + 0.35),
      mark: (start: "stealth", end: "stealth"),
      mark-size: 0.08,
      stroke: 0.5pt + black,
    )
    content((x-grid-start + orig-x / 2, y-panel-top + 0.15), text(
      size: 6.5pt,
    )[$qty(9.525, "mm")$])

    // Dimension: Column Pitch (19.05 mm)
    line(
      (x-grid-start + col2-x, y-panel-top),
      (x-grid-start + col2-x, y-panel-top + 0.4),
      stroke: 0.4pt + luma(140),
    )
    line(
      (x-grid-start + orig-x, y-panel-top + 0.35),
      (x-grid-start + col2-x, y-panel-top + 0.35),
      mark: (start: "stealth", end: "stealth"),
      mark-size: 0.08,
      stroke: 0.5pt + black,
    )
    content((x-grid-start + (orig-x + col2-x) / 2, y-panel-top + 0.55), text(
      size: 6.5pt,
    )[$qty(19.05, "mm")$ Pitch])

    // Dimension: Origin Y (22.225 mm from top)
    let row1-y = row-y.at(0)
    line(
      (x-grid-start, y-panel-top),
      (x-grid-start - 0.5, y-panel-top),
      stroke: 0.4pt + luma(140),
    )
    line(
      (x-grid-start, row1-y),
      (x-grid-start - 0.5, row1-y),
      stroke: 0.4pt + luma(140),
    )
    line(
      (x-grid-start - 0.45, y-panel-top),
      (x-grid-start - 0.45, row1-y),
      mark: (start: "stealth", end: "stealth"),
      mark-size: 0.08,
      stroke: 0.5pt + black,
    )
    content(
      (x-grid-start - 0.75, (y-panel-top + row1-y) / 2),
      text(size: 6.5pt)[Origin Y: $qty(22.225, "mm")$],
      angle: 90deg,
    )

    // Dimension: Row Pitch (19.05 mm)
    let row2-y = row-y.at(1)
    line(
      (x-grid-start, row2-y),
      (x-grid-start - 0.5, row2-y),
      stroke: 0.4pt + luma(140),
    )
    line(
      (x-grid-start - 0.45, row1-y),
      (x-grid-start - 0.45, row2-y),
      mark: (start: "stealth", end: "stealth"),
      mark-size: 0.08,
      stroke: 0.5pt + black,
    )
    content(
      (x-grid-start - 0.25, (row1-y + row2-y) / 2),
      text(size: 6.5pt)[Row pitch: $qty(19.05, "mm")$],
      angle: 90deg,
    )

    // ==========================================
    // 2. COMPONENT PLACEMENT EXAMPLES (MAGNIFIED ROWS 1–3)
    // ==========================================
    let x-ex-start = 5.8
    let ex-scale = 1.35 // Magnification factor for detail view
    let ex-w = panel-w * ex-scale
    let ex-h = (orig-y-top + 2.5 * row-pitch) * ex-scale

    let y-ex-top = y-panel-top
    let y-ex-bot = y-ex-top - ex-h

    // View Title
    content((x-ex-start + ex-w / 2, y-panel-top + 1.5), text(
      size: 8.5pt,
      weight: "bold",
    )[2. COMPONENT PLACEMENT EXAMPLES])
    content((x-ex-start + ex-w / 2, y-panel-top + 1.15), text(
      size: 7pt,
      fill: luma(90),
    )[(Magnified Detail: Rows 1 to 3)])

    // Magnified Panel Outline
    rect(
      (x-ex-start, y-ex-bot),
      (x-ex-start + ex-w, y-ex-top),
      fill: luma(248),
      stroke: 1.2pt + black,
    )

    // Magnified Grid Lines
    let ex-colA-x = x-ex-start + orig-x * ex-scale
    let ex-colB-x = x-ex-start + col2-x * ex-scale
    let ex-row1-y = y-ex-top - orig-y-top * ex-scale
    let ex-row2-y = ex-row1-y - row-pitch * ex-scale
    let ex-row3-y = ex-row2-y - row-pitch * ex-scale

    let ex-rows = (ex-row1-y, ex-row2-y, ex-row3-y)

    line((ex-colA-x, y-ex-bot), (ex-colA-x, y-ex-top), stroke: (
      paint: luma(140),
      thickness: 0.4pt,
      dash: "dash-dotted",
    ))
    line((ex-colB-x, y-ex-bot), (ex-colB-x, y-ex-top), stroke: (
      paint: luma(140),
      thickness: 0.4pt,
      dash: "dash-dotted",
    ))
    for ery in ex-rows {
      line((x-ex-start, ery), (x-ex-start + ex-w, ery), stroke: (
        paint: luma(140),
        thickness: 0.4pt,
        dash: "dash-dotted",
      ))
    }

    // Placement Example 1: 4 mm Banana Jack Hole on A1 (\diameter 8.0 mm)
    let r-banana = (8.0 / 2) * s * ex-scale
    circle(
      (ex-colA-x, ex-row1-y),
      radius: r-banana,
      fill: white,
      stroke: 1pt + black,
    )
    circle((ex-colA-x, ex-row1-y), radius: 0.05, fill: black)

    // Placement Example 2: Potentiometer Shaft Hole on B1 (\diameter 7.0 mm)
    let r-pot = (7.0 / 2) * s * ex-scale
    circle(
      (ex-colB-x, ex-row1-y),
      radius: r-pot,
      fill: white,
      stroke: 1pt + black,
    )
    circle((ex-colB-x, ex-row1-y), radius: 0.05, fill: black)

    // Placement Example 3: Off-Grid 3 mm LED Hole between A1 and A2 (\diameter 3.2 mm)
    let ex-led-y = (ex-row1-y + ex-row2-y) / 2
    let r-led = (3.2 / 2) * s * ex-scale
    circle(
      (ex-colA-x, ex-led-y),
      radius: r-led,
      fill: luma(220),
      stroke: 0.9pt + black,
    )
    circle((ex-colA-x, ex-led-y), radius: 0.04, fill: black)

    // Placement Example 4: Toggle Switch Hole on B2 (\diameter 6.0 mm)
    let r-sw = (6.0 / 2) * s * ex-scale
    circle(
      (ex-colB-x, ex-row2-y),
      radius: r-sw,
      fill: white,
      stroke: 1pt + black,
    )

    // Callout Annotations for Placement Examples
    line(
      (ex-colA-x, ex-row1-y),
      (ex-colA-x - 0.85, ex-row1-y),
      stroke: 0.5pt + black,
    )
    content(
      (ex-colA-x - 0.95, ex-row1-y + r-banana + 0.35),
      angle: 90deg,
      text(
        size: 6pt,
      )[A1: $4 upright("mm")$ Banana Jack ($diameter 8.0 upright("mm")$)],
      anchor: "east",
    )

    line(
      (ex-colB-x, ex-row1-y),
      (ex-colB-x + 0.85, ex-row1-y),
      stroke: 0.5pt + black,
    )
    content(
      (ex-colB-x + 0.95, ex-row1-y),
      text(
        size: 6pt,
      )[B1: Pot shaft ($diameter 7.0 upright("mm")$)],
      anchor: "west",
    )

    line(
      (ex-colA-x + r-led, ex-led-y),
      (x-ex-start + ex-w + 0.3, ex-led-y),
      stroke: 0.5pt + black,
    )
    content(
      (x-ex-start + ex-w + 0.35, ex-led-y),
      text(
        size: 6pt,
      )[Off-grid LED ($diameter 3.2 upright("mm")$)],
      anchor: "west",
    )

    line(
      (ex-colB-x + r-sw, ex-row2-y),
      (x-ex-start + ex-w + 0.3, ex-row2-y),
      stroke: 0.5pt + black,
    )
    content(
      (x-ex-start + ex-w + 0.35, ex-row2-y),
      text(
        size: 6pt,
      )[B2: toggle switch ($diameter 6.0 upright("mm")$)],
      anchor: "west",
    )
  }),
  caption: [Panels grid specification technical drawing showing full $2 times 8$ grid matrix coordinates (origin X = $qty(9.525, "mm")$, origin Y = $qty(22.225, "mm")$, pitch = $qty(19.05, "mm")$) and component placement rules],
) <figure-panels-grid-diagram>
