class BoardPostsController < ApplicationController
  include BoardAccessible

  before_action :require_login
  before_action :set_game
  before_action :set_board
  before_action :set_current_participant

  def create
    # 参加チェック
    unless @board.member?(@current_participant) && @game.status == "in_progress"
      return redirect_to game_board_path(@game, @board), alert: "投稿権限がありません"
    end

    @post = @board.board_posts.new(post_params)
    @post.participant = @current_participant

    # フェーズ情報の付与（latest_turnアソシエーションを使用）
    if @game.latest_turn
      @post.phase = @game.latest_turn.phase
    end

    if @post.save
      redirect_to game_board_path(@game, @board) # noticeはチャット的にうるさいので省略
    else
      redirect_to game_board_path(@game, @board), alert: "投稿に失敗しました"
    end
  end

  private

  def post_params
    params.require(:board_post).permit(:body)
  end
end
