module Dalmatian
  class Aurora
    def initialize(cluster:, reference:)
      @cluster = cluster
      @reference = reference
    end

    attr_reader :cluster, :reference

    def identifier
      reference["identifier"]
    end

    def in_use_by
      reference["in_use_by"] || []
    end

    def clusters_in_use
      clusters_in_use = {}
      cluster_environments.each do |env_name, _env_config|
        clusters_in_use[env_name] = []
        services.each do |service|
          in_use_by.each do |service_name|
            next if service.name != service_name
            clusters_in_use[env_name] << if service.launch_on_cluster.empty?
              "default_dalmatian_ecs_cluster"
            else
              service.launch_on_cluster
            end
          end
        end
        if in_use_by.empty?
          clusters_in_use[env_name] << "default_dalmatian_ecs_cluster" ## fails SimpleCov
        end
        clusters_in_use[env_name] = clusters_in_use[env_name].uniq
      end
      clusters_in_use
    end

    def minimum_size
      minimum_size = {}
      reference["minimum_size"] = {} if reference["minimum_size"].nil?
      cluster_environments.each do |env_name, _env_config|
        minimum_size[env_name] = reference["minimum_size"][env_name] || ""
      end
      minimum_size
    end

    def maximum_size
      maximum_size = {}
      reference["maximum_size"] = {} if reference["maximum_size"].nil?
      cluster_environments.each do |env_name, _env_config|
        maximum_size[env_name] = reference["maximum_size"][env_name] || ""
      end
      maximum_size
    end

    def engine
      reference["engine"]
    end

    def engine_version
      reference["engine_version"]
    end

    def db_name
      reference["db_name"]
    end

    def port
      reference["port"] || ""
    end

    def maintenance_window
      reference["maintenance_window"] || ""
    end

    def backup_window
      reference["backup_window"] || ""
    end

    def backup_retention_period
      reference["backup_retention_period"] || ""
    end

    def parameter_store_path_db_url_name
      reference["parameter_store_path_db_url_name"] || ""
    end

    def sql_backup_scheduled_task_environment_variables
      reference["sql_backup_scheduled_task_environment_variables"] || []
    end

    def sync_sql_backup_to_azure
      reference["sync_sql_backup_to_azure"] || false
    end

    def replication_bucket_destination_arn
      reference["replication_bucket_destination_arn"] || ""
    end

    def replication_kms_key_id
      reference["replication_kms_key_id"] || ""
    end

    def force_ssl
      reference["force_ssl"] || "false"
    end

    def cluster_id
      cluster.id
    end

    def cluster_environments
      cluster.environments
    end

    def account_id
      cluster.account_id
    end

    def services
      cluster.services
    end

    def to_params
      {
        "identifier" => identifier,
        "in_use_by" => in_use_by,
        "clusters_in_use" => clusters_in_use,
        "minimum_size" => minimum_size,
        "maximum_size" => maximum_size,
        "engine" => engine,
        "engine_version" => engine_version,
        "db_name" => db_name,
        "port" => port,
        "force_ssl" => force_ssl,
        "maintenance_window" => maintenance_window,
        "backup_window" => backup_window,
        "backup_retention_period" => backup_retention_period,
        "parameter_store_path_db_url_name" => parameter_store_path_db_url_name,
        "sql_backup_scheduled_task_environment_variables" => sql_backup_scheduled_task_environment_variables,
        "sync_sql_backup_to_azure" => sync_sql_backup_to_azure,
        "replication_bucket_destination_arn" => replication_bucket_destination_arn,
        "replication_kms_key_id" => replication_kms_key_id
      }
    end
  end
end
