class Api::V1::Projects::Pulls::Versions::GetDiffService < ApplicationService 

  attr_reader :project, :pull_request, :version_id, :owner, :repo, :index, :filepath, :token
  attr_accessor :gitea_data 

  def initialize(project, pull_request, version_id, params, token=nil)
    @project = project 
    @pull_request = pull_request
    @version_id = version_id
    @owner = project&.owner.login 
    @repo = project&.identifier 
    @index = pull_request.gitea_number 
    @filepath = params[:filepath]
    @token = token
  end

  def call 
    load_gitea_data 

    gitea_data
  end

  private 
  def request_params
    params = {
      access_token: token
    }
    params.merge!(filepath: filepath) if filepath.present?
    params
  end

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