<PhoenixKitWeb.Components.LayoutWrapper.app_layout
  flash={@flash}
  phoenix_kit_current_scope={assigns[:phoenix_kit_current_scope]}
  page_title="Storage Dimensions"
  current_path={@current_path}
  current_locale={@current_locale}
  project_title={@project_title}
>
  <div class="container mx-auto px-4 py-8 max-w-6xl">
    <.admin_page_header
      back={PhoenixKit.Utils.Routes.path("/admin/settings/media")}
      title="Instance Dimensions"
      subtitle="Manage dimension presets for automatic file instance generation"
    />

    <%!-- Info Alert --%>
    <div class="alert alert-info mb-6">
      <.icon name="hero-information-circle" class="w-5 h-5" />
      <div>
        <div class="flex justify-between items-start">
          <div>
            <h3 class="font-bold">Dimension Presets</h3>
            <p class="text-sm">
              Dimensions define the target sizes for automatic variant generation. Images are resized and videos are transcoded to these preset sizes.
              The migration seeds 8 default dimensions: 4 image sizes (thumbnail, small, medium, large) and 4 video variants (360p, 720p, 1080p, video_thumbnail).
            </p>
          </div>
          <button
            phx-click="reset_dimensions_to_defaults"
            data-confirm="Are you sure? This will delete all current dimensions and restore the default 8 dimensions. This action cannot be undone."
            class="btn btn-error btn-sm"
          >
            <.icon name="hero-arrow-path" class="w-4 h-4 mr-1" /> Reset to Defaults
          </button>
        </div>
      </div>
    </div>

    <%!-- Dimensions List --%>
    <div class="card bg-base-100 shadow-xl">
      <div class="card-body">
        <%= if length(@dimensions) == 0 do %>
          <div class="text-center py-12">
            <.icon name="hero-photo" class="w-16 h-16 mx-auto mb-4 text-base-content/30" />
            <p class="text-lg text-base-content/70 mb-2">No dimensions configured</p>
            <p class="text-sm text-base-content/50 mb-4">
              Create your first dimension preset to start generating file variants.
            </p>
            <div class="flex justify-center gap-4">
              <.link
                navigate={
                  PhoenixKit.Utils.Routes.path("/admin/settings/media/dimensions/new/image")
                }
                class="btn btn-info"
              >
                <.icon name="hero-plus" class="w-4 h-4 mr-1" /> Add Image Dimension
              </.link>
              <.link
                navigate={
                  PhoenixKit.Utils.Routes.path("/admin/settings/media/dimensions/new/video")
                }
                class="btn btn-warning"
              >
                <.icon name="hero-plus" class="w-4 h-4 mr-1" /> Add Video Dimension
              </.link>
            </div>
          </div>
        <% else %>
          <%!-- Image Dimensions --%>
          <div class="mb-8">
            <div class="flex justify-between items-center mb-4">
              <h3 class="text-2xl font-bold text-info">
                <.icon name="hero-photo" class="w-6 h-6 mr-2" /> Image Dimensions
              </h3>
              <.link
                navigate={
                  PhoenixKit.Utils.Routes.path("/admin/settings/media/dimensions/new/image")
                }
                class="btn btn-info"
              >
                <.icon name="hero-plus" class="w-4 h-4 mr-1" /> Add Image Dimension
              </.link>
            </div>
            <div class="overflow-x-auto">
              <table class="table table-zebra">
                <thead>
                  <tr>
                    <th>Name</th>
                    <th>
                      <%= if Enum.any?(Enum.filter(@dimensions, & &1.applies_to in ["image", "both"]), & &1.maintain_aspect_ratio) do %>
                        Target Width / Size
                      <% else %>
                        Size
                      <% end %>
                    </th>
                    <th>Mode</th>
                    <th>Quality</th>
                    <th>Format</th>
                    <th>Status</th>
                    <th class="text-right">Actions</th>
                  </tr>
                </thead>
                <tbody>
                  <%= for dimension <- Enum.filter(@dimensions, & &1.applies_to in ["image", "both"]) do %>
                    <tr>
                      <td>
                        <div class="font-bold">{dimension.name}</div>
                      </td>
                      <td>
                        <span class="font-mono text-sm">
                          <%= if dimension.maintain_aspect_ratio do %>
                            {dimension.width}px
                          <% else %>
                            {format_dimension_size(dimension.width, dimension.height)}
                          <% end %>
                        </span>
                      </td>
                      <td>
                        <%= if dimension.maintain_aspect_ratio do %>
                          <span class="badge badge-info badge-sm h-auto">Aspect Ratio</span>
                        <% else %>
                          <span class="badge badge-secondary badge-sm h-auto">Fixed</span>
                        <% end %>
                      </td>
                      <td>
                        <span class="text-sm">{dimension.quality}%</span>
                      </td>
                      <td>
                        <%= if dimension.format do %>
                          <span class="text-sm font-mono uppercase">{dimension.format}</span>
                        <% else %>
                          <span class="text-sm text-base-content/60">Original</span>
                        <% end %>
                      </td>
                      <td>
                        <%= if dimension.enabled do %>
                          <span class="badge badge-success badge-sm h-auto">Enabled</span>
                        <% else %>
                          <span class="badge badge-error badge-sm h-auto">Disabled</span>
                        <% end %>
                      </td>
                      <td>
                        <div class="flex justify-end gap-1">
                          <.link
                            navigate={
                              PhoenixKit.Utils.Routes.path(
                                "/admin/settings/media/dimensions/#{dimension.uuid}/edit"
                              )
                            }
                            class="btn btn-xs btn-ghost"
                            title="Edit dimension"
                          >
                            <.icon name="hero-pencil" class="w-4 h-4" />
                          </.link>
                          <button
                            phx-click="toggle_dimension"
                            phx-debounce="1000"
                            phx-value-id={dimension.uuid}
                            class="btn btn-xs btn-ghost"
                            title={
                              if dimension.enabled,
                                do: "Disable dimension",
                                else: "Enable dimension"
                            }
                          >
                            <%= if dimension.enabled do %>
                              <.icon name="hero-eye-slash" class="w-4 h-4" />
                            <% else %>
                              <.icon name="hero-eye" class="w-4 h-4" />
                            <% end %>
                          </button>
                          <button
                            phx-click="delete_dimension"
                            phx-debounce="1000"
                            phx-value-id={dimension.uuid}
                            data-confirm="Are you sure you want to delete this dimension?"
                            class="btn btn-xs btn-ghost text-error"
                            title="Delete dimension"
                          >
                            <.icon name="hero-trash" class="w-4 h-4" />
                          </button>
                        </div>
                      </td>
                    </tr>
                  <% end %>
                </tbody>
              </table>
            </div>
          </div>

          <%!-- Video Dimensions --%>
          <div>
            <div class="flex justify-between items-center mb-4">
              <h3 class="text-2xl font-bold text-warning">
                <.icon name="hero-video-camera" class="w-6 h-6 mr-2" /> Video Dimensions
              </h3>
              <.link
                navigate={
                  PhoenixKit.Utils.Routes.path("/admin/settings/media/dimensions/new/video")
                }
                class="btn btn-warning"
              >
                <.icon name="hero-plus" class="w-4 h-4 mr-1" /> Add Video Dimension
              </.link>
            </div>
            <div class="overflow-x-auto">
              <table class="table table-zebra">
                <thead>
                  <tr>
                    <th>Name</th>
                    <th>
                      <%= if Enum.any?(Enum.filter(@dimensions, & &1.applies_to in ["video", "both"]), & &1.maintain_aspect_ratio) do %>
                        Target Width / Resolution
                      <% else %>
                        Resolution
                      <% end %>
                    </th>
                    <th>Mode</th>
                    <th>Quality (CRF)</th>
                    <th>Codec</th>
                    <th>Status</th>
                    <th class="text-right">Actions</th>
                  </tr>
                </thead>
                <tbody>
                  <%= for dimension <- Enum.filter(@dimensions, & &1.applies_to in ["video", "both"]) do %>
                    <tr>
                      <td>
                        <div class="font-bold">{dimension.name}</div>
                      </td>
                      <td>
                        <span class="font-mono text-sm">
                          <%= if dimension.maintain_aspect_ratio do %>
                            {dimension.width}px
                          <% else %>
                            {format_dimension_size(dimension.width, dimension.height)}
                          <% end %>
                        </span>
                      </td>
                      <td>
                        <%= if dimension.maintain_aspect_ratio do %>
                          <span class="badge badge-info badge-sm h-auto">Aspect Ratio</span>
                        <% else %>
                          <span class="badge badge-secondary badge-sm h-auto">Fixed</span>
                        <% end %>
                      </td>
                      <td>
                        <span class="text-sm">{dimension.quality}</span>
                      </td>
                      <td>
                        <%= if dimension.format do %>
                          <span class="text-sm font-mono uppercase">{dimension.format}</span>
                        <% else %>
                          <span class="text-sm text-base-content/60">Original</span>
                        <% end %>
                      </td>
                      <td>
                        <%= if dimension.enabled do %>
                          <span class="badge badge-success badge-sm h-auto">Enabled</span>
                        <% else %>
                          <span class="badge badge-error badge-sm h-auto">Disabled</span>
                        <% end %>
                      </td>
                      <td>
                        <div class="flex justify-end gap-1">
                          <.link
                            navigate={
                              PhoenixKit.Utils.Routes.path(
                                "/admin/settings/media/dimensions/#{dimension.uuid}/edit"
                              )
                            }
                            class="btn btn-xs btn-ghost"
                            title="Edit dimension"
                          >
                            <.icon name="hero-pencil" class="w-4 h-4" />
                          </.link>
                          <button
                            phx-click="toggle_dimension"
                            phx-debounce="1000"
                            phx-value-id={dimension.uuid}
                            class="btn btn-xs btn-ghost"
                            title={
                              if dimension.enabled,
                                do: "Disable dimension",
                                else: "Enable dimension"
                            }
                          >
                            <%= if dimension.enabled do %>
                              <.icon name="hero-eye-slash" class="w-4 h-4" />
                            <% else %>
                              <.icon name="hero-eye" class="w-4 h-4" />
                            <% end %>
                          </button>
                          <button
                            phx-click="delete_dimension"
                            phx-debounce="1000"
                            phx-value-id={dimension.uuid}
                            data-confirm="Are you sure you want to delete this dimension?"
                            class="btn btn-xs btn-ghost text-error"
                            title="Delete dimension"
                          >
                            <.icon name="hero-trash" class="w-4 h-4" />
                          </button>
                        </div>
                      </td>
                    </tr>
                  <% end %>
                </tbody>
              </table>
            </div>
          </div>
        <% end %>
      </div>
    </div>

    <%!-- Help Section --%>
    <div class="alert mt-6">
      <.icon name="hero-light-bulb" class="w-5 h-5" />
      <div>
        <h3 class="font-bold">Variant Generation System</h3>
        <p class="text-sm">
          <strong>Quality Settings:</strong>
          Images use 1-100 scale (compression quality)<br />
          <strong>Video Settings:</strong>
          Videos use CRF 0-51 scale (lower = higher quality)<br />
          <strong>Format Override:</strong>
          Leave empty to preserve original format, or specify jpg/png/webp/mp4
        </p>
      </div>
    </div>
  </div>
</PhoenixKitWeb.Components.LayoutWrapper.app_layout>
