require 'rails/generators/active_record'
require 'dunlop/generators'
class Dunlop::Install::WorkflowGenerator < Rails::Generators::Base
  include Rails::Generators::Migration
  include Dunlop::Generators::GeneratorHelpers
  source_root File.expand_path('../templates', __FILE__)

  def create_models
    copy_file "models/workflow_step_collection.rb", "app/models/concerns/workflow_step_collection.rb"
    copy_file "models/workflow_step_model.rb", "app/models/concerns/workflow_step_model.rb"
  end

  def setup_workflow_basics
    template "models/workflow_instance_batch.rb", "app/models/workflow_instance_batch.rb"
    setup_workflow_decorators
    setup_workflow_instances
    setup_workflow_steps
    #TODO: call workflow related generators here
  end

  def generate_views
    directory "views/workflow_steps", "app/views/workflow_steps"
  end

  def create_controllers
    template "controllers/workflow_instances_controller.rb", "app/controllers/workflow_instances_controller.rb"
    template "controllers/workflow_instance_batches_controller.rb", "app/controllers/workflow_instance_batches_controller.rb"
    directory "controllers/concerns", "app/controllers/concerns"
  end

  def add_csv_builders
    directory "csv_builders", "app/csv_builders"
  end

  def create_locales
    directory "locales", "config/locales"
  end

  def setup_rspec_files
    directory "factories", "spec/factories"
  end

  def setup_initializers
    directory "initializers", "config/initializers"
  end

  private

  def setup_workflow_decorators
    template "decorators/workflow_instance_decorator.rb", "app/decorators/workflow_instance_decorator.rb"
  end

  def setup_workflow_instances
    migration_template "migrations/create_workflow_instances.rb", "db/migrate/create_workflow_instances.rb"
    migration_template "migrations/create_workflow_instance_batches.rb", "db/migrate/create_workflow_instance_batches.rb"
    migration_template "migrations/create_dunlop_last_attribute_changes.rb", "db/migrate/create_dunlop_last_attribute_changes.rb"
    copy_file "models/workflow_instance.rb", "app/models/workflow_instance.rb"
    copy_file "models/workflow_instance_data_setup.rb", "app/models/workflow_instance/data_setup.rb"
    directory "views/workflow_instances", "app/views/workflow_instances"
    directory "views/workflow_instance_batches", "app/views/workflow_instance_batches"

    route <<-ROUTE.strip_heredoc
      namespace :workflow_instance do
          WorkflowInstance.scenario_names.each do |scenario_name|
            collection_resources scenario_name.to_s.pluralize do
              collection do
                match :selection, via: [:get, :post]
              end
              post :reset, on: :member
            end
          end
        end

        namespace :workflow_instance_batch do
          WorkflowInstance.scenario_names.each do |scenario_name|
            resources scenario_name.to_s.pluralize
          end
        end
    ROUTE
  end

  def setup_workflow_steps
    route <<-ROUTE.strip_heredoc
      WorkflowInstance.possible_workflow_step_names.each do |workflow_step|
          namespace workflow_step do
            WorkflowInstance.scenario_names.each do |scenario_name|
              collection_resources scenario_name.to_s.pluralize, only: [:edit, :update]
            end
          end
        end
    ROUTE
  end

end
