class Api::V1::Projects::Branches::RestoreService < ApplicationService
   
  include ActiveModel::Model

  attr_accessor :project, :token, :owner, :repo, :branch_id, :branch_name 
  attr_accessor :gitea_data

  validates :branch_id, :branch_name, presence: true 

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

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

    true
  end

  private 
  def request_params 
    {
      access_token: token 
    }
  end

  def request_body 
    {
      branch_id: branch_id,
      name: branch_name,
    }
  end

  def excute_data_to_gitea
    begin
      @gitea_data = $gitea_hat_client.post_repos_branches_restore_by_owner_repo(owner, repo, {query: request_params, body: request_body.to_json})
   rescue => e
     raise Error, '恢复分支失败!'
    end
  end
end