require "test_helper"

class BoardTest < ActiveSupport::TestCase
  def setup
    @game = games(:one) # fixturesから取得
    @austria = users(:austria)
    @germany = users(:germany)
    @russia = users(:russia)

    # 参加者作成（fixtureに依存せず動的に作る方が安全だが簡略化）
    @p_austria = Participant.create!(game: @game, user: @austria, power: "AUSTRIA")
    @p_germany = Participant.create!(game: @game, user: @germany, power: "GERMANY")
    @p_russia = Participant.create!(game: @game, user: @russia, power: "RUSSIA")
  end

  test "should create global board automatically" do
    new_game = Game.create!(
      title: "New Game",
      status: "recruiting",
      turn_schedule: "0,12",
      participants_count: 7,
      victory_sc_count: 18,
      scoring_system: "none"
    )
    assert new_game.boards.global.exists?
  end

  test "should create negotiation board" do
    board = @game.boards.new(board_type: "negotiation", created_by_participant_id: @p_austria.id)
    assert board.save

    # メンバー追加
    board.board_memberships.create!(participant: @p_austria, joined_at: Time.current)
    board.board_memberships.create!(participant: @p_germany, joined_at: Time.current)

    assert board.member?(@p_austria)
    assert board.member?(@p_germany)
    assert_not board.member?(@p_russia)
  end

  test "should not allow duplicate member combination" do
    # 既存のボード: Austria & Germany
    board1 = @game.boards.create!(board_type: "negotiation", created_by_participant_id: @p_austria.id)
    board1.board_memberships.create!(participant: @p_austria, joined_at: Time.current)
    board1.board_memberships.create!(participant: @p_germany, joined_at: Time.current)

    # 新しいボード: Austria & Germany (重複)
    board2 = @game.boards.new(board_type: "negotiation", created_by_participant_id: @p_austria.id)
    board2.board_memberships.build(participant: @p_austria, joined_at: Time.current)
    board2.board_memberships.build(participant: @p_germany, joined_at: Time.current)

    assert_not board2.valid?
    assert_includes board2.errors[:base], "同じメンバー構成の掲示板がすでに存在します"

    # メンバーが違えばOK: Austria & Russia
    board3 = @game.boards.new(board_type: "negotiation", created_by_participant_id: @p_austria.id)
    board3.board_memberships.build(participant: @p_austria, joined_at: Time.current)
    board3.board_memberships.build(participant: @p_russia, joined_at: Time.current)

    assert board3.valid?
  end

  test "unread count" do
    board = @game.boards.create!(board_type: "negotiation", created_by_participant_id: @p_austria.id)
    m_austria = board.board_memberships.create!(participant: @p_austria, joined_at: Time.current)

    # 投稿前の未読は0
    assert_equal 0, board.unread_count_for(@p_austria)

    # 投稿作成
    post1 = board.board_posts.create!(participant: @p_austria, body: "Hello")
    post2 = board.board_posts.create!(participant: @p_austria, body: "World")

    # Last read が nil なので全件未読扱いにはならない（ロジック上は last_read_post_id || 0 なので `id > 0` の件数になる）
    # `id` はauto incrementで正の数なので、全件カウントされるはず
    assert_equal 2, board.unread_count_for(@p_austria)

    # 既読更新
    m_austria.mark_read!(post1.id)
    assert_equal 1, board.unread_count_for(@p_austria)

    m_austria.mark_read!(post2.id)
    assert_equal 0, board.unread_count_for(@p_austria)
  end
end
