require "devops-client/output/base"
require "devops-client/output/concerns/has_provider"

module Output
  class Stack < Base
    include Concerns::HasProvider

    def table
      if outputting_list?
        title = I18n.t("output.title.stack.list")
        headers, rows = create_list
      else
        puts 'Details are not displayed in table view'
        title = I18n.t("output.title.stack.show", id: @data["name"])
        headers, rows = create_show
      end
      create_table(headers, rows, title, with_num?)
    end

    def csv
      if outputting_list?
        headers, rows = create_list
      else
        headers, rows = create_show
      end
      create_csv(headers, rows, with_num?)
    end

  private

    def create_list
      abort(I18n.t("output.not_found.stack.list")) if @data.empty?

      fields_to_output = %w(name project deploy_env stack_template stack_status)
      fields_to_output << 'provider' unless provider_given?

      headers_and_rows(@data, fields_to_output)
    end

    def create_show
      headers_and_rows([@data], %w(name project deploy_env stack_template stack_status))
    end

  end
end
