# typed: false
# frozen_string_literal: true

require_relative "base"

module Hephaestus
  class ConfigGenerator < Generators::Base
    def application
      config = <<~CONFIG


        if ENV.fetch("DEBUG", false) && defined?(Rails::Server) && Rails.env.development?
          require "debug/open_nonstop"
        end

      CONFIG

      inject_into_file(
        "config/application.rb",
        config,
        after: "require \"rails/test_unit/railtie\"",
      )
      inject_into_file(
        "config/application.rb",
        "\n  CURRENT_VERSION = \"2023-03-06\"\n\n",
        before: "class Application < Rails::Application",
      )

      raltie_overload = <<~CONFIG

        # Load dotenv only in development environment
        Dotenv::Railtie.overload if Rails.env.development?
      CONFIG
      inject_into_file(
        "config/application.rb",
        raltie_overload,
        after: "Bundler.require(*Rails.groups)",
      )

      config = <<~CONFIG

        # Only loads a smaller set of middleware suitable for API-only Rails.
        # Middleware like session, flash, cookies can be added back manually.
        # Skip views, helpers and assets when generating a new resource.
        config.api_only = true

        Rails.root.glob("app/lib/#{app_name.underscore}/middleware/*.{rb}").each { |file| require_relative file }

        config.middleware.insert(0, PlugApp::Middleware::TracingAttributes)
        config.middleware.insert(0, PlugApp::Middleware::MalformedRequest)

        config.middleware.use(PlugApp::Middleware::OpenapiValidation)

        GIT_SHA = %x(git rev-parse HEAD).chomp
      CONFIG

      inject_into_file(
        "config/application.rb",
        config,
        before: "\n  end",
      )

      replace_in_file(
        "config/application.rb",
        "require \"rails\"\n",
        "",
      )

      comment_lines("config/application.rb", "require \"active_model/railtie\"")
      uncomment_lines("config/application.rb", "require \"active_job/railtie\"")
    end

    def boot
      boot = "require \"bootsnap/setup\" # Speed up boot time by caching expensive operations."
      inject_into_file(
        "config/boot.rb",
        boot,
      )
    end

    def initializers
      remove_dir("config/initializers")
      directory("config/initializers", "config/initializers")
    end

    def configure_routes
      routes = <<~ROUTES
        root "root#index"

        # events into the plug, usually from yetto
        get "/api/2023-03-06/settings", to: "settings#new"
        post "/api/2023-03-06/:event/:record_type", to: "yetto#event"

        # inbound message
        post "/app/2023-03-06/webhook/:organization_id/:inbox_id/:plug_installation_id", to: "app#webhook"
        # post "/app/2023-03-06/webhook/inbound", to: "app#process_inbound" # for generic inbound pings

        # Staff pages
        get "staff", to: "staff#index"
        require "sidekiq/web"
        constraints ->(request) { StaffController.staff_request?(request) } do
          mount Sidekiq::Web => "staff/sidekiq"
        end

        #############################################
        # error pages -- these MUST be at the end! ##
        #############################################

        get "/500", to: "application#render500" if Rails.env.production? || Rails.env.staging?

        match "/", to: "application#not_found", via: :all
        match "/*unmatched_route", to: "application#not_found", via: :all
      ROUTES

      replace_in_file(
        "config/routes.rb",
        "# root \"articles#index\"",
        routes,
      )
    end

    def sidekiq
      copy_file("config/sidekiq.yml", "config/sidekiq.yml")
    end

    def credentials
      remove_file("config/credentials.yml.enc")
    end
  end
end