<%
  # Determine the public page URL
  page_path = @page.slug == 'home' ? '/' : "/p/#{@page.slug}"
  public_page_url = "#{request.protocol}#{request.host_with_port}#{page_path}"
  page_parts = @page.page_parts.where(website_id: current_website&.id, show_in_editor: true).order(:order_in_editor)
%>

<div class="max-w-5xl mx-auto">
  <!-- Header -->
  <div class="mb-6 flex items-center justify-between">
    <%= link_to '← Back to Pages', site_admin_pages_path, class: 'text-blue-600 hover:text-blue-800' %>
    <div class="flex items-center gap-3">
      <%= link_to public_page_url, target: '_blank', class: 'inline-flex items-center px-3 py-1.5 border border-gray-300 text-gray-700 text-sm rounded-lg hover:bg-gray-50' do %>
        <svg class="w-4 h-4 mr-1.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
          <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 6H6a2 2 0 00-2 2v10a2 2 0 002 2h10a2 2 0 002-2v-4M14 4h6m0 0v6m0-6L10 14"></path>
        </svg>
        View on Site
      <% end %>
      <%= link_to edit_site_admin_page_path(@page), class: 'inline-flex items-center px-3 py-1.5 text-sm bg-blue-600 text-white hover:bg-blue-700 rounded-lg shadow-sm' do %>
        <svg class="w-4 h-4 mr-1.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
          <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"></path>
        </svg>
        Edit Page
      <% end %>
      <%= link_to settings_site_admin_page_path(@page), class: 'inline-flex items-center px-3 py-1.5 text-sm text-gray-600 hover:text-gray-900 hover:bg-gray-100 rounded-lg border border-gray-300' do %>
        <svg class="w-4 h-4 mr-1.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
          <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z"></path>
          <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"></path>
        </svg>
        Settings
      <% end %>
    </div>
  </div>

  <!-- Page Info Card -->
  <div class="bg-white rounded-lg shadow overflow-hidden mb-6">
    <div class="px-6 py-5">
      <div class="flex items-start justify-between">
        <div>
          <h1 class="text-2xl font-bold text-gray-900"><%= @page.slug.titleize %></h1>
          <p class="text-sm text-gray-500 mt-1">
            <span class="font-mono bg-gray-100 px-2 py-0.5 rounded">/<%= @page.slug %></span>
          </p>
        </div>
        <div class="flex items-center gap-4 text-sm text-gray-500">
          <div class="text-right">
            <p class="text-xs text-gray-400">Navigation</p>
            <p>
              <% if @page.show_in_top_nav %>
                <span class="inline-flex items-center text-green-600">
                  <svg class="w-3 h-3 mr-1" fill="currentColor" viewBox="0 0 20 20"><path fill-rule="evenodd" d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z" clip-rule="evenodd"></path></svg>
                  Top Nav
                </span>
              <% end %>
              <% if @page.show_in_footer %>
                <span class="inline-flex items-center text-green-600 ml-2">
                  <svg class="w-3 h-3 mr-1" fill="currentColor" viewBox="0 0 20 20"><path fill-rule="evenodd" d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z" clip-rule="evenodd"></path></svg>
                  Footer
                </span>
              <% end %>
              <% unless @page.show_in_top_nav || @page.show_in_footer %>
                <span class="text-gray-400">Not in navigation</span>
              <% end %>
            </p>
          </div>
        </div>
      </div>
    </div>
  </div>

  <!-- Content Sections -->
  <div class="bg-white rounded-lg shadow overflow-hidden">
    <div class="px-6 py-4 border-b border-gray-200 flex items-center justify-between">
      <div>
        <h2 class="text-lg font-semibold text-gray-900">Content Sections</h2>
        <p class="text-sm text-gray-500"><%= page_parts.count %> editable section<%= page_parts.count == 1 ? '' : 's' %> on this page</p>
      </div>
    </div>

    <% if page_parts.any? %>
      <div class="divide-y divide-gray-100">
        <% page_parts.each do |page_part| %>
          <% editor_setup = page_part.editor_setup || {} %>
          <% tab_title = editor_setup['tabTitleKey'] ? I18n.t(editor_setup['tabTitleKey'], default: page_part.page_part_key.titleize) : page_part.page_part_key.titleize %>
          <% page_content = @page.page_contents.find_by(page_part_key: page_part.page_part_key) %>
          <% is_visible = page_content&.visible_on_page %>

          <div class="px-6 py-4 flex items-center justify-between hover:bg-gray-50 transition-colors">
            <div class="flex items-center flex-1">
              <div class="w-10 h-10 rounded-lg bg-blue-50 border border-blue-100 flex items-center justify-center mr-4">
                <svg class="w-5 h-5 text-blue-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                  <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 5a1 1 0 011-1h14a1 1 0 011 1v2a1 1 0 01-1 1H5a1 1 0 01-1-1V5zM4 13a1 1 0 011-1h6a1 1 0 011 1v6a1 1 0 01-1 1H5a1 1 0 01-1-1v-6zM16 13a1 1 0 011-1h2a1 1 0 011 1v6a1 1 0 01-1 1h-2a1 1 0 01-1-1v-6z"></path>
                </svg>
              </div>
              <div class="flex-1">
                <p class="font-medium text-gray-900"><%= tab_title %></p>
                <p class="text-sm text-gray-500"><%= page_part.page_part_key %></p>
              </div>
            </div>

            <div class="flex items-center gap-3">
              <!-- Visibility Toggle -->
              <% if page_content %>
                <%= button_to toggle_visibility_site_admin_page_page_part_path(@page, page_part),
                              method: :patch,
                              class: "inline-flex items-center px-2.5 py-1 text-xs rounded-full cursor-pointer transition-colors #{is_visible ? 'bg-green-100 text-green-700 hover:bg-green-200' : 'bg-gray-200 text-gray-600 hover:bg-gray-300'}",
                              title: is_visible ? 'Click to hide this section' : 'Click to show this section',
                              data: { turbo: false } do %>
                  <% if is_visible %>
                    <svg class="w-3.5 h-3.5 mr-1" fill="currentColor" viewBox="0 0 20 20">
                      <path d="M10 12a2 2 0 100-4 2 2 0 000 4z"></path>
                      <path fill-rule="evenodd" d="M.458 10C1.732 5.943 5.522 3 10 3s8.268 2.943 9.542 7c-1.274 4.057-5.064 7-9.542 7S1.732 14.057.458 10zM14 10a4 4 0 11-8 0 4 4 0 018 0z" clip-rule="evenodd"></path>
                    </svg>
                    Visible
                  <% else %>
                    <svg class="w-3.5 h-3.5 mr-1" fill="currentColor" viewBox="0 0 20 20">
                      <path fill-rule="evenodd" d="M3.707 2.293a1 1 0 00-1.414 1.414l14 14a1 1 0 001.414-1.414l-1.473-1.473A10.014 10.014 0 0019.542 10C18.268 5.943 14.478 3 10 3a9.958 9.958 0 00-4.512 1.074l-1.78-1.781zm4.261 4.26l1.514 1.515a2.003 2.003 0 012.45 2.45l1.514 1.514a4 4 0 00-5.478-5.478z" clip-rule="evenodd"></path>
                      <path d="M12.454 16.697L9.75 13.992a4 4 0 01-3.742-3.741L2.335 6.578A9.98 9.98 0 00.458 10c1.274 4.057 5.065 7 9.542 7 .847 0 1.669-.105 2.454-.303z"></path>
                    </svg>
                    Hidden
                  <% end %>
                <% end %>
              <% else %>
                <span class="px-2.5 py-1 text-xs rounded-full bg-yellow-100 text-yellow-700">Not configured</span>
              <% end %>

              <!-- Edit Button -->
              <%= link_to edit_site_admin_page_page_part_path(@page, page_part),
                          class: 'inline-flex items-center px-4 py-2 bg-blue-600 text-white text-sm rounded-lg hover:bg-blue-700 transition-colors shadow-sm' do %>
                <svg class="w-4 h-4 mr-1.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                  <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"></path>
                </svg>
                Edit Content
              <% end %>
            </div>
          </div>
        <% end %>
      </div>
    <% else %>
      <div class="px-6 py-12 text-center text-gray-500">
        <svg class="mx-auto h-12 w-12 text-gray-300 mb-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
          <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"></path>
        </svg>
        <p class="text-gray-600">No editable content sections configured for this page</p>
        <p class="text-sm text-gray-400 mt-1">Content sections are defined by the theme</p>
      </div>
    <% end %>
  </div>
</div>
