# frozen_string_literal: true
require 'json'
require 'digest'

module Evidence
  module_function

  def rows(dir)
    File.readlines(File.join(dir, '../inference-roadmap-bench.jsonl')).map { |line| JSON.parse(line) }
  end

  def check_rows(rows)
    expected = [["bench-ollama-qwen3-4b", nil, 1]] * 2 + [2,4,8].map { |n| ["bench-ollama-qwen3-4b", nil, n] }
    %w[gpu cpu].each do |backend|
      [1,4,8].each do |slots|
        (backend == 'gpu' ? [1,4,8,16] : [1,4,8]).each do |n|
          expected << ["bench-llamaserver-qwen3-4b-#{backend}", slots, n]
        end
      end
    end
    raise 'run matrix/order/count mismatch' unless rows.map { |r| r.values_at('run','slots','concurrency') } == expected
    rows.each do |r|
      n, tokens, wall, tps, latency = r.values_at('concurrency','total_gen_tokens','wall_s','aggregate_tps','max_latency_s')
      raise 'token count mismatch' unless tokens == n * 200
      raise 'invalid time/rate' unless [wall,tps,latency].all? { |v| v.is_a?(Numeric) && v.finite? && v.positive? }
      # Published wall seconds are rounded to 0.01 and rates to 0.1.
      low, high = tokens / (wall + 0.005) - 0.05, tokens / (wall - 0.005) + 0.05
      raise 'aggregate does not match tokens/wall' unless (low..high).cover?(tps)
      raise 'latency exceeds wall' unless latency <= wall + 0.01
      rates = r.fetch('per_req_decode_tps')
      raise 'per-request rates mismatch' unless rates.size == n && rates.all? { |v| v.is_a?(Numeric) && v.finite? && v.positive? }
      if r['run'] == 'bench-ollama-qwen3-4b'
        raise 'prefill rates mismatch' unless r.fetch('prefill_tps').size == n
      end
    end
    true
  end

  def check(dir)
    manifest = JSON.parse(File.read(File.join(dir, 'manifest.json')))
    manifest.fetch('sha256').each do |path, digest|
      raise "artifact checksum mismatch: #{path}" unless Digest::SHA256.file(File.join(dir,path)).hexdigest == digest
    end
    data = rows(dir)
    check_rows(data)
    html = File.read(File.join(dir, '../inference-roadmap.html'))
    [1,4,8].each do |slots|
      row = data.find { |r| r['run'] == 'bench-llamaserver-qwen3-4b-gpu' && r['slots'] == slots && r['concurrency'] == 16 }
      cells = [slots.to_s, row.fetch('aggregate_tps').to_s, format('%.1f', row.fetch('max_latency_s'))]
      fragment = html[/<tr data-benchmark-slots="#{slots}">(.*?)<\/tr>/m,1]
      raise "fixed-16 table mismatch: #{slots}" unless fragment && fragment.scan(/<td[^>]*>(.*?)<\/td>/m).flatten == cells
    end
    true
  end
end

if $PROGRAM_NAME == __FILE__
  Evidence.check(__dir__)
  puts 'PASS: original artifact hashes, 26-row matrix, arithmetic and fixed-16 article table (no inference run).'
end
