class Api::V1::Projects::CodeChecksController < Api::V1::BaseController
  before_action :require_public_and_member_above
  before_action :load_project
  before_action -> { verification_with_setting('open_huawei_check_users') }

  before_action do
    @client = Api::V1::Projects::CodecheckService.new(@project)
  end
  def index
    render_ok({check:true})
  end
  def create
    render_client do
      @client.create_task(params["branch"])
    end
  end

  def run_task
    render_client do
      @client.run_task(params["branch"],params["task_id"])
    end
  end

  def query_task
    render_client do
      @client.query_task(params["task_id"])
    end
  end
  #----- 缺陷管理 ------
  def defects_summary
    allowed_query = %w[job_id merge_id]
    query_params, _ = filter_params(allowed_query_keys: allowed_query)
    render_client do
      @client.defects_summary(query_params)
    end
  end

  def metrics_summary
    render_client do
      @client.metrics_summary
    end
  end

  def defects_detail
    allowed_query = %w[offset limit status_ids severity delay_status]
    query_params, _ = filter_params(allowed_query_keys: allowed_query)

    render_client do
      @client.defects_detail(query_params)
    end
  end

  def defects_statistic
    render_client do
      @client.defects_statistic
    end
  end

  def defect_status #put
    allowed_body = %w[defect_id defect_status]
    _, body_params = filter_params(allowed_body_keys: allowed_body)
    render_client do
      @client.defect_status(body_params)
    end
  end
  #------ 代码问题 ----
  def issue_status  #post
    allowed_body = %w[mergeId jobId status comment mergeKey operator]
    _, body_params = filter_params(allowed_body_keys: allowed_body)
    render_client do
      @client.issue_status(body_params)
    end
  end

  def issue_list_by_filter #post
    allowed_body = %w[mergeId jobId pageSize page languages ruleIds authors isNew statusIds severities delayStatus fileNames userTags cwes statuses]
    _, body_params = filter_params(allowed_body_keys: allowed_body)
    render_client do
      @client.issue_list_by_filter(body_params)
    end
  end

  def issue_filter #post
    allowed_body = %w[mergeId jobId facets languages ruleIds authors isNew statusIds severities delayStatus fileNames userTags cwes]
    _, body_params = filter_params(allowed_body_keys: allowed_body)
    render_client do
      @client.issue_filter(body_params)
    end
  end

  def defect_metric_trend #get
    allowed_query = %w[start_date end_date dimension]
    query_params, _ = filter_params(allowed_query_keys: allowed_query)
    render_client do
      @client.defect_metric_trend(query_params)
    end
  end

  def criterion_rule
    allowed_query = %w[criterion_rule_id]
    query_params, _ = filter_params(allowed_query_keys: allowed_query)
    render_client do
      @client.criterion_rule(query_params)
    end
  end

  def generate_pdf
    render_client do
      @client.generate_pdf
    end
  end

  def query_generate_pdf
    render_client do
      @client.query_generate_pdf(params[:async_job_id])
    end
  end

  def download_pdf
    allowed_query = %w[job_file]
    query_params, _ = filter_params(allowed_query_keys: allowed_query)
    @result = @client.download_pdf(query_params)
    send_data(
      @result[:data],
      filename: @result[:filename],
      type: @result[:content_type],
      disposition: "attachment"
    )
  end

  def measure_total
    allowed_body = %w[jobId search]
    _, body_params = filter_params(allowed_body_keys: allowed_body)
    render_client do
      @client.measure_total(body_params)
    end
  end

  def related_duplicate_blocks
    allowed_query = %w[job_id file_path block_id duplication_type]
    query_params, _ = filter_params(allowed_query_keys: allowed_query)
    render_client do
      @client.related_duplicate_blocks(query_params)
    end
  end

  def measure_list
    allowed_body = %w[filterType page pageSize]
    _, body_params = filter_params(allowed_body_keys: allowed_body)
    render_client do
      @client.measure_list(body_params)
    end
  end
  private

  # 过滤参数：可同时处理 query 和 body
  def filter_params(allowed_query_keys: [], allowed_body_keys: [])
    query_params = request.query_parameters.slice(*allowed_query_keys)
    body_params  = request.request_parameters.slice(*allowed_body_keys)
    [query_params, body_params]
  end

  def render_client
    @code, @result = yield  # 执行传入的 block
    if @code == "200"
      render :index
    else
      render_error(@result["error_msg"] + @result["error_code"])
    end
  end
end