local Unit = require('Module:UnitTests')

-- CHANGE THIS if you want to point at the non-sandbox module
local MOD = 'Module:Build bracket/sandbox'

local M = require(MOD)

-- Minimal frame that your module expects
local function makeFrame(fargs, pargs)
  local parent = { args = pargs or {} }
  return {
    args = fargs or {},
    getParent = function() return parent end,
    -- Render.buildTable calls frame:extensionTag for TemplateStyles;
    -- return empty string so tests don't depend on it.
    extensionTag = function() return '' end,
  }
end

local function render(fargs, pargs)
  -- Use the exported _main so we bypass #invoke and still exercise end-to-end
  return M._main(makeFrame(fargs, pargs))
end

-------------------------
-- Test cases
-------------------------

function Unit:test_basic_numbered_one_round()
  local out = render({
    rounds = 1,
    ['col1-matches'] = '1',
    paramstyle = 'numbered',
    -- numbered args (teams then scores, per team):
    [1] = 'Alpha', [2] = '3',
    [3] = 'Beta',  [4] = '1',
  })

  self:equals('renders team Alpha', out:find('Alpha') ~= nil, true)
  self:equals('renders team Beta',  out:find('Beta')  ~= nil, true)
end

function Unit:test_numbered_with_seeds()
  local out = render({
    rounds = 1,
    ['col1-matches'] = '1',
    paramstyle = 'numbered',
    seeds = 'yes',           -- forceseeds
    -- numbered args with seeds first for each team:
    [1] = 'A1', [2] = 'Alpha', [3] = '5',
    [4] = 'B2', [5] = 'Beta',  [6] = '3',
  })

  self:equals('shows seed A1', out:find('>A1<') ~= nil, true)
  self:equals('shows seed B2', out:find('>B2<') ~= nil, true)
end

function Unit:test_groups_from_paths()
  local out = render({
    rounds = 2,
    ['col1-matches'] = '3,7',
    ['col2-matches'] = '5',
    ['col1-col2-paths'] = '(3,7)-5',  -- groups should derive at col1 row-pair
    ['RD1-group1'] = 'Group 1',
    paramstyle = 'numbered',
    -- teams across both rounds (col1 has 2 matches = 4 teams; col2 has 1 match = 2 teams)
    [1]  = 'A', [2]  = '1',
    [3]  = 'B', [4]  = '2',
    [5]  = 'C', [6]  = '3',
    [7]  = 'D', [8]  = '4',
    [9]  = 'E', [10] = '5',
    [11] = 'F', [12] = '6',
  })

  self:equals('group label text appears', out:find('>Group 1<') ~= nil, true)
  -- group cells render as centered text (class brk-center on the <td>)
  self:equals('group cell has center class', out:find('class="[^"]*brk%-center[^"]*"') ~= nil, true)
end

function Unit:test_paths_draw_borders()
  local out = render({
    rounds = 2,
    ['col1-matches'] = '1,2',
    ['col2-matches'] = '1',
    ['col1-col2-paths'] = '1-1,2-1',
    paramstyle = 'numbered',
    [1]='T1',[2]='1',[3]='T2',[4]='2',[5]='T3',[6]='3',[7]='T4',[8]='4',[9]='T5',[10]='5',[11]='T6',[12]='6',
  })

  -- Path <td>s use inline border-width to draw lines; just sanity check it exists
  self:equals('path cells rendered (inline border-width present)',
    out:find('border%-width:%s*%d+px') ~= nil, true)
end

function Unit:test_cross_background()
  local out = render({
    rounds = 2,
    ['col1-matches'] = '1,2',
    ['col2-matches'] = '1',
    ['col1-col2-paths'] = '1-1,2-1',
    ['col1-col2-cross'] = '1',
    paramstyle = 'numbered',
    [1]='AA',[2]='1',[3]='BB',[4]='2',[5]='CC',[6]='3',[7]='DD',[8]='4',[9]='EE',[10]='5',[11]='FF',[12]='6',
  })

  -- Cross cells use linear-gradient backgrounds; just check presence
  self:equals('cross background drawn', out:find('linear%-gradient%(') ~= nil, true)
end

-- (Optional) autocol spacing sanity (does not assert layout, only no crash)
function Unit:test_autocol_ok()
  local out = render({
    rounds = 3,
    autocol = 'y',
    ['col1-matches'] = '1',
    ['col1-col2-paths'] = '1-1',
    ['col2-matches'] = '1',
    paramstyle = 'numbered',
    [1]='X',[2]='1',[3]='Y',[4]='2',[5]='Z',[6]='3',[7]='W',[8]='4',
  })
  self:equals('no empty result, rendered some HTML', type(out) == 'string' and #out > 0, true)
end

return Unit