class DynamicApiRouter

  def self.load
    Tf::Application.routes.draw do

      api_routes_file = Rails.root.join('tmp', 'api_routes.txt')
      File.delete(api_routes_file) if File.exists?(api_routes_file)
      File.open(api_routes_file, 'w') do |f|

        DynamicApiRouter.controllers.each do |controller|
          f.puts "http://api.localhost.com:3000/#{controller}"
          f.puts "Devuelve una lista de #{I18n.t("activerecord.models.#{controller.to_s.singularize}")}\n\n"
          resources controller, only: [:index], to: "api/dynamic_api#index", defaults: { subdomain: :api, format: :json, resources: controller }
        end
      end
    end
  end

  def self.reload
    Tf::Application.routes_reloader.reload!
  end

  def self.get_api_associations
    DynamicApiRouter.controllers.each do |controller|
      begin
        model = controller.to_s.singularize.camelize.constantize
        reflections = model.reflect_on_all_associations(:belongs_to).map(&:name).map(&:to_sym)
        attributes = model.new.attributes.keys.select{ |attr| attr.end_with?('_id') }.map{ |attr| attr.gsub('_id', '') }.map(&:to_sym)
        attributes.each do |attr|
          if reflections.include?(attr)
            # puts "OK: #{attr}"
          else
            puts "#{model}: Reflection missing: #{attr}"
          end
        end
      rescue
        nil
      end
    end
  end

  def self.controllers
    [
      :academic_grades,
      :accountants,
      :budget_units,
      :carreers,
      :categories,
      :cities,
      :civil_organization_types,
      :civil_organizations,
      :companies,
      :company_penalties,
      :cooperative_types,
      :cooperatives,
      :cultural_fee_place_contacts,
      :cultural_fee_places,
      :cultural_fee_price_types,
      :cultural_fee_types,
      :cultural_fees,
      :delation_infos,
      :delation_institutions,
      :delegation_infos,
      :delegations,
      :disabled_associations,
      :doctor_especialities,
      :doctors,
      :documents,
      :electricity_charge_types,
      :electricity_companies,
      :electricity_demand_levels,
      :electricity_prices,
      :electricity_rate_types,
      :executing_works,
      :finance_types,
      :finances,
      :fodes_cities_transfer_infos,
      :fodes_cities_transfers,
      :food_establishment_areas,
      :food_establishment_health_communities,
      :food_establishment_types,
      :food_establishments,
      :fovial_companies,
      :health_establishment_types,
      :health_establishments,
      :hydro_departments,
      :hydro_establishments,
      :hydro_establishments_hydro_routes,
      :hydro_municipalities,
      :hydro_prices,
      :hydro_references,
      :hydro_routes,
      :hydro_zones,
      :information_officers,
      :information_standard_categories,
      :information_standard_infos,
      :information_standard_frames,
      :information_standards,
      :institution_consultants,
      :institution_events,
      :institution_information_standards_settings,
      :institution_officials,
      :institution_organizational_structures,
      :institution_remunerations,
      :institution_service_categories,
      :institution_service_steps,
      :institution_services,
      :institution_travels,
      :institution_types,
      :institutions,
      :lawyers,
      :line_of_works,
      :medicine_categories,
      :medicines,
      :nationalities,
      :occupations,
      :procurements,
      :product_brands,
      :product_categories,
      :product_presentations,
      :product_probes,
      :products,
      :radial_concessions,
      :radial_frequencies,
      :refuges,
      :resources_to_private_recipients,
      :risk_prevention_consultants,
      :roads,
      :school_infos,
      :schools,
      :shopping_establishments,
      :solvent_companies,
      :sports_federation_transfers,
      :sports_grants,
      :states,
      :syndicate_categories,
      :syndicates,
      :telephone_companies_concessions,
      :universities,
      :woman_cities,
      :woman_city_services
    ]
  end
end
