import type { ReactNode } from "react";
import { StatusNote } from "../_components/SiteShell";

type Tone = "verified" | "smart" | "copy" | "transcode" | "conditional" | "not-claimed";

function Badge({ tone, children }: { tone: Tone; children: ReactNode }) {
  return <span className={`support-badge badge-${tone}`}>{children}</span>;
}

const videoRows = [
  {
    codec: "MPEG-2 Video",
    profiles: "8-bit 4:2:0; DVD/VOB, MPEG program stream, TS and M2TS workflows",
    edit: <><Badge tone="verified">Edit + preview</Badge><span>Software decode and D3D11VA preview with automatic software retry.</span></>,
    smart: <><Badge tone="smart">Smart render</Badge><span>Source-codec boundary encoding; complete interior GOPs copied.</span></>,
    transcode: <><Badge tone="transcode">Full transcode</Badge><span>FFmpeg software MPEG-2; Intel Quick Sync when its real encoder opens.</span></>,
    evidence: "Signed player matrix: TS/AC-3, M2TS/MP2, MKV/PCM and VOB/DVD LPCM.",
  },
  {
    codec: "MPEG-4 Part 2",
    profiles: "Profiles represented by the real-file corpus",
    edit: <><Badge tone="verified">Edit + preview</Badge><span>Software decode and exact source-frame indexing.</span></>,
    smart: <><Badge tone="smart">Smart render</Badge><span>Real-file source-codec boundary export with copied interior GOPs.</span></>,
    transcode: <><Badge tone="not-claimed">No dedicated preset</Badge><span>Use source-compatible smart render or choose another output codec.</span></>,
    evidence: "Automated real-file corpus gate; not one of the 19 signed player-matrix cases.",
  },
  {
    codec: "H.264 / AVC",
    profiles: "Main, High, High 4:2:2 and High 4:4:4 paths; tested 8-bit 4:2:0 plus deterministic 4:2:2/4:4:4 fixtures",
    edit: <><Badge tone="verified">Edit + preview</Badge><span>D3D11VA or software decode for playback, thumbnails and Ad-Detective.</span></>,
    smart: <><Badge tone="smart">Smart render</Badge><span>x264 software boundary encoding; reusable compressed GOPs remain byte-copied.</span></>,
    transcode: <><Badge tone="transcode">Full transcode</Badge><span>x264 software; NVIDIA NVENC, Intel Quick Sync or AMD AMF when validated by the machine self-test.</span></>,
    evidence: "Signed cases cover AAC, AC-3, E-AC-3, DTS, MP2, MP3 and PCM across MP4, MKV, TS and AVI inputs. MP4, MOV and TS output gates pass.",
  },
  {
    codec: "HEVC / H.265",
    profiles: "Main 8-bit, Main10, Main 4:2:2 10 and Main 4:4:4",
    edit: <><Badge tone="verified">Edit + preview</Badge><span>D3D11VA or software decode; 4K Main10 bounded-memory gate included.</span></>,
    smart: <><Badge tone="smart">Smart render</Badge><span>x265 software boundary encoding with source VPS/SPS/PPS handling and direct GOP copy.</span></>,
    transcode: <><Badge tone="transcode">Full transcode</Badge><span>x265 software; NVENC, Quick Sync or AMF where the requested profile is supported.</span></>,
    evidence: "Signed cases cover Main 8 and Main10 with AAC, ALAC, DTS/DTS-HD-family packets, TrueHD, PGS and no-audio sources. HDR and Dolby Vision evidence is listed below.",
  },
  {
    codec: "AV1",
    profiles: "Main profile, 8-bit and 10-bit 4:2:0",
    edit: <><Badge tone="verified">Edit + preview</Badge><span>D3D11VA or software decode with per-codec fallback.</span></>,
    smart: <><Badge tone="smart">Smart render</Badge><span>libaom boundary encoding plus copied interior GOPs.</span></>,
    transcode: <><Badge tone="transcode">Full transcode</Badge><span>libaom software; NVENC, Quick Sync or AMF when available.</span></>,
    evidence: "Signed mid-GOP playback cases: AV1 8-bit + Opus and AV1 10-bit + FLAC.",
  },
  {
    codec: "VP9",
    profiles: "Profile 0 / 8-bit and Profile 2 / 10-bit 4:2:0",
    edit: <><Badge tone="verified">Edit + preview</Badge><span>D3D11VA or software decode with automatic fallback.</span></>,
    smart: <><Badge tone="smart">Smart render</Badge><span>libvpx boundary encoding plus copied interior GOPs.</span></>,
    transcode: <><Badge tone="transcode">Software transcode</Badge><span>libvpx-vp9. No VP9 hardware encoder is claimed by SmartCut64.</span></>,
    evidence: "Signed cases: VP9 + PCM, VP9 8-bit + Vorbis and VP9 10-bit + Opus.",
  },
  {
    codec: "FFV1",
    profiles: "Lossless archival video; source pixel format retained by the software profile where supported",
    edit: <><Badge tone="verified">Edit + preview</Badge><span>Software decode; FFmpeg 9 Vulkan decode is probed where supported.</span></>,
    smart: <><Badge tone="not-claimed">Full transcode path</Badge><span>No compressed-GOP smart-render claim.</span></>,
    transcode: <><Badge tone="transcode">Lossless output</Badge><span>Software FFV1 or validated Vulkan FFV1 with automatic software fallback.</span></>,
    evidence: "Encoder registration, runtime opening, output decode and CPU/GPU self-test gates. Vulkan slice geometry may require 4:4:4 encoder input.",
  },
  {
    codec: "Apple ProRes",
    profiles: "10-bit ProRes output; ProRes and supported ProRes RAW decode paths",
    edit: <><Badge tone="conditional">Edit + preview</Badge><span>Software decode; FFmpeg 9 Vulkan paths are probed for the installed driver/device.</span></>,
    smart: <><Badge tone="not-claimed">Full transcode path</Badge><span>No compressed-GOP smart-render claim.</span></>,
    transcode: <><Badge tone="transcode">Mezzanine output</Badge><span>Software prores_ks or validated Vulkan ProRes with fallback.</span></>,
    evidence: "Encoder/runtime/decode gates and per-machine self-test. ProRes is visually lossless, not mathematically lossless.",
  },
] as const;

const audioRows = [
  ["AAC", "Decode for playback/waveform", "Complete-frame copy and retiming", "AAC encode with gain, bitrate, sample-rate and channel controls", "Signed H.264/HEVC cases; MP4/MOV/TS/MKV routes depend on the selected stream set."],
  ["AC-3 / Dolby Digital", "Decode", "Complete-frame copy and retiming", "AC-3 encode with profile controls", "Signed MPEG-2 and H.264 cases; verified M2TS/TS transcode choice."],
  ["E-AC-3 / Dolby Digital Plus", "Decode", "Complete-frame copy and retiming", "No dedicated E-AC-3 encoder profile", "Signed H.264 + E-AC-3 case. Copy only where the destination has a native mapping."],
  ["MP2", "Decode", "Complete-frame copy and retiming", "MP2 encode with profile controls", "Signed MPEG-2/M2TS and H.264/TS cases."],
  ["MP3", "Decode", "Complete-frame copy and retiming", "No dedicated MP3 encoder profile", "Signed H.264/AVI input case; elementary extraction is supported."],
  ["FLAC", "Decode", "Lossless packet preservation", "No dedicated FLAC encoder profile", "Signed AV1 10-bit + FLAC case; Matroska is the normal preservation container."],
  ["Opus", "Decode", "Complete-frame copy and retiming", "Opus encode with gain/rate/channel controls", "Signed AV1 and VP9 cases; Matroska/WebM-origin workflows."],
  ["Vorbis", "Decode", "Complete-frame copy and retiming", "No dedicated Vorbis encoder profile", "Signed VP9 8-bit + Vorbis case."],
  ["ALAC", "Decode", "Lossless packet preservation", "No dedicated ALAC encoder profile", "Signed HEVC Main10 + ALAC MP4 input case."],
  ["PCM S16LE / S24LE", "Decode and real dB waveform", "Lossless payload copy/rechunk with PCM validation", "PCM S16LE encode; gain/rate/channel controls", "Signed MPEG-2/H.264 PCM cases; validation compares decoded PCM where applicable."],
  ["DVD LPCM", "16-bit path decoded", "16-bit lossless Matroska normalisation and compatible delivery", "No DVD LPCM encoder profile", "Signed MPEG-2/VOB mid-GOP case. A 20/24-bit DVD-LPCM Matroska bridge is not currently claimed."],
  ["Blu-ray LPCM", "16-bit and 24-bit decoded", "Lossless Matroska normalise/restore bridge", "No Blu-ray LPCM encoder profile", "Player-tested M2TS matrix: 48/96/192 kHz and mono, stereo, 5.1 and 7.1 representatives; supported native layouts span mono through 7.1."],
  ["DTS / DTS-HD family", "FFmpeg decode where supported", "DTS-family packets, including HD extensions when present, are retained on copy routes", "No DTS encoder profile", "Signed H.264 + DTS and HEVC Main10 multi-DTS cases. No DTS certification claim."],
  ["Dolby TrueHD / Atmos bitstream", "FFmpeg TrueHD decode where supported", "TrueHD packets are retained; embedded Atmos extension data is not rewritten on copy routes", "No TrueHD encoder profile", "Signed HEVC Main10 + TrueHD case. SmartCut64 does not interpret or certify Atmos metadata."],
  ["Dolby E / SMPTE 337M and SMPTE 302M", "Opaque carrier only; no programme-audio decode", "Lossless Matroska bridge and byte-exact supported carrier restoration", "No decode or encode", "Supported Dolby E bridge is limited to 16-bit little-endian SMPTE 337M carrier bursts. No Dolby certification claim."],
] as const;

const containerRows = [
  ["Matroska / MKV", "Input, smart render, keyframe copy, full transcode, QuickStream Fix and Joiner", "Broadest preservation route: multiple audio/subtitle tracks, chapters, attachments, cover art, tags and special lossless audio bridges."],
  ["MP4 / M4V", "Input and intelligent/full-transcode output", "H.264 smart-render outputs are automated across MP4. Compatible HEVC/H.264/AV1 delivery depends on codec tags and selected tracks. Text subtitles are converted to MOV_TEXT."],
  ["QuickTime MOV", "Input and intelligent/full-transcode output", "Automated H.264 smart-render output. Text subtitles convert to MOV_TEXT; incompatible bitmap tracks are rejected with alternatives."],
  ["MPEG-TS / TS", "Input, smart-render/remux output and elementary multiplex", "Native route includes MPEG-2/H.264/HEVC plus compatible broadcast audio. Verified transcode choices include H.264 or MPEG-2 with AAC, AC-3 or MP2."],
  ["Blu-ray M2TS / MTS", "Input and intelligent/remux output", "Preflight allows native MPEG-2/H.264/HEVC/VC-1 video and compatible Blu-ray audio/subtitles. Verified fallback choices include MPEG-2/H.264/HEVC + AC-3; 16/24-bit Blu-ray LPCM preservation is tested."],
  ["MPEG Program Stream / MPG / MPEG / VOB", "Input and intelligent/remux output", "DVD-originated MPEG-2 editing is supported, including the validated 16-bit DVD-LPCM route. DVD authoring, menus and disc burning are excluded."],
  ["AVI", "Input", "Used by a signed H.264 + MP3 input case. SmartCut64 does not expose AVI as a standard Save Video destination."],
  ["WebM", "Input", "VP9/AV1-origin media can be edited; the normal broad preservation output is Matroska MKV. No dedicated .webm Save profile is claimed."],
  ["WTV and DVR-MS", "Validated packet-remux output profiles", "Stream codecs, packets and timing are checked. Recorder/platform-specific metadata still benefits from device-diverse fixtures."],
  ["Topfield REC", "Validated transport-stream representation", "Packet-remux output is covered; proprietary recorder-header variants require matching hardware fixtures."],
  ["MXF", "Validated packet-remux output", "Stream/timing checks allow only explicitly permitted PCM rechunking."],
  ["Elementary streams", "Mux and demux", "MPEG-2, H.264, HEVC, AV1, VP9, AAC, AC-3/E-AC-3, MP2/MP3, FLAC, Opus, PCM, DTS and TrueHD. Missing raw timestamps are generated from codec frame duration."],
] as const;

const signedCases = [
  "MPEG-2 + AC-3 from TS",
  "MPEG-2 + MP2 from M2TS",
  "MPEG-2 + PCM S16LE from MKV",
  "H.264 + AAC + AC-3 from MP4",
  "H.264 + E-AC-3 from MP4",
  "H.264 + DTS from MKV",
  "H.264 + MP2 from TS",
  "H.264 + MP3 from AVI",
  "H.264 + PCM S16LE from MKV",
  "HEVC Main 8-bit with no audio from MKV",
  "HEVC Main10 + TrueHD + two DTS-family tracks + PGS from MKV",
  "HEVC Main10 + ALAC + Dolby Vision Profile 8.1 from MP4",
  "VP9 + PCM S16LE from MKV",
  "HEVC Main 8-bit + AAC from MKV",
  "AV1 8-bit + Opus from MKV",
  "AV1 10-bit + FLAC from MKV",
  "VP9 8-bit + Vorbis from MKV",
  "VP9 10-bit + Opus from MKV",
  "MPEG-2 DVD + 16-bit DVD LPCM from VOB",
] as const;

export function FormatMatrix() {
  return <div className="section format-page"><div className="content-wide">
    <section className="format-overview" aria-labelledby="matrix-status">
      <div>
        <p className="eyebrow">Current pre-launch release candidate</p>
        <h2 id="matrix-status">Broad codec support, qualified by operation</h2>
        <p>The packaged engine is based on FFmpeg 9.0 with x264, x265, libaom, libvpx and libdovi. A format is not called “supported” merely because FFmpeg recognises its name: SmartCut64 gates the actual edit, copy, encode and delivery route.</p>
      </div>
      <div className="format-stats" aria-label="Current compatibility evidence">
        <div><strong>19/19</strong><span>signed mid-GOP player cases</span></div>
        <div><strong>22</strong><span>real-library audio signatures decoded</span></div>
        <div><strong>5</strong><span>Blu-ray LPCM player cases</span></div>
        <div><strong>91</strong><span>configured release tests</span></div>
      </div>
    </section>

    <nav className="format-jumps" aria-label="Format support sections">
      <a href="#video-codecs">Video codecs</a><a href="#audio-codecs">Audio codecs</a><a href="#containers">Containers</a><a href="#subtitles-hdr">Subtitles &amp; HDR</a><a href="#verification">Verification</a><a href="#limits">Important limits</a>
    </nav>

    <section className="matrix-section" aria-labelledby="legend-heading">
      <h2 id="legend-heading">How to read the tables</h2>
      <div className="support-legend">
        <div><Badge tone="verified">Verified</Badge><span>Exercised by the current automated or signed player evidence.</span></div>
        <div><Badge tone="smart">Smart render</Badge><span>Encode unsafe cut-boundary pictures; copy safe interior GOPs.</span></div>
        <div><Badge tone="copy">Copy / preserve</Badge><span>Retain packets or losslessly bridge payloads without a lossy conversion.</span></div>
        <div><Badge tone="transcode">Transcode</Badge><span>Decode and encode all kept media, normally to change codec or format.</span></div>
        <div><Badge tone="conditional">Conditional</Badge><span>Depends on profile, container, driver, device or the actual stream set.</span></div>
        <div><Badge tone="not-claimed">Not claimed</Badge><span>SmartCut64 deliberately does not promise that operation.</span></div>
      </div>
      <StatusNote><strong>Container rules still win.</strong> A supported codec can be incompatible with a requested destination. Save Video preflights every selected stream and offers a compatible container, track deselection or a working transcode instead of silently writing private or unplayable data.</StatusNote>
    </section>

    <section className="matrix-section" id="video-codecs">
      <div className="matrix-heading"><div><p className="eyebrow">Picture formats</p><h2>Video codec support</h2></div><p>Smart-render boundary GOPs always use the validated software encoder. Optional GPU encoders apply only to full transcodes and remain off by default.</p></div>
      <div className="matrix-wrap"><table className="support-matrix video-matrix"><thead><tr><th>Codec / tested profiles</th><th>Open, edit and preview</th><th>Smart-render route</th><th>Full-transcode output</th><th>Evidence and qualification</th></tr></thead><tbody>{videoRows.map((row) => <tr key={row.codec}><td><strong>{row.codec}</strong><span>{row.profiles}</span></td><td>{row.edit}</td><td>{row.smart}</td><td>{row.transcode}</td><td>{row.evidence}</td></tr>)}</tbody></table></div>
    </section>

    <section className="matrix-section" id="audio-codecs">
      <div className="matrix-heading"><div><p className="eyebrow">Sound formats</p><h2>Audio codec support</h2></div><p>Compressed audio cuts use complete-frame overlap copy and retiming; sample-accurate splicing inside a compressed audio frame is not claimed. A transcode profile intentionally encodes the primary audio track.</p></div>
      <div className="matrix-wrap"><table className="support-matrix"><thead><tr><th>Codec</th><th>Read / preview</th><th>Smart export</th><th>Transcode output</th><th>Evidence and qualification</th></tr></thead><tbody>{audioRows.map(([codec, read, copy, encode, note]) => <tr key={codec}><td><strong>{codec}</strong></td><td><Badge tone={read.startsWith("Opaque") ? "not-claimed" : "verified"}>{read}</Badge></td><td><Badge tone="copy">{copy}</Badge></td><td><Badge tone={encode.startsWith("No ") ? "not-claimed" : "transcode"}>{encode}</Badge></td><td>{note}</td></tr>)}</tbody></table></div>
    </section>

    <section className="matrix-section" id="containers">
      <div className="matrix-heading"><div><p className="eyebrow">Files and delivery</p><h2>Container and elementary-stream support</h2></div><p>“Input” means the editor can probe/index a compatible stream set. “Output” means SmartCut64 has an explicit mux/remux route with validation—not that every codec can technically live in that container.</p></div>
      <div className="matrix-wrap"><table className="support-matrix container-matrix"><thead><tr><th>Container / extension</th><th>Implemented operations</th><th>Compatibility detail</th></tr></thead><tbody>{containerRows.map(([container, operations, note]) => <tr key={container}><td><strong>{container}</strong></td><td>{operations}</td><td>{note}</td></tr>)}</tbody></table></div>
    </section>

    <section className="matrix-section" id="subtitles-hdr">
      <div className="matrix-heading"><div><p className="eyebrow">Beyond picture and sound</p><h2>Subtitles, captions, HDR and metadata</h2></div><p>Preservation is reported from the actual export. SmartCut64 does not use a generic “metadata copied” claim when a boundary or container changes what can be delivered.</p></div>
      <div className="detail-grid">
        <article><h3>Text subtitles</h3><p><strong>SRT, ASS/SSA, WebVTT and plain text</strong> support overlap trimming and splitting. MP4/MOV delivery converts supported text tracks to MOV_TEXT while preserving language and chapters.</p></article>
        <article><h3>Bitmap subtitles</h3><p><strong>PGS, DVD/VobSub and compatible broadcast subtitle packets</strong> use conservative whole-event overlap copy. Matroska is the broad preservation route; incompatible MP4/MOV delivery is rejected with Matroska, deselection or OCR guidance.</p></article>
        <article><h3>HDR10 and HLG</h3><p>BT.2020/PQ or ARIB STD-B67 colour signalling, mastering-display data and content-light metadata are indexed and checked across tested HEVC boundaries.</p></article>
        <article><h3>HDR10+</h3><p>Per-packet dynamic T.35 payload coverage, registration header, source-map alignment and physical boundary presence have deterministic validation.</p></article>
        <article><h3>Dolby Vision</h3><p><strong>Profile 8.1</strong> RPU NALs and Matroska DOVI configuration are preserved. At a discontinuity the first RPU is marked as a scene refresh and its CRC is recomputed; later RPUs remain exact. No Dolby certification is claimed.</p></article>
        <article><h3>Captions and file metadata</h3><p>A53 closed-caption payload equality is tested at H.264/HEVC boundaries. Chapters are trimmed/retimed; tags, dispositions, attachments, fonts and cover art are preserved where the destination supports them.</p></article>
      </div>
    </section>

    <section className="matrix-section" id="verification">
      <div className="matrix-heading"><div><p className="eyebrow">Player evidence</p><h2>The signed 19-case mid-GOP matrix</h2></div><p>Every case starts and ends away from a safe random-access boundary, so success requires both newly encoded boundary frames and copied interior frames. Each output decodes fully with FFmpeg and visibly advances at three positions in MPC-BE.</p></div>
      <ol className="evidence-list">{signedCases.map((item) => <li key={item}>{item}</li>)}</ol>
      <StatusNote tone="good"><strong>All 19 passed.</strong> The separate Blu-ray LPCM matrix adds five 16/24-bit layout/rate combinations, each with a mid-GOP MPEG-2 edit, full decode and three distinct MPC-BE captures.</StatusNote>
    </section>

    <section className="matrix-section" id="limits">
      <div className="matrix-heading"><div><p className="eyebrow">Honest boundaries</p><h2>What this matrix does not promise</h2></div><p>A codec label alone cannot guarantee a good edit. Profiles, damaged timing, encryption, proprietary recorder headers, unusual side data, driver capability and the destination container all matter.</p></div>
      <ul className="limit-grid"><li>No DVD authoring, menu creation, VIDEO_TS construction or disc burning.</li><li>No 20/24-bit DVD-LPCM-to-Matroska bridge claim; the verified DVD LPCM route is 16-bit.</li><li>No sample-accurate cut inside a copied compressed-audio frame.</li><li>No DTS, Dolby, Apple, Blu-ray or hardware-vendor certification claim.</li><li>No guarantee that every proprietary WTV/DVR-MS/REC variant retains every private field.</li><li>No silent “success” when a selected stream is incompatible or post-export validation fails.</li></ul>
      <p className="matrix-footnote">This page describes the current pre-launch release candidate and its retained evidence. The final public release record will identify the exact build, dependency inventory, tests and checksums to which these claims apply.</p>
    </section>
  </div></div>;
}
