<div class="max-w-6xl mx-auto">
  <div class="mb-6">
    <%= link_to '&larr; Back to Widgets'.html_safe, site_admin_widgets_path, class: 'text-blue-600 hover:text-blue-800' %>
  </div>

  <div class="bg-white rounded-lg shadow overflow-hidden">
    <div class="px-6 py-4 border-b border-gray-200 flex justify-between items-center">
      <div>
        <h1 class="text-2xl font-bold text-gray-900"><%= @widget.name %></h1>
        <p class="text-sm text-gray-500 font-mono">ID: <%= @widget.widget_key %></p>
      </div>
      <div class="flex gap-2">
        <%= link_to 'Edit', edit_site_admin_widget_path(@widget), class: 'px-4 py-2 border border-gray-300 text-gray-700 rounded-lg hover:bg-gray-50' %>
        <%= link_to 'Preview', preview_site_admin_widget_path(@widget), target: '_blank', class: 'px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700' %>
      </div>
    </div>

    <div class="p-6">
      <!-- Stats -->
      <div class="grid grid-cols-2 md:grid-cols-4 gap-4 mb-8">
        <div class="bg-gray-50 rounded-lg p-4 text-center">
          <div class="text-2xl font-bold text-gray-900"><%= number_with_delimiter(@widget.impressions_count) %></div>
          <div class="text-sm text-gray-500">Impressions</div>
        </div>
        <div class="bg-gray-50 rounded-lg p-4 text-center">
          <div class="text-2xl font-bold text-gray-900"><%= number_with_delimiter(@widget.clicks_count) %></div>
          <div class="text-sm text-gray-500">Clicks</div>
        </div>
        <div class="bg-gray-50 rounded-lg p-4 text-center">
          <div class="text-2xl font-bold text-gray-900">
            <%= @widget.impressions_count > 0 ? number_to_percentage((@widget.clicks_count.to_f / @widget.impressions_count) * 100, precision: 1) : '0%' %>
          </div>
          <div class="text-sm text-gray-500">CTR</div>
        </div>
        <div class="bg-gray-50 rounded-lg p-4 text-center">
          <div class="text-2xl font-bold text-gray-900"><%= @widget.properties_query.count %></div>
          <div class="text-sm text-gray-500">Properties</div>
        </div>
      </div>

      <!-- Embed Code -->
      <div class="mb-8">
        <h2 class="text-lg font-medium text-gray-900 mb-4">Embed Code</h2>
        <p class="text-sm text-gray-600 mb-4">Copy and paste this code into any HTML page to display your properties:</p>

        <div class="space-y-4">
          <!-- Script Embed -->
          <div>
            <label class="block text-sm font-medium text-gray-700 mb-2">JavaScript Embed (Recommended)</label>
            <div class="relative">
              <pre class="bg-gray-900 text-green-400 p-4 rounded-lg text-sm overflow-x-auto font-mono" id="script-embed-code"><%= @widget.embed_code(host: request.host_with_port) %></pre>
              <button type="button"
                      onclick="copyToClipboard('script-embed-code')"
                      class="absolute top-2 right-2 px-3 py-1 bg-gray-700 text-white text-xs rounded hover:bg-gray-600">
                Copy
              </button>
            </div>
          </div>

          <!-- Iframe Embed -->
          <div>
            <label class="block text-sm font-medium text-gray-700 mb-2">Iframe Embed (Alternative)</label>
            <div class="relative">
              <pre class="bg-gray-900 text-green-400 p-4 rounded-lg text-sm overflow-x-auto font-mono" id="iframe-embed-code"><%= @widget.iframe_embed_code(host: request.host_with_port) %></pre>
              <button type="button"
                      onclick="copyToClipboard('iframe-embed-code')"
                      class="absolute top-2 right-2 px-3 py-1 bg-gray-700 text-white text-xs rounded hover:bg-gray-600">
                Copy
              </button>
            </div>
          </div>
        </div>
      </div>

      <!-- Configuration Summary -->
      <div class="grid grid-cols-1 md:grid-cols-2 gap-8">
        <div>
          <h2 class="text-lg font-medium text-gray-900 mb-4">Display Settings</h2>
          <dl class="space-y-2 text-sm">
            <div class="flex justify-between">
              <dt class="text-gray-500">Layout</dt>
              <dd class="text-gray-900 font-medium"><%= @widget.layout.capitalize %></dd>
            </div>
            <% if @widget.layout == 'grid' %>
              <div class="flex justify-between">
                <dt class="text-gray-500">Columns</dt>
                <dd class="text-gray-900 font-medium"><%= @widget.columns %></dd>
              </div>
            <% end %>
            <div class="flex justify-between">
              <dt class="text-gray-500">Max Properties</dt>
              <dd class="text-gray-900 font-medium"><%= @widget.max_properties %></dd>
            </div>
            <div class="flex justify-between">
              <dt class="text-gray-500">Show Search</dt>
              <dd class="text-gray-900 font-medium"><%= @widget.show_search ? 'Yes' : 'No' %></dd>
            </div>
            <div class="flex justify-between">
              <dt class="text-gray-500">Show Pagination</dt>
              <dd class="text-gray-900 font-medium"><%= @widget.show_pagination ? 'Yes' : 'No' %></dd>
            </div>
          </dl>
        </div>

        <div>
          <h2 class="text-lg font-medium text-gray-900 mb-4">Filters</h2>
          <dl class="space-y-2 text-sm">
            <div class="flex justify-between">
              <dt class="text-gray-500">Listing Type</dt>
              <dd class="text-gray-900 font-medium"><%= @widget.listing_type&.capitalize || 'All' %></dd>
            </div>
            <% if @widget.min_bedrooms.present? || @widget.max_bedrooms.present? %>
              <div class="flex justify-between">
                <dt class="text-gray-500">Bedrooms</dt>
                <dd class="text-gray-900 font-medium">
                  <%= @widget.min_bedrooms.present? ? "#{@widget.min_bedrooms}+" : '' %>
                  <%= @widget.max_bedrooms.present? ? "up to #{@widget.max_bedrooms}" : '' %>
                </dd>
              </div>
            <% end %>
            <div class="flex justify-between">
              <dt class="text-gray-500">Highlighted Only</dt>
              <dd class="text-gray-900 font-medium"><%= @widget.highlighted_only ? 'Yes' : 'No' %></dd>
            </div>
            <div class="flex justify-between">
              <dt class="text-gray-500">Status</dt>
              <dd>
                <% if @widget.active %>
                  <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-green-100 text-green-800">Active</span>
                <% else %>
                  <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-gray-100 text-gray-600">Inactive</span>
                <% end %>
              </dd>
            </div>
          </dl>
        </div>
      </div>

      <% if @widget.allowed_domains.present? %>
        <div class="mt-8 pt-8 border-t">
          <h2 class="text-lg font-medium text-gray-900 mb-4">Allowed Domains</h2>
          <div class="flex flex-wrap gap-2">
            <% @widget.allowed_domains.each do |domain| %>
              <span class="inline-flex items-center px-3 py-1 rounded-full text-sm bg-gray-100 text-gray-700 font-mono">
                <%= domain %>
              </span>
            <% end %>
          </div>
        </div>
      <% end %>

      <!-- Theme Preview -->
      <div class="mt-8 pt-8 border-t">
        <h2 class="text-lg font-medium text-gray-900 mb-4">Theme Colors</h2>
        <div class="flex flex-wrap gap-4">
          <% theme = @widget.effective_theme %>
          <div class="flex items-center gap-2">
            <div class="w-8 h-8 rounded border" style="background-color: <%= theme['primary_color'] %>"></div>
            <span class="text-sm text-gray-600">Primary</span>
          </div>
          <div class="flex items-center gap-2">
            <div class="w-8 h-8 rounded border" style="background-color: <%= theme['secondary_color'] %>"></div>
            <span class="text-sm text-gray-600">Secondary</span>
          </div>
          <div class="flex items-center gap-2">
            <div class="w-8 h-8 rounded border" style="background-color: <%= theme['background_color'] %>"></div>
            <span class="text-sm text-gray-600">Background</span>
          </div>
          <div class="flex items-center gap-2">
            <div class="w-8 h-8 rounded border" style="background-color: <%= theme['card_background'] %>"></div>
            <span class="text-sm text-gray-600">Card</span>
          </div>
        </div>
      </div>
    </div>
  </div>

  <!-- Danger Zone -->
  <div class="mt-8 bg-red-50 rounded-lg p-6">
    <h3 class="text-lg font-medium text-red-900 mb-2">Danger Zone</h3>
    <p class="text-sm text-red-700 mb-4">Deleting this widget will break any embeds using this widget key.</p>
    <%= button_to 'Delete Widget', site_admin_widget_path(@widget),
        method: :delete,
        data: { turbo_confirm: 'Are you sure you want to delete this widget? This cannot be undone.' },
        class: 'px-4 py-2 bg-red-600 text-white rounded-lg hover:bg-red-700' %>
  </div>
</div>

<script>
function copyToClipboard(elementId) {
  const element = document.getElementById(elementId);
  const text = element.textContent;
  navigator.clipboard.writeText(text).then(() => {
    const button = element.parentElement.querySelector('button');
    const originalText = button.textContent;
    button.textContent = 'Copied!';
    setTimeout(() => { button.textContent = originalText; }, 2000);
  });
}
</script>
