class Admin::Settings::StateReportingController < Admin::Controller
  def show
    render_success :ok, json: school_props
  end

  def update
    if current_school.update(school_params)
      render_success :ok, json: school_props
    else
      render_error :unprocessable_entity, errors: current_school
    end
  end

  private
    def school_params
      params.permit(
        :ed_fi_system,
        :student_address_opt,
        school_state_attributes: [:id, :state_id, :_destroy],
        school_config_attributes: [:id, :ed_fi_type]
      )
    end

    def school_state
      @school_state ||= SchoolState.find_or_initialize_by(school: current_school)
    end

    def school_config
      @school_config ||= SchoolConfig.find_or_initialize_by(school: current_school)
    end

    def school_props
      {
        ed_fi_system: current_school.ed_fi_system,
        student_address_opt: current_school.student_address_opt,
        school_state_attributes: {
          id: school_state.id,
          state_id: school_state.state_id? ? school_state.state_id : nil
        },
        school_config_attributes: {
          id: school_config.id,
          ed_fi_type: school_config.ed_fi_type
        }
      }
    end
end
