class School::IsoCountriesController < ApplicationController
  include EdFi::IndianaHelper

  skip_before_action :authenticate_request!
  skip_before_action :enforce_password_reset!
  skip_before_action :enforce_terms_of_service_agreement!

  def index
    render_success :ok, json: countries
  end

  private
    def countries
      if current_school&.ed_fi_system_indiana?
        IsoCountry.where(alpha2: indiana_countries).ordered
      else
        IsoCountry.ordered
      end
    end

    def indiana_countries
      @indiana_countries ||= descriptor_service(
        current_school_year.id,
        descriptor: :countrydescriptors
      )
        .pluck('description')
        .map { |d| ISO3166::Country.find_country_by_name(d.strip)&.alpha2 }
    end
end
