# frozen_string_literal: true

require 'git/commands/base'

module Git
  module Commands
    module Branch
      # Implements the `git branch --delete` command for deleting branches
      #
      # @example Delete a single branch
      #   delete = Git::Commands::Branch::Delete.new(execution_context)
      #   result = delete.call('feature-branch')
      #
      # @example Delete multiple branches
      #   delete = Git::Commands::Branch::Delete.new(execution_context)
      #   result = delete.call('branch1', 'branch2')
      #
      # @example Force delete (works even if not merged)
      #   delete = Git::Commands::Branch::Delete.new(execution_context)
      #   result = delete.call('feature-branch', force: true)
      #
      # @example Delete remote-tracking branch
      #   delete = Git::Commands::Branch::Delete.new(execution_context)
      #   result = delete.call('origin/feature', remotes: true)
      #
      # @note `arguments` block audited against https://git-scm.com/docs/git-branch/2.53.0
      #
      # @see Git::Commands::Branch
      #
      # @see https://git-scm.com/docs/git-branch git-branch
      #
      # @api private
      #
      class Delete < Git::Commands::Base
        arguments do
          literal 'branch'
          literal '--delete'
          flag_option %i[force f]
          flag_option %i[remotes r]

          end_of_options

          operand :branch_name, repeatable: true, required: true
        end

        # git branch --delete exits 1 when one or more branches cannot be deleted
        allow_exit_status 0..1

        # Executes the git branch --delete command to delete branches
        #
        # @overload call(*branch_name, **options)
        #
        #   @param branch_name [Array<String>] one or more branch names to delete
        #
        #   @param options [Hash] command options
        #
        #   @option options [Boolean, nil] :force (nil) allow deleting the branch irrespective of
        #     its merged status, or whether it even points to a valid commit
        #
        #     Equivalent to the `-D` shortcut (`--delete --force`)
        #
        #     Alias: :f
        #
        #   @option options [Boolean, nil] :remotes (nil) delete remote-tracking branches
        #
        #     Use this together with `--delete` to remove remote-tracking branches that no
        #     longer exist in the remote repository, or if `git fetch` was configured not to
        #     fetch them again
        #
        #     Alias: :r
        #
        #   @return [Git::CommandLine::Result] the result of calling `git branch --delete`
        #
        #   @raise [ArgumentError] if no branch names are provided
        #
        #   @raise [ArgumentError] if unsupported options are provided
        #
        #   @raise [Git::FailedError] if git exits outside the allowed range (exit code > 1)
        #
        #   @api public
        #
        def call(*, **)
          super
        end
      end
    end
  end
end
