# == Schema Information
#
# Table name: sync_repository_branches
#
#  id                   :integer          not null, primary key
#  sync_repository_id   :integer
#  gitlink_branch_name  :string(255)
#  external_branch_name :string(255)
#  sync_time            :datetime
#  sync_status          :integer          default("0")
#  reposync_branch_id   :integer
#  created_at           :datetime         not null
#  updated_at           :datetime         not null
#  enable               :boolean          default("1")
#
# Indexes
#
#  index_sync_repository_branches_on_sync_repository_id  (sync_repository_id)
#

class SyncRepositoryBranch < ApplicationRecord

  belongs_to :sync_repository

  before_destroy :unbind_reposyncer

  enum sync_status: {success: 1, failure: 2}


  def unbind_reposyncer
    if self.sync_repository.sync_direction.to_i == 1
      Reposync::DeleteBranchService.call(self.sync_repository&.repo_name, self.gitlink_branch_name) rescue nil
    else 
      Reposync::DeleteBranchService.call(self.sync_repository&.repo_name, self.external_branch_name) rescue nil
    end
  end

end
