# frozen_string_literal: true

# Copyright (c) [2024-2025] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require "agama/storage/proposal_strategies/base"
require "agama/storage/proposal_settings"
require "agama/storage/proposal_settings_reader"

module Agama
  module Storage
    module ProposalStrategies
      # Strategy used by the Agama proposal for backwards compatibility with AutoYaST.
      class Autoyast < Base
        include Yast::I18n

        # @param product_config [Config] Product config
        # @param storage_system [Storage::System]
        # @param partitioning [Array<Hash>]
        # @param logger [Logger]
        def initialize(product_config, storage_system, partitioning, logger)
          textdomain "agama"

          super(product_config, storage_system, logger)
          @partitioning = partitioning
        end

        # Settings used for calculating the proposal.
        #
        # @return [Array<Hash>]
        attr_reader :partitioning
        alias_method :settings, :partitioning

        # @see Base#calculate
        def calculate
          @ay_issues = ::Installation::AutoinstIssues::List.new
          proposal = Y2Storage::AutoinstProposal.new(
            partitioning:      partitioning,
            proposal_settings: proposal_settings,
            devicegraph:       storage_system.devicegraph,
            disk_analyzer:     storage_system.analyzer,
            issues_list:       ay_issues
          )
          proposal.propose
        ensure
          storage_manager.proposal = proposal
        end

        # @see Base#issues
        def issues
          ay_issues.map { |i| agama_issue(i) }
        end

      private

        # Issues generated by the AutoYaST proposal
        # @return [::Installation::AutoinstIssues::List]
        attr_reader :ay_issues

        # Default proposal settings, potentially used to calculate omitted information
        #
        # @return [Y2Storage::ProposalSettings]
        def proposal_settings
          agama_default = ProposalSettingsReader.new(product_config).read
          agama_default.to_y2storage(config: product_config)
        end

        # Agama issue equivalent to the given AutoYaST issue
        def agama_issue(ay_issue)
          Issue.new(
            ay_issue.message,
            source:   Issue::Source::CONFIG,
            severity: ay_issue.warn? ? Issue::Severity::WARN : Issue::Severity::ERROR
          )
        end
      end
    end
  end
end
