#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 = 35.7 * s // 1.6065 units
    let panel-h = 165.1 * s // 7.4295 units
    let cx-panel = panel-w / 2
    let cy-panel = panel-h / 2 // 82.5 mm from top/bottom

    // DB-25 Cutout Dimensions (Scale for Detail View)
    let ds = 0.12 // Magnified detail scale

    let d-width = 33.3 * ds // Overall cutout width (33.3 mm)
    let d-height = 12.5 * ds // Cutout height (12.5 mm)
    let hole-spacing = 24.99 * ds // Mounting hole spacing from center / pitch
    let hole-r = (3.2 / 2) * ds // 3.2 mm diameter (1.6 mm radius)

    // ==========================================
    // 1. REAR PANEL CUTOUT PLACEMENT VIEW (35.7 mm x 165.1 mm)
    // ==========================================
    let x-panel-start = 0
    let y-panel-start = 0

    // View Title
    content((cx-panel, panel-h + 0.8), text(
      size: 8.5pt,
      weight: "bold",
    )[REAR PANEL PLACEMENT])
    content((cx-panel, panel-h + 0.45), text(
      size: 7pt,
      fill: luma(90),
    )[(Centered Position)])

    // Rear Closure Plate Outline
    rect(
      (x-panel-start, y-panel-start),
      (x-panel-start + panel-w, y-panel-start + panel-h),
      fill: luma(245),
      stroke: 1.2pt + black,
    )

    // Centerlines on Rear Panel
    line(
      (x-panel-start - 0.4, cy-panel),
      (x-panel-start + panel-w + 0.4, cy-panel),
      stroke: (paint: luma(120), thickness: 0.4pt, dash: "dash-dotted"),
    )
    line(
      (cx-panel, y-panel-start - 0.4),
      (cx-panel, y-panel-start + panel-h + 0.4),
      stroke: (paint: luma(120), thickness: 0.4pt, dash: "dash-dotted"),
    )

    // Small DB-25 D-Sub Cutout Representation in Center
    let sm-w = 26.0 * s
    let sm-h = 11.0 * s
    rect(
      (cx-panel - sm-w / 2, cy-panel - sm-h / 2),
      (cx-panel + sm-w / 2, cy-panel + sm-h / 2),
      fill: white,
      stroke: 0.8pt + black,
    )
    circle(
      (cx-panel - 12.5 * s, cy-panel),
      radius: 1.6 * s,
      fill: white,
      stroke: 0.6pt + black,
    )
    circle(
      (cx-panel + 12.5 * s, cy-panel),
      radius: 1.6 * s,
      fill: white,
      stroke: 0.6pt + black,
    )

    // Center X Dimension Callout (17.85 mm)
    line(
      (x-panel-start, y-panel-start),
      (x-panel-start, y-panel-start - 0.5),
      stroke: 0.4pt + luma(140),
    )
    line(
      (cx-panel, y-panel-start),
      (cx-panel, y-panel-start - 0.5),
      stroke: 0.4pt + luma(140),
    )
    line(
      (x-panel-start, y-panel-start - 0.45),
      (cx-panel, y-panel-start - 0.45),
      mark: (start: "stealth", end: "stealth"),
      mark-size: 0.08,
      stroke: 0.6pt + black,
    )
    content((cx-panel / 2, y-panel-start - 0.7), text(
      size: 6.5pt,
      weight: "bold",
    )[$qty(17.85, "mm")$])

    // Center Y Dimension Callout (82.5 mm)
    line(
      (x-panel-start, y-panel-start),
      (x-panel-start - 0.5, y-panel-start),
      stroke: 0.4pt + luma(140),
    )
    line(
      (x-panel-start, cy-panel),
      (x-panel-start - 0.5, cy-panel),
      stroke: 0.4pt + luma(140),
    )
    line(
      (x-panel-start - 0.45, y-panel-start),
      (x-panel-start - 0.45, cy-panel),
      mark: (start: "stealth", end: "stealth"),
      mark-size: 0.08,
      stroke: 0.6pt + black,
    )
    content(
      (x-panel-start - 0.75, cy-panel / 2),
      text(size: 6.5pt, weight: "bold")[$qty(82.5, "mm")$],
      angle: 90deg,
    )

    // ==========================================
    // 2. MAGNIFIED DB-25 CUTOUT GEOMETRY DETAIL
    // ==========================================
    let x-det-center = 6.2
    let y-det-center = cy-panel

    // View Title
    content((x-det-center, y-det-center + 2.2), text(
      size: 8.5pt,
      weight: "bold",
    )[CUTOUT GEOMETRY DETAIL])
    content((x-det-center, y-det-center + 1.85), text(
      size: 7pt,
      fill: luma(90),
    )[(Standard D-Shaped DB-25 Profile)])

    // Centerlines
    line(
      (x-det-center - 2.5, y-det-center),
      (x-det-center + 2.5, y-det-center),
      stroke: (paint: luma(120), thickness: 0.4pt, dash: "dash-dotted"),
    )
    line(
      (x-det-center, y-det-center - 1.5),
      (x-det-center, y-det-center + 1.5),
      stroke: (paint: luma(120), thickness: 0.4pt, dash: "dash-dotted"),
    )

    // D-Sub Trapezoidal Shroud Profile (Closed Path)
    let tw = 25.0 * ds // Top wider width
    let bw = 22.5 * ds // Bottom narrower width
    let hh = d-height / 2

    line(
      (x-det-center - tw / 2, y-det-center + hh),
      (x-det-center + tw / 2, y-det-center + hh),
      (x-det-center + bw / 2, y-det-center - hh),
      (x-det-center - bw / 2, y-det-center - hh),
      close: true,
      fill: white,
      stroke: 1.2pt + black,
    )

    // Mounting Holes (Left & Right, Diameter 3.2 mm)
    let hole-left-x = x-det-center - hole-spacing / 2
    let hole-right-x = x-det-center + hole-spacing / 2

    circle(
      (hole-left-x, y-det-center),
      radius: hole-r,
      fill: white,
      stroke: 0.8pt + black,
    )
    circle(
      (hole-right-x, y-det-center),
      radius: hole-r,
      fill: white,
      stroke: 0.8pt + black,
    )

    // Hole Center Crosshairs
    line(
      (hole-left-x - 0.2, y-det-center),
      (hole-left-x + 0.2, y-det-center),
      stroke: 0.3pt + luma(120),
    )
    line(
      (hole-left-x, y-det-center - 0.2),
      (hole-left-x, y-det-center + 0.2),
      stroke: 0.3pt + luma(120),
    )
    line(
      (hole-right-x - 0.2, y-det-center),
      (hole-right-x + 0.2, y-det-center),
      stroke: 0.3pt + luma(120),
    )
    line(
      (hole-right-x, y-det-center - 0.2),
      (hole-right-x, y-det-center + 0.2),
      stroke: 0.3pt + luma(120),
    )

    // Overall D-Opening Width Dimension (33.3 mm)
    let x-left-edge = x-det-center - d-width / 2
    let x-right-edge = x-det-center + d-width / 2

    line(
      (x-left-edge, y-det-center + hh),
      (x-left-edge, y-det-center + hh + 0.6),
      stroke: 0.4pt + luma(140),
    )
    line(
      (x-right-edge, y-det-center + hh),
      (x-right-edge, y-det-center + hh + 0.6),
      stroke: 0.4pt + luma(140),
    )
    line(
      (x-left-edge, y-det-center + hh + 0.6),
      (x-right-edge, y-det-center + hh + 0.6),
      mark: (start: "stealth", end: "stealth"),
      mark-size: 0.08,
      stroke: 0.6pt + black,
    )
    content((x-det-center, y-det-center + hh + 0.3), text(
      size: 7pt,
    )[Opening width: $qty(33.3, "mm")$])

    // D-Opening Height Dimension (12.5 mm)
    line(
      (x-left-edge, y-det-center - hh),
      (x-left-edge - 0.5, y-det-center - hh),
      stroke: 0.4pt + luma(140),
    )
    line(
      (x-left-edge, y-det-center + hh),
      (x-left-edge - 0.5, y-det-center + hh),
      stroke: 0.4pt + luma(140),
    )
    line(
      (x-left-edge - 0.5, y-det-center - hh),
      (x-left-edge - 0.5, y-det-center + hh),
      mark: (start: "stealth", end: "stealth"),
      mark-size: 0.08,
      stroke: 0.6pt + black,
    )
    content(
      (x-left-edge - 0.75, y-det-center),
      text(size: 7pt)[$qty(12.5, "mm")$],
      angle: 90deg,
    )

    // Mounting Hole Spacing Dimension (24.99 mm)
    line(
      (hole-left-x, y-det-center - hh),
      (hole-left-x, y-det-center - hh - 0.6),
      stroke: 0.4pt + luma(140),
    )
    line(
      (hole-right-x, y-det-center - hh),
      (hole-right-x, y-det-center - hh - 0.6),
      stroke: 0.4pt + luma(140),
    )
    line(
      (hole-left-x, y-det-center - hh - 0.6),
      (hole-right-x, y-det-center - hh - 0.6),
      mark: (start: "stealth", end: "stealth"),
      mark-size: 0.08,
      stroke: 0.6pt + black,
    )
    content((x-det-center, y-det-center - hh - 0.9), text(
      size: 7pt,
    )[Hole pitch: $qty(24.99, "mm")$])

    // Hole Diameter Callout (diameter 3.2 mm)
    line(
      (hole-right-x, y-det-center + hole-r),
      (hole-right-x + 0.6, y-det-center + 1.1),
      mark: (start: "stealth"),
      mark-size: 0.08,
      stroke: 0.6pt + black,
    )
    content(
      (hole-right-x + 0.65, y-det-center + 1.15),
      text(size: 6.5pt)[$2 times diameter qty(3.2, "mm")$ holes],
      anchor: "west",
    )
  }),
  caption: [Cassette DB-25 connector cutout technical drawing showing rear panel placement coordinates and magnified CAD geometry dimensions],
) <figure-cassette-db-25-connector-cutout-diagram>
