class Api::V1::Projects::BlameService < ApplicationService 
  include ActiveModel::Model

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

  validates :sha, :filepath, presence: true

  def initialize(project, sha, filepath, token=nil)
    @project = project 
    @owner = project&.owner.login 
    @repo = project&.identifier 
    @sha = sha 
    @filepath = filepath
    @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,
      sha: sha, 
      filepath: filepath 
    }
  end

  def load_gitea_data 
    @gitea_data = $gitea_hat_client.get_repos_blame_by_owner_repo(owner, repo, {query: request_params})
    raise Error, '获取项目blame失败！' unless @gitea_data.is_a?(Hash)
  end
end