class Api::V1::Projects::Pulls::Versions::ListService < ApplicationService 
  
  attr_reader :project, :pull_request, :token, :owner, :repo, :index, :page, :limit
  attr_accessor :gitea_data 

  def initialize(project, pull_request, params, token = nil)
    @project = project 
    @pull_request = pull_request
    @owner = project&.owner.login 
    @repo = project&.identifier
    @page = params[:page] || 1
    @limit = params[:limit] || 15
    @index = pull_request.gitea_number
    @token = token 
  end

  def call 
    load_gitea_data 

    gitea_data
  end

  private 
  def request_params 
    {
      access_token: token, 
      page: page,
      limit: limit
    }
  end

  def load_gitea_data
    @gitea_data = $gitea_hat_client.get_repos_pulls_versions_by_owner_repo_index(owner, repo, index, {query: request_params})
    raise Error, '获取合并请求版本失败！' unless @gitea_data.is_a?(Hash)
  end
end