{# events/partials/rsvp_page_questions_answer.html #}
{# Render inputs only — the wrapping form handles submission. #}

{% comment %}
  Helper pattern: scan the responses QuerySets to see if an existing Response exists
  for this question (and guest when relevant). The template uses loops to determine
  whether to render `checked` or prefill `value`.
{% endcomment %}

{% if question.kind == "single" %}
  {% for choice in question.choices.all %}
    <div>
      <label>
        <input type="radio" name="choice" value="{{ choice.id }}"
          {% if question.scope == "rsvp" %}
            {% for resp in responses_rsvp %}
              {% if resp.question.id == question.id %}
                {% for resp_choice in resp.choices.all %}
                  {% if resp_choice.id == choice.id %}checked{% endif %}
                {% endfor %}
              {% endif %}
            {% endfor %}
          {% else %}
            {% for resp in responses_guest %}
              {% if resp.question.id == question.id and resp.guest.id == guest.id %}
                {% for resp_choice in resp.choices.all %}
                  {% if resp_choice.id == choice.id %}checked{% endif %}
                {% endfor %}
              {% endif %}
            {% endfor %}
          {% endif %}
        >
        {{ choice.label }}
      </label>
    </div>
  {% endfor %}

{% elif question.kind == "multiple" %}
  {% for choice in question.choices.all %}
    <div>
      <label>
        <input type="checkbox" name="choices" value="{{ choice.id }}"
          {% if question.scope == "rsvp" %}
            {% for resp in responses_rsvp %}
              {% if resp.question.id == question.id %}
                {% for resp_choice in resp.choices.all %}
                  {% if resp_choice.id == choice.id %}checked{% endif %}
                {% endfor %}
              {% endif %}
            {% endfor %}
          {% else %}
            {% for resp in responses_guest %}
              {% if resp.question.id == question.id and resp.guest.id == guest.id %}
                {% for resp_choice in resp.choices.all %}
                  {% if resp_choice.id == choice.id %}checked{% endif %}
                {% endfor %}
              {% endif %}
            {% endfor %}
          {% endif %}
        >
        {{ choice.label }}
      </label>
    </div>
  {% endfor %}

{% elif question.kind == "boolean" %}
  <div>
    <label>
      <input type="radio" name="value_bool" value="true"
        {% if question.scope == "rsvp" %}
          {% for resp in responses_rsvp %}
            {% if resp.question.id == question.id and resp.value_text == "true" %}checked{% endif %}
          {% endfor %}
        {% else %}
          {% for resp in responses_guest %}
            {% if resp.question.id == question.id and resp.guest.id == guest.id and resp.value_text == "true" %}checked{% endif %}
          {% endfor %}
        {% endif %}
      >
      Yes
    </label>
  </div>
  <div>
    <label>
      <input type="radio" name="value_bool" value="false"
        {% if question.scope == "rsvp" %}
          {% for resp in responses_rsvp %}
            {% if resp.question.id == question.id and resp.value_text == "false" %}checked{% endif %}
          {% endfor %}
        {% else %}
          {% for resp in responses_guest %}
            {% if resp.question.id == question.id and resp.guest.id == guest.id and resp.value_text == "false" %}checked{% endif %}
          {% endfor %}
        {% endif %}
      >
      No
    </label>
  </div>

{% elif question.kind == "integer" %}
  <input
    type="number"
    name="value"
    {% if question.scope == "rsvp" %}
      value="{% for resp in responses_rsvp %}{% if resp.question.id == question.id %}{{ resp.value_text|default:'' }}{% endif %}{% endfor %}"
    {% else %}
      value="{% for resp in responses_guest %}{% if resp.question.id == question.id and resp.guest.id == guest.id %}{{ resp.value_text|default:'' }}{% endif %}{% endfor %}"
    {% endif %}
  >

{% else %}
  <input
    type="text"
    name="value"
    {% if question.scope == "rsvp" %}
      value="{% for resp in responses_rsvp %}{% if resp.question.id == question.id %}{{ resp.value_text|default:'' }}{% endif %}{% endfor %}"
    {% else %}
      value="{% for resp in responses_guest %}{% if resp.question.id == question.id and resp.guest.id == guest.id %}{{ resp.value_text|default:'' }}{% endif %}{% endfor %}"
    {% endif %}
  >
{% endif %}