# frozen_string_literal: true

require_relative "ui"
require_relative "../utils"
require_relative "../../../neeto_compliance/lib/neeto_compliance/neeto_repos.rb"

module Neetob
  class CLI::Base
    include Utils

    NEETO_APPS_LIST_LINK = "https://github.com/bigbinary/neeto-compliance/blob/main/lib/neeto_compliance/neeto_apps.rb"

    attr_reader :ui

    def initialize
      @ui = CLI::UI.new
    end

    private

      def find_all_matching_apps(apps, platform_name, sandbox_mode, quiet = false, all_neeto_repos = false)
        inform_about_current_working_mode(sandbox_mode, quiet)
        all_available_apps = sandbox_mode ?
          testing_apps(platform_name) :
          build_app_list_from_neeto_compliance(platform_name, all_neeto_repos)
        matching_apps = match_apps(apps || ["*"], all_available_apps)
        if matching_apps.length == 0
          error_msg = sandbox_mode ?
          "Only \"neeto-dummy\" app is available for sandbox mode. Remove the \"--sandbox\" flag to run the given command for all the neeto applications." :
          "No matching app is found in the neeto apps list maintained at \"#{NEETO_APPS_LIST_LINK}\""
          ui.error(error_msg)
          exit
        end

        matching_apps
      end

      def match_apps(required_apps, available_apps)
        apps = required_apps&.map { |app| Regexp.new "#{app.gsub("*", "[-a-zA-Z0-9]*")}" }
        available_apps.select do |available_app|
          apps&.any? { |app| app.match?(available_app) }
        end
      end

      def read_json_file(path)
        file = File.read(path)
        JSON.parse(file)
      end

      def build_app_list_from_neeto_compliance(platform_name, all_neeto_repos)
        apps = all_neeto_repos ? fetch_all_neeto_repos : NeetoCompliance::NeetoRepos.products.keys
        platform_name == :heroku ? add_env_suffix(apps) : prefix_org_name(apps)
      end

      def add_env_suffix(apps)
        [
          suffix_slug(apps, :staging),
          suffix_slug(apps, :production)
        ].flatten.sort
      end

      def suffix_slug(apps, suffix)
        apps.map { |app| "#{app}-#{suffix}" }
      end

      def prefix_org_name(apps)
        apps.map { |app| "bigbinary/#{app}" }
      end

      def testing_apps(platform_name)
        platform_name == :heroku ? ["neeto-dummy"] : ["bigbinary/neeto-dummy"]
      end

      def inform_about_current_working_mode(sandbox_mode, quiet)
        return if quiet

        callout_message = "Running in the sandbox mode. \nOnly \"neeto-dummy\" app will be available."
        unless sandbox_mode
          callout_message = "Running in non-sandbox mode. \nAll neeto applications are available. Some of the actions are irreversible."
        end
        ui.say(callout_message)
      end

      def fetch_all_neeto_repos
        NeetoCompliance::NeetoRepos::products.keys +
        NeetoCompliance::NeetoRepos::nanos +
        NeetoCompliance::NeetoRepos::widgets +
        NeetoCompliance::NeetoRepos::chrome_extensions +
        NeetoCompliance::NeetoRepos::helper_packages +
        NeetoCompliance::NeetoRepos::electron_apps +
        NeetoCompliance::NeetoRepos::executables +
        NeetoCompliance::NeetoRepos::mobile_apps +
        NeetoCompliance::NeetoRepos::other_repos
      end
  end
end