class Api::V1::Projects::CommitsController < Api::V1::BaseController 
  before_action :require_public_and_member_above, only: [:index, :diff, :recent, :files]

  def index 
    @result_object = Api::V1::Projects::Commits::ListService.call(@project, {page: page, limit: limit, sha: params[:sha]}, current_user&.gitea_token)
    puts @result_object
  end

  def diff 
    @result_object = Api::V1::Projects::Commits::DiffService.call(@project, params[:sha], current_user&.gitea_token)
  end

  def files 
    if params[:filepath].present? 
      @result_object = $gitea_hat_client.get_repos_commits_files_by_owner_repo_sha_filepath(@project&.owner.login, @project&.identifier, params[:sha], CGI.escape(params[:filepath]), {query: {token: current_user&.gitea_token}})
    else
      @result_object = $gitea_hat_client.get_repos_commits_files_by_owner_repo_sha(@project&.owner.login, @project&.identifier, params[:sha], {query: {token: current_user&.gitea_token, page: page, limit: limit}})
    end
  end

  def recent
    hash = Api::V1::Projects::Commits::RecentService.call(@project, {keyword: params[:keyword], page: page, limit: limit}, current_user&.gitea_token)
    @result_object = hash[:result]
    @object_detail = hash[:detail]
    puts @object_detail
  end
end