class Api::V1::Projects::CompareService < ApplicationService 
  include ActiveModel::Model
  attr_reader :project, :from, :to, :token, :owner, :repo 
  attr_accessor :gitea_data

  validates :from, :to, presence: true

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

  def call 
    raise Error, errors.full_messages.join(",") unless valid?
    load_gitea_data
    raise Error, '获取对比信息失败！' unless gitea_data.is_a?(Hash)
    gitea_data
  end

  private 
  def request_params 
    {
      access_token: token
    }
  end

  def load_gitea_data 
    @gitea_data = $gitea_hat_client.get_repos_compare_by_owner_repo_baseref_headref(owner, repo, to, from, {query: request_params}) rescue nil
  end
end