<%# Bologna Theme Search Form For Sale - URL-based with Turbo Frames %>
<%= form_with url: buy_path, method: :get, local: true,
              data: {
                turbo_frame: "search-results",
                action: "submit->search#handleSubmit",
                search_target: "form"
              },
              class: "search-filter-form space-y-5" do |f| %>

  <%# Property Type %>
  <div>
    <label for="filter-type" class="block text-sm font-medium text-warm-gray-700 mb-2">
      <%= I18n.t("propertyTypes.label", default: "Property Type") %>
    </label>
    <select id="filter-type"
            name="type"
            data-filter="type"
            data-action="change->search#filterChanged"
            class="w-full border-warm-gray-300 rounded-soft shadow-sm focus:border-terra-500 focus:ring-terra-500 text-warm-gray-900">
      <option value=""><%= I18n.t("propertyTypes.all", default: "All Types") %></option>
      <% Pwb::ListedProperty.distinct.pluck(:prop_type_key).compact.each do |type| %>
        <% type_slug = type.to_s.split('.').last %>
        <% is_selected = @search_criteria_for_view[:property_type].present? &&
                         (type == @search_criteria_for_view[:property_type] ||
                          type_slug == @search_criteria_for_view[:property_type]) %>
        <option value="<%= type_slug %>" <%= 'selected' if is_selected %>>
          <%= I18n.t("propertyTypes.#{type_slug}", default: type_slug.titleize) %>
        </option>
      <% end %>
    </select>
  </div>

  <%# Price Range %>
  <div>
    <label class="block text-sm font-medium text-warm-gray-700 mb-2">
      <%= I18n.t("search.price_range", default: "Price Range") %>
    </label>
    <div class="grid grid-cols-2 gap-3">
      <div>
        <select id="filter-price-min"
                name="price_min"
                data-filter="price_min"
                data-action="change->search#filterChanged"
                class="w-full border-warm-gray-300 rounded-soft shadow-sm focus:border-terra-500 focus:ring-terra-500 text-sm">
          <option value=""><%= I18n.t("search.min", default: "Min") %></option>
          <% @prices_from_collection&.each do |price| %>
            <option value="<%= price %>" <%= 'selected' if @search_criteria_for_view[:price_min].to_i == price.to_i && price.present? %>>
              <%= number_to_currency(price, precision: 0) %>
            </option>
          <% end %>
        </select>
      </div>
      <div>
        <select id="filter-price-max"
                name="price_max"
                data-filter="price_max"
                data-action="change->search#filterChanged"
                class="w-full border-warm-gray-300 rounded-soft shadow-sm focus:border-terra-500 focus:ring-terra-500 text-sm">
          <option value=""><%= I18n.t("search.max", default: "Max") %></option>
          <% @prices_till_collection&.each do |price| %>
            <option value="<%= price %>" <%= 'selected' if @search_criteria_for_view[:price_max].to_i == price.to_i && price.present? %>>
              <%= number_to_currency(price, precision: 0) %>
            </option>
          <% end %>
        </select>
      </div>
    </div>
  </div>

  <%# Bedrooms %>
  <div>
    <label class="block text-sm font-medium text-warm-gray-700 mb-3">
      <%= I18n.t("amenities.bedrooms", default: "Bedrooms") %>
    </label>
    <div class="flex flex-wrap gap-2">
      <% [nil, 1, 2, 3, 4, 5].each do |num| %>
        <label class="bedroom-chip">
          <input type="radio"
                 name="bedrooms"
                 value="<%= num %>"
                 data-filter="bedrooms"
                 data-action="change->search#filterChanged"
                 class="sr-only peer"
                 <%= 'checked' if @search_criteria_for_view[:bedrooms].to_i == num.to_i && (num.present? || @search_criteria_for_view[:bedrooms].blank?) %>>
          <span class="inline-flex items-center px-3 py-1.5 rounded-pill text-sm font-medium cursor-pointer border border-warm-gray-300 bg-white text-warm-gray-700 peer-checked:bg-terra-500 peer-checked:text-white peer-checked:border-terra-500 hover:bg-warm-gray-50 peer-checked:hover:bg-terra-600 transition-all duration-200">
            <%= num.nil? ? I18n.t("search.any", default: "Any") : "#{num}+" %>
          </span>
        </label>
      <% end %>
    </div>
  </div>

  <%# Bathrooms %>
  <div>
    <label class="block text-sm font-medium text-warm-gray-700 mb-3">
      <%= I18n.t("amenities.bathrooms", default: "Bathrooms") %>
    </label>
    <div class="flex flex-wrap gap-2">
      <% [nil, 1, 2, 3, 4].each do |num| %>
        <label class="bathroom-chip">
          <input type="radio"
                 name="bathrooms"
                 value="<%= num %>"
                 data-filter="bathrooms"
                 data-action="change->search#filterChanged"
                 class="sr-only peer"
                 <%= 'checked' if @search_criteria_for_view[:bathrooms].to_i == num.to_i && (num.present? || @search_criteria_for_view[:bathrooms].blank?) %>>
          <span class="inline-flex items-center px-3 py-1.5 rounded-pill text-sm font-medium cursor-pointer border border-warm-gray-300 bg-white text-warm-gray-700 peer-checked:bg-terra-500 peer-checked:text-white peer-checked:border-terra-500 hover:bg-warm-gray-50 peer-checked:hover:bg-terra-600 transition-all duration-200">
            <%= num.nil? ? I18n.t("search.any", default: "Any") : "#{num}+" %>
          </span>
        </label>
      <% end %>
    </div>
  </div>

  <%# Location (Zone) %>
  <% if @zones.present? && @zones.length > 1 %>
    <div>
      <label for="filter-zone" class="block text-sm font-medium text-warm-gray-700 mb-2">
        <%= I18n.t("search.zone", default: "Zone") %>
      </label>
      <select id="filter-zone"
              name="zone"
              data-filter="zone"
              data-action="change->search#filterChanged"
              class="w-full border-warm-gray-300 rounded-soft shadow-sm focus:border-terra-500 focus:ring-terra-500">
        <option value=""><%= I18n.t("search.all_zones", default: "All Zones") %></option>
        <% @zones.each do |zone| %>
          <option value="<%= zone.key %>" <%= 'selected' if @search_criteria_for_view[:zone] == zone.key %>>
            <%= zone.to_s %>
          </option>
        <% end %>
      </select>
    </div>
  <% end %>

  <%# Location (Locality) %>
  <% if @localities.present? && @localities.length > 1 %>
    <div>
      <label for="filter-locality" class="block text-sm font-medium text-warm-gray-700 mb-2">
        <%= I18n.t("search.locality", default: "Locality") %>
      </label>
      <select id="filter-locality"
              name="locality"
              data-filter="locality"
              data-action="change->search#filterChanged"
              class="w-full border-warm-gray-300 rounded-soft shadow-sm focus:border-terra-500 focus:ring-terra-500">
        <option value=""><%= I18n.t("search.all_localities", default: "All Localities") %></option>
        <% @localities.each do |locality| %>
          <option value="<%= locality.key %>" <%= 'selected' if @search_criteria_for_view[:locality] == locality.key %>>
            <%= locality.to_s %>
          </option>
        <% end %>
      </select>
    </div>
  <% end %>

  <%# Features %>
  <% if @feature_list.present? %>
    <div class="border-t border-warm-gray-200 pt-5">
      <label class="block text-sm font-medium text-warm-gray-700 mb-3">
        <%= I18n.t("search.features", default: "Features") %>
      </label>
      <div class="space-y-2 max-h-48 overflow-y-auto">
        <% @feature_list.each do |feature| %>
          <label class="flex items-center group cursor-pointer">
            <input type="checkbox"
                   name="features[]"
                   value="<%= feature[:key] %>"
                   data-filter="feature"
                   data-action="change->search#filterChanged"
                   class="rounded-soft border-warm-gray-300 text-terra-500 focus:ring-terra-500"
                   <%= 'checked' if @search_criteria_for_view[:features]&.include?(feature[:key]) %>>
            <span class="ml-2 text-sm text-warm-gray-600 group-hover:text-warm-gray-900 transition-colors"><%= feature[:label] %></span>
          </label>
        <% end %>
      </div>
    </div>
  <% end %>

  <%# Mobile: Apply filters button %>
  <div class="lg:hidden pt-5 border-t border-warm-gray-200">
    <button type="submit"
            class="w-full bg-terra-500 text-white font-medium py-3 px-4 rounded-soft shadow-soft hover:bg-terra-600 hover:shadow-medium focus:outline-none focus:ring-2 focus:ring-terra-500 focus:ring-offset-2 transition-all duration-200"
            data-action="click->search#applyAndClose">
      <%= I18n.t("search.show_results", default: "Show Results") %>
    </button>
  </div>
<% end %>
