class Api::V1::Projects::CodeStats::ListService < ApplicationService 

  attr_reader :project, :ref, :owner, :repo, :token, :page, :limit
  attr_accessor :gitea_data 

  def initialize(project, params, token=nil) 
    @project = project 
    @ref = params[:ref] 
    @owner = project&.owner.login
    @repo = project&.identifier
    @token = token
  end

  def call 
    load_gitea_data 

    gitea_data
  end

  private 
  def request_params 
    param = {
      access_token: token
    }
    param.merge!(ref: ref) if ref.present?

    param
  end

  def load_gitea_data 
    @gitea_data = $gitea_hat_client.get_repos_code_stats_by_owner_repo(owner, repo, {query: request_params}) rescue nil
    raise Error, '获取贡献者贡献度失败！' unless @gitea_data.is_a?(Hash)
  end
end