class Projects::ForkService < ApplicationService
  attr_reader :target_owner, :project, :organization, :new_name, :new_identifier

  def initialize(target_owner, project, organization=nil, new_name=nil, new_identifier=nil)
    @target_owner = target_owner
    @project      = project
    @organization = organization
    @new_name = new_name
    @new_identifier = new_identifier
  end

  def call
    ActiveRecord::Base.transaction do
      clone_project =
        @project.deep_clone include: :repository,
          only: [:name, :description,:is_public, :identifier,
            :rep_identifier, :project_category_id, :project_language_id,
            :license_id, :ignore_id, {repository: [:identifier, :hidden]}]

      result = Gitea::Repository::ForkService.new(@project.owner, @target_owner, @project.identifier, @organization, @new_identifier).call
      Rails.logger.info("##### ForkService #{@project.identifier} result======#{result}")
      raise Error, 'fork失败' if result.blank? or result['id'].blank?
      clone_project.owner = @target_owner
      clone_project.forked_from_project_id = @project.id
      clone_project.gpid = result['id']
      clone_project.name = @new_name if @new_name.present?
      clone_project.identifier = @new_identifier if @new_identifier.present?
      clone_project.save!

      new_repository = clone_project.repository
      new_repository.owner = @target_owner
      new_repository.identifier = @project.identifier
      new_repository.save!

      ProjectUnit.init_types(clone_project.id)

      @project.update_attribute('forked_count', @project&.forked_count.to_i + 1)
      new_repository.update_column('url', result['clone_url']) if result
      
      ForkUser.create(project_id: @project.id, fork_project_id: clone_project.id, user_id: clone_project.user_id)
      SendTemplateMessageJob.perform_later('ProjectForked', @target_owner.id, @project.id) if Site.has_notice_menu?
      ProjectActions::ForkEvent.build(@project, @target_owner, clone_project)

      clone_project
    end
  rescue => e
    Rails.logger.info "fork project service error: #{e.message}"
    raise Error, e.message
  end

end
