class Api::V1::Projects::Pulls::PullsController < Api::V1::BaseController
  before_action :require_public_and_member_above

  def index  
    @pulls = Api::V1::Projects::Pulls::ListService.call(@project, query_params)
    @pulls = kaminari_paginate(@pulls)
  end

  before_action :load_pull_request, only: [:show, :files] 

  def show 
   @result_object = Api::V1::Projects::Pulls::GetService.call(@project, @pull_request, current_user&.gitea_token)
   @last_review = @pull_request.reviews.order(created_at: :desc).take
  end

  def files
    if params[:filepath].present? 
      @result_object = $gitea_hat_client.get_repos_pulls_files_by_owner_repo_index_filepath(@project&.owner.login, @project&.identifier, @pull_request.gitea_number, CGI.escape(params[:filepath]), {query: {token: current_user&.gitea_token}})
    else
      @result_object = $gitea_hat_client.get_repos_pulls_files_by_owner_repo_index(@project&.owner.login, @project&.identifier, @pull_request.gitea_number, {query: {isNew: "true",token: current_user&.gitea_token, page: page, limit: limit}})
    end
  end 

  private 
  def query_params 
    params.permit(:status, :keyword, :priority_id, :issue_tag_id, :version_id, :reviewer_id, :sort_by, :sort_direction)
  end
end