class Api::V1::Projects::Commits::DiffService < ApplicationService 
  include ActiveModel::Model

  attr_accessor :project, :sha, :token, :owner, :repo 
  attr_accessor :gitea_data 

  validates :sha, presence: true

  def initialize(project, sha, token = nil)
    @project = project 
    @sha = sha
    @owner = project&.owner.login 
    @repo = project&.identifier 
    @token = token
  end

  def call 
    raise Error, errors.full_messages.join(",") unless valid?
    load_gitea_data

    gitea_data
  end

  private 
  def request_params
    {
      access_token: token
    }
  end

  def load_gitea_data 
    @gitea_data = $gitea_hat_client.get_repos_commits_diff_by_owner_repo_sha(owner, repo, sha, {query: request_params}) rescue nil
    raise Error, '获取提交对比失败！' unless @gitea_data.is_a?(Hash)
  end

end