class Api::V1::Projects::Contributors::StatService < ApplicationService 

  attr_reader :project, :branch, :pass_year, :owner, :repo, :token, :page, :limit
  attr_accessor :gitea_data 

  def initialize(project, params, token=nil) 
    @project = project 
    @branch = params[:branch] 
    @pass_year = params[:pass_year] || 3
    @page = params[:page] || 1
    @limit = params[:limit] || 15
    @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, page: page, limit: limit
    }
    param.merge!(branch: branch) if branch.present?
    param.merge!(pass_year: @pass_year)

    param
  end

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