class MembersController < ApplicationController
  before_action :require_login
  before_action :load_project
  before_action :find_user_with_id, only: %i[create remove change_role]
  before_action :check_user_profile_completed, only: [:create]
  before_action :operate!
  before_action :check_member_exists!, only: %i[create]
  before_action :check_member_not_exists!, only: %i[remove change_role]

  def create
    interactor = Projects::AddMemberInteractor.call(@project.owner, @project, @user)
    SendTemplateMessageJob.perform_later('ProjectJoined', current_user.id, @user.id, @project.id) if Site.has_notice_menu?
    SendTemplateMessageJob.perform_later('ProjectMemberJoined', current_user.id, @user.id, @project.id) if Site.has_notice_menu?
    render_response(interactor)
  rescue Exception => e
    uid_logger_error(e.message)
    tip_exception(e.message)
  end

  def index
    scope = @project.members.includes(:roles, user: :user_extension)
    search = params[:search].to_s.downcase
    role = params[:role].to_s
    scope = scope.joins(:user).merge(User.like(search))
    scope = scope.joins(:roles).where("roles.name LIKE ?", "%#{role}%") if role.present?

    @total_count = scope.size
    @members = paginate(scope)
    if @project.owner.is_a?(Organization) && (params[:page].to_i == 1 || params[:page].blank?) && !@project.members.exists?(user_id: current_user.id)
      @current_user_header_team = Team.joins(:team_users, :team_projects).where(team_projects: {project_id: @project.id}, team_users: {user_id: current_user.id}).order(authorize: :desc).take
    end
  end

  def remove
    interactor = Projects::DeleteMemberInteractor.call(@project.owner, @project, @user)
    SendTemplateMessageJob.perform_later('ProjectLeft', current_user.id, @user.id, @project.id) if Site.has_notice_menu?
    SendTemplateMessageJob.perform_later('ProjectMemberLeft', current_user.id, @user.id, @project.id) if Site.has_notice_menu?
    render_response(interactor)
  rescue Exception => e
    uid_logger_error(e.message)
    tip_exception(e.message)
  end

  def change_role
    interactor = Projects::ChangeMemberRoleInteractor.call(@project.owner, @project, @user, params[:role])
    SendTemplateMessageJob.perform_later('ProjectRole', current_user.id, @user.id, @project.id, message_role_name) if Site.has_notice_menu?
    render_response(interactor)
  rescue Exception => e
    uid_logger_error(e.message)
    tip_exception(e.message)
  end

  private
  def can_operate?
    current_user.project_manager?(@project)
  end

  def member_exists?
    @project.members.exists?(user_id: params[:user_id])
  end

  def operate!
    return render_forbidden('你不是管理员，没有权限操作') unless can_operate?
  end

  def check_member_exists!
    @current_user_header_team = Team.joins(:team_users, :team_projects).where(team_projects: {project_id: @project.id}, team_users: {user_id: current_user.id}).order(authorize: :desc).take
    return render_error("#{@user&.nickname}已经是项目成员") if member_exists? || (params[:user_id].to_i == current_user.id && @current_user_header_team.present?)
  end

  def check_member_not_exists!
    @current_user_header_team = Team.joins(:team_users, :team_projects).where(team_projects: {project_id: @project.id}, team_users: {user_id: current_user.id}).order(authorize: :desc).take
    return render_error("用户为组织成员，请到组织下操作！") if (params[:user_id].to_i == current_user.id && @current_user_header_team.present?) && !member_exists?
    return render_error("#{@user&.nickname}还不是项目成员") unless member_exists?
  end

  def check_user_profile_completed
    require_user_profile_completed(@user)
  end

  def message_role_name 
    case params[:role]
    when 'Manager' then '管理员'
    when 'Developer' then '开发者'
    when 'Reporter' then '报告者'
    else 
      ''
    end
  end
end
