#!/usr/bin/env ruby
require "souls"

begin
  begin
    require "./config/initializers/souls" unless ARGV[0] == "new" || ARGV[0] == "init"
  rescue
    Souls::Init.config_init
  end
  case ARGV[0]
  when "new"
    if ARGV[1].nil?
      puts "you need to specify your app name \n `souls new app_name`"
      exit
    end
    puts "Which framework:   \n 1. SOULS gRPC Service \n 2. SOULS GraphQL API gRPC compatible \n 3. SOULS GraphQL API \n 4. SOULS Media Web \n 5. SOULS Admin Web \n Enter Number: "
    strain = STDIN.gets.chomp.to_i
    (1..5).include?(strain) ? puts("Generating Souls.. \n") : raise(StandardError, "Choose Number 1..5")
    Souls::Init.create_souls strain: strain, app_name: ARGV[1]
  when "s", "server"
    strain = Souls.configuration.strain
    case strain
    when "api"
      system "bundle exec puma -p 3000"
    when "service"
      system "bundle exec rake run_server"
    else
      system "yarn dev"
    end
  when "c", "console"
    strain = Souls.configuration.strain
    case strain
    when "api"
      system "bundle exec irb"
    when "service"
      system "bundle exec irb"
    else
      system "yarn dev"
    end
  when "i", "infra"
    Souls.send ARGV[1]
  when "p", "proto"
    Souls::Init.proto proto_package_name: "souls", service: ARGV[1]
  when "init"
    Souls::Init.config_init
  when "-v", "--version"
    puts Souls::VERSION
  when "g", "generate"
    case ARGV[1]
    when "test"
      Souls::Init.get_tables
    when "model"
      Souls::Init.model class_name: ARGV[2]
    when "mutation"
      Souls::Init.mutation class_name: ARGV[2]
    when "query"
      Souls::Init.query class_name: ARGV[2]
    when "type"
      Souls::Init.type class_name: ARGV[2]
    when "migrate"
      Souls::Init.migrate class_name: ARGV[2]
    when "migrate_all"
      Souls::Init.migrate_all
    when "migration"
      `rake db:create_migration NAME=#{ARGV[3]}`
    when "rspec_factory"
      Souls::Init.rspec_factory class_name: ARGV[2]
    when "rspec_model"
      Souls::Init.rspec_model class_name: ARGV[2]
    when "rspec_mutation"
      Souls::Init.rspec_mutation class_name: ARGV[2]
    when "rspec_query"
      Souls::Init.rspec_query class_name: ARGV[2]
    when "rspec_type"
      Souls::Init.rspec_type class_name: ARGV[2]
    else
    end
  when "t", "test"
    puts `rubocop`
    puts `bundle exec rspec`
  when "deploy"
    project_id = Souls.configuration.project_id
    `gcloud builds submit --config=cloudbuild.yml --project #{project_id}`
  else
    puts "Welcome to SOULs!"
  end
rescue StandardError => error
  puts error
  puts "Thank you!!"
  puts "SOULs"
end