require_relative "test_helper"

class BoardEnsurerTest < Minitest::Test
  include BacklogTestHelpers

  def test_creates_missing_project_board_lists_and_labels
    client = FakePlankaClient.new
    ensurer = Backlog::BoardEnsurer.new(client: client, human_user_id: "human-1")

    result = ensurer.ensure("llf-schema", project_name: "Dev")

    assert result.created
    assert_equal "llf-schema", result.board.name
    assert_equal Backlog::BoardSpec::LISTS, result.lists_created
    assert_equal Backlog::BoardSpec::LABEL_NAMES, result.labels_created

    project = client.projects_store.first
    assert_equal "Dev", project.name
  end

  def test_rerun_is_a_noop
    client = FakePlankaClient.new
    ensurer = Backlog::BoardEnsurer.new(client: client, human_user_id: "human-1")
    ensurer.ensure("llf-schema", project_name: "Dev")

    result = ensurer.ensure("llf-schema", project_name: "Dev")

    refute result.created
    assert_empty result.lists_created
    assert_empty result.labels_created
    assert_equal 1, client.boards_store.size
    assert_equal Backlog::BoardSpec::LISTS.size, client.lists_store.size
    assert_equal Backlog::BoardSpec::LABEL_NAMES.size, client.labels_store.size
  end

  def test_reuses_existing_project
    client = FakePlankaClient.new
    ensurer = Backlog::BoardEnsurer.new(client: client, human_user_id: "human-1")
    ensurer.ensure("board-a", project_name: "Dev")

    ensurer.ensure("board-b", project_name: "Dev")

    assert_equal 1, client.projects_store.size
    assert_equal 2, client.boards_store.size
  end

  def test_applies_owner_manager_visibility_fix_on_new_project
    client = FakePlankaClient.new
    ensurer = Backlog::BoardEnsurer.new(client: client, human_user_id: "human-1")

    ensurer.ensure("llf-schema", project_name: "Dev")

    project = client.projects_store.first
    assert_includes client.managers_for(project.id), "human-1"
    assert_nil project.owner_project_manager_id
  end

  def test_semi_label_is_part_of_enforced_set
    assert_includes Backlog::BoardSpec::LABEL_NAMES, "semi"
    assert_includes Backlog::BoardSpec::LABEL_NAMES, "hitl"
    assert_includes Backlog::BoardSpec::LABEL_NAMES, "afk-ready"
  end

  def test_label_color_falls_back_to_next_candidate_on_rejection
    first_choice = Backlog::BoardSpec::LABEL_COLOR_CANDIDATES.fetch("P0").first
    client = FakePlankaClient.new(reject_colors: [first_choice])
    ensurer = Backlog::BoardEnsurer.new(client: client, human_user_id: "human-1")

    ensurer.ensure("llf-schema", project_name: "Dev")

    p0 = client.labels_store.find { |l| l.name == "P0" }
    refute_equal first_choice, p0.color
  end


  def test_activate_renames_archived_board_back
    client = FakePlankaClient.new
    ensurer = Backlog::BoardEnsurer.new(client: client, human_user_id: "human-1")
    ensurer.ensure("llf-schema", project_name: "Dev")
    board = client.boards_store.first
    client.boards.update(board.id, name: "archived--llf-schema")

    result = ensurer.activate("llf-schema")

    assert_equal "llf-schema", result.name
  end

  def test_archive_renames_active_board
    client = FakePlankaClient.new
    ensurer = Backlog::BoardEnsurer.new(client: client, human_user_id: "human-1")
    ensurer.ensure("llf-schema", project_name: "Dev")

    result = ensurer.archive("llf-schema")

    assert_equal "archived--llf-schema", result.name
  end

  def test_ensure_never_touches_archived_boards
    client = FakePlankaClient.new
    ensurer = Backlog::BoardEnsurer.new(client: client, human_user_id: "human-1")
    ensurer.ensure("llf-schema", project_name: "Dev")
    ensurer.archive("llf-schema")

    result = ensurer.ensure("llf-schema", project_name: "Dev")

    assert result.created
    assert_equal 2, client.boards_store.size
    archived = client.boards_store.find { |b| b.name == "archived--llf-schema" }
    refute_nil archived
  end

  def test_ensure_rejects_archived_prefixed_name
    client = FakePlankaClient.new
    ensurer = Backlog::BoardEnsurer.new(client: client, human_user_id: "human-1")

    assert_raises(ArgumentError) { ensurer.ensure("archived--x", project_name: "Dev") }
  end
end
