require "./config"
require "./version"
require "./providers/signal_spam"

require "http/client"
require "option_parser"
require "file_utils"
require "mechanize"

# Classe pour gérer les interactions avec Signal-Spam
module BulkBarrage
  class Cli
    def initialize
      @option_parser = OptionParser.new do |parser|
        parser.banner = [
          "#{PROGRAM_NAME} v#{VERSION}",
          "Usage: bulkbarrage [options] [command] [args]"
        ].join("\n")

        parser.on("configure", "Initialize the configuration") do
          init_config 
        end
        parser.on("report", "Process and report spam email (default: from STDIN)") do
          process_email_spam 
        end
        parser.on("--help", "-h", "show this help") do
          puts parser
        end
      end
    end

    def parse_arguments(args : Array(String))
      @option_parser.parse(args)
    rescue e : OptionParser::Exception
      puts e.message
      puts @option_parser
      exit(1)
    end


    private def init_config
      print "Enter username: "
      username = gets.try &.strip || "anonymous"
      print "Enter password: "
      password = gets.try &.strip || "anonymous"
      create_config_file(username, password)
      puts "Configuration file created."
    end

    private def process_email_spam
      email_spam = STDIN.gets_to_end

      signalspam = Providers::SignalSpam.new
      signalspam.process(email_spam)

      puts "Email spam processed."
    end

    private def create_config_file(username : String, password : String)
      signalspam = Providers::SignalSpam.new
      signalspam.create_config_file(username, password)
    end

  end
end

