class Api::V1::Projects::GetService < ApplicationService

  attr_reader :project, :token, :owner, :repo
  attr_accessor :gitea_data, :gitea_branch_tag_count

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

  def call 
    load_gitea_data
    load_gitea_branch_tag_count

    result_object
  rescue 
    raise Error, "服务器错误，请联系系统管理员！"
  end

  private 
  def request_params 
    {
      access_token: token
    }
  end

  def result_object
    {
      full_name: "#{owner}/#{repo}",
      owner: project&.owner,
      ssh_url: gitea_data["ssh_url"],
      clone_url: gitea_data["clone_url"],
      size: gitea_data["size"],
      default_branch: gitea_data["default_branch"],
      empty: gitea_data["empty"],
      branch_count: gitea_branch_tag_count["branch_count"],
      tag_count: gitea_branch_tag_count["tag_count"],
    }
  end

  def load_gitea_data 
    @gitea_data = $gitea_client.get_repos_by_owner_repo(owner, repo, {query: request_params}) rescue nil
  end

  def load_gitea_branch_tag_count 
    @gitea_branch_tag_count = $gitea_hat_client.get_repos_branch_tag_count_by_owner_repo(owner, repo, {query: request_params}) rescue nil
  end
end