# frozen_string_literal: true

require 'time'

module GDK
  module Command
    # Switches to a branch in the monolith and sets up the environment for it.
    class Switch < BaseCommand
      def run(args = [])
        branch = args.pop

        unless branch
          out.warn('Usage: gdk switch [BRANCH_NAME]')
          return false
        end

        # Don't save config after this
        config.bury!('gitlab.default_branch', branch)

        success = GDK::Hooks.with_hooks(config.gdk.update_hooks, 'gdk switch') do
          run_rake('update_branch', branch)
        end

        unless success
          GDK::Output.error('Failed to switch branches.', report_error: false)
          display_help_message
          return false
        end

        color_branch = out.wrap_in_color(branch, Output::COLOR_CODE_YELLOW)
        GDK::Output.success("Switched to #{color_branch}.")

        true
      rescue Support::Rake::TaskWithLogger::LoggerError => e
        e.print!
        false
      end
    end
  end
end
