require "gli"
require "vindetta/version"

module Vindetta
  class CLI
    extend GLI::App

    program_desc "Vehicle Identification Number (VIN) CLI"
    version Vindetta::VERSION
    error_device = error_device

    desc "Transliterates a VIN character"
    arg_name "character"

    command %i[transliterate t] do |c|
      c.action do |_global, _options, args|
        puts Vindetta::Transliterator.run(args.first)
      end
    end

    desc "Validates a VIN"

    command %i[validate v] do |c|
      c.action do |_global, _options, args|
        vin = args.first

        exit_now!("vin is required", 1) if vin.nil? # TODO: Replace w/ I18n

        puts Vindetta::Validator.run(vin)
      end
    end

    desc "Decodes a VIN"

    command %i[decode d] do |c|
      c.action do |_global, _options, args|
        vin = args.first

        exit_now!("vin is required", 1) if vin.nil? # TODO: Replace w/ I18n

        puts Vindetta::Decoder.decode_vin(vin).to_json
      end
    end

    desc "Generates a random VIN"

    command %i[generate g] do |c|
      c.action do |_global, _options, _args|
        puts Vindetta::Generator.generate
      end
    end
  end
end

require "gli"
require "vindetta/version"

module Vindetta
  class CLI
    extend GLI::App

    program_desc "Vehicle Identification Number (VIN) CLI"
    version Vindetta::VERSION
    error_device = error_device

    desc "Transliterates a VIN character"
    arg_name "character"

    command %i[transliterate t] do |c|
      c.action do |_global, _options, args|
        puts Vindetta::Transliterator.run(args.first)
      end
    end

    desc "Validates a VIN"

    command %i[validate v] do |c|
      c.action do |_global, _options, args|
        vin = args.first

        exit_now!("vin is required", 1) if vin.nil? # TODO: Replace w/ I18n

        valid = Vindetta::Validator.run(vin)

        puts valid ? 1 : 0
      end
    end

    desc "Decodes a VIN"

    command %i[decode d] do |c|
      c.action do |_global, _options, args|
        vin = args.first

        exit_now!("vin is required", 1) if vin.nil? # TODO: Replace w/ I18n

        puts Vindetta::Decoder.decode_vin(vin).to_json
      end
    end

    desc "Generates a random VIN"

    command %i[generate g] do |c|
      c.action do |_global, _options, _args|
        puts Vindetta::Generator.generate
      end
    end
  end
end

Validate CLI command now prints 1 or 0
Validate CLI command now prints 1 or 0
