#!/usr/bin/env ruby
require 'thor'
require 'yaml'
require 'fileutils'

class LbhrrCLI < Thor
  no_commands do

    def create_example_global_config
      example_global_config = {
        'harbr' => 'harbr.zero2one.ee'
      }
      YAML.dump(example_global_config)
    end

    def create_example_local_config
      example_local_config = {
        'name' => 'name',
        'host' => 'example.com'
      }

      YAML.dump(example_local_config)
    end
    
    def load_configuration
      global_config_dir = File.expand_path('~/.config/harbr')
      global_config_path = File.join(global_config_dir, 'harbr.manifest.yml')
      local_config_path = File.join(Dir.pwd, 'config', 'manifest.yml')
    
      # Ensure global configuration exists
      unless File.exist?(global_config_path)
        FileUtils.mkdir_p(global_config_dir) unless Dir.exist?(global_config_dir)
        File.write(global_config_path, create_example_global_config)
      end
    
      # Ensure local configuration exists
      unless File.exist?(local_config_path)
        FileUtils.mkdir_p(File.dirname(local_config_path)) unless Dir.exist?(File.dirname(local_config_path))
        File.write(local_config_path, create_example_local_config)
      end
    
      # Load and merge configurations
      global_config = YAML.load_file(global_config_path) || {}
      local_config = YAML.load_file(local_config_path) || {}
      global_config.merge(local_config)
    end
    
    
  end  

  desc "deploy", "Deploy an application using the configuration from harbr.manifest.yml"
  def deploy
    config = load_configuration
    source = config['source']
    harbr = config['harbr']
    # Implement deployment logic using source and harbr
    puts "Deploying application from #{source} to #{harbr}..."
  end
  desc "status HARBR", "Check the status of the application on the HARBR server"
  def status(harbr)
    # Implement status checking logic
    puts "Checking application status on #{harbr}..."
  end

  desc "rollback HARBR VERSION", "Rollback the application on the HARBR server to a specified VERSION"
  def rollback(harbr, version)
    # Implement rollback logic
    puts "Rolling back application on #{harbr} to version #{version}..."
  end

  # ... additional commands ...
end

LbhrrCLI.start(ARGV)