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

    <.form for={@changeset} phx-change="validate" phx-submit="save" class="space-y-6">
      <%!-- Basic Information --%>
      <div class="card bg-base-100 shadow-sm">
        <div class="card-body gap-4">
          <h2 class="card-title text-lg">
            <.icon name="hero-tag" class="w-5 h-5" /> {gettext("Basic Information")}
          </h2>

          <fieldset class="fieldset">
            <legend class="fieldset-legend font-semibold">
              {gettext("Dimension Name")} *
            </legend>
            <input
              type="text"
              name="dimension[name]"
              phx-debounce="blur"
              value={get_field_value(@changeset, :name)}
              class={"input input-bordered w-full #{input_class(@changeset, :name)}"}
              placeholder={gettext("e.g., thumbnail, medium, 720p")}
              required
            />
            {render_error(@changeset, :name)}
            <p class="text-xs text-base-content/50 mt-1">
              {gettext("A unique name to identify this dimension preset")}
            </p>
          </fieldset>

          <input type="hidden" name="dimension[applies_to]" value={@dimension_type} />
        </div>
      </div>

      <%!-- Size Configuration --%>
      <div class="card bg-base-100 shadow-sm">
        <div class="card-body gap-4">
          <h2 class="card-title text-lg">
            <.icon name="hero-arrows-pointing-out" class="w-5 h-5" />
            {gettext("Size Configuration")}
          </h2>

          <%!-- Aspect Ratio Toggle --%>
          <div class="form-control">
            <.checkbox
              name="dimension[maintain_aspect_ratio]"
              checked={get_field_value(@changeset, :maintain_aspect_ratio) != false}
            >
              <span class="font-semibold">{gettext("Maintain Aspect Ratio")}</span>
              <:description>
                <%= if get_field_value(@changeset, :maintain_aspect_ratio) != false do %>
                  {gettext("Only width is used, height is calculated automatically")}
                <% else %>
                  {gettext("Both width and height are used for fixed dimensions")}
                <% end %>
              </:description>
            </.checkbox>
          </div>

          <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
            <fieldset class="fieldset">
              <legend class="fieldset-legend font-semibold">
                <%= if get_field_value(@changeset, :maintain_aspect_ratio) != false do %>
                  {gettext("Target Width (px)")} *
                <% else %>
                  {gettext("Width (px)")} *
                <% end %>
              </legend>
              <input
                type="number"
                name="dimension[width]"
                phx-debounce="blur"
                value={get_field_value(@changeset, :width)}
                class={"input input-bordered w-full #{input_class(@changeset, :width)}"}
                placeholder="800"
                min="1"
                required
              />
              {render_error(@changeset, :width)}
            </fieldset>

            <%= if get_field_value(@changeset, :maintain_aspect_ratio) == false do %>
              <fieldset class="fieldset">
                <legend class="fieldset-legend font-semibold">
                  {gettext("Height (px)")} *
                </legend>
                <input
                  type="number"
                  name="dimension[height]"
                  phx-debounce="blur"
                  value={get_field_value(@changeset, :height)}
                  class={"input input-bordered w-full #{input_class(@changeset, :height)}"}
                  placeholder="600"
                  min="1"
                  required
                />
                {render_error(@changeset, :height)}
              </fieldset>
            <% end %>
          </div>
        </div>
      </div>

      <%!-- Quality & Format --%>
      <div class="card bg-base-100 shadow-sm">
        <div class="card-body gap-4">
          <h2 class="card-title text-lg">
            <.icon name="hero-adjustments-horizontal" class="w-5 h-5" />
            {gettext("Quality & Format")}
          </h2>

          <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
            <fieldset class="fieldset">
              <legend class="fieldset-legend font-semibold">
                <%= if @dimension_type == "video" do %>
                  {gettext("Quality (CRF)")}
                <% else %>
                  {gettext("Quality")}
                <% end %>
              </legend>
              <input
                type="number"
                name="dimension[quality]"
                phx-debounce="blur"
                value={get_field_value(@changeset, :quality)}
                class={"input input-bordered w-full #{input_class(@changeset, :quality)}"}
                placeholder={if @dimension_type == "video", do: "23", else: "85"}
                min={if @dimension_type == "video", do: "0", else: "1"}
                max={if @dimension_type == "video", do: "51", else: "100"}
              />
              {render_error(@changeset, :quality)}
              <p class="text-xs text-base-content/50 mt-1">
                <%= if @dimension_type == "video" do %>
                  {gettext("CRF 0-51, lower = higher quality. 18-28 recommended.")}
                <% else %>
                  {gettext("1-100, higher = better quality. 80-90 recommended.")}
                <% end %>
              </p>
            </fieldset>

            <fieldset class="fieldset">
              <legend class="fieldset-legend font-semibold">
                <%= if @dimension_type == "video" do %>
                  {gettext("Codec Override")}
                <% else %>
                  {gettext("Format Override")}
                <% end %>
              </legend>
              <% format_value = get_field_value(@changeset, :format) %>
              <label class={"select w-full #{input_class(@changeset, :format)}"}>
                <select name="dimension[format]">
                  <option value="">{gettext("Preserve original")}</option>
                  <%= if @dimension_type != "video" do %>
                    <option value="jpg" selected={format_value == "jpg"}>JPEG</option>
                    <option value="png" selected={format_value == "png"}>PNG</option>
                    <option value="webp" selected={format_value == "webp"}>WebP</option>
                  <% end %>
                  <%= if @dimension_type == "video" do %>
                    <option value="mp4" selected={format_value == "mp4"}>MP4</option>
                  <% end %>
                </select>
              </label>
              {render_error(@changeset, :format)}
              <p class="text-xs text-base-content/50 mt-1">
                {gettext("Leave empty to preserve the original file format")}
              </p>
            </fieldset>
          </div>
        </div>
      </div>

      <%!-- Alternative Formats (image dimensions only) --%>
      <%= if @dimension_type != "video" do %>
        <div class="card bg-base-100 shadow-sm">
          <div class="card-body gap-4">
            <h2 class="card-title text-lg">
              <.icon name="hero-document-duplicate" class="w-5 h-5" />
              {gettext("Alternative Formats")}
            </h2>
            <p class="text-sm text-base-content/60">
              {gettext(
                "Generate additional format variants alongside the primary format. Modern formats like WebP and AVIF offer better compression."
              )}
            </p>

            <input type="hidden" name="dimension[alternative_formats][]" value="" />
            <div class="flex flex-wrap gap-4">
              <%= for {fmt, label} <- [{"webp", "WebP"}, {"avif", "AVIF"}, {"png", "PNG"}, {"jpg", "JPEG"}] do %>
                <%= if fmt != @primary_format do %>
                  <label class="label cursor-pointer gap-2">
                    <input
                      type="checkbox"
                      name="dimension[alternative_formats][]"
                      value={fmt}
                      checked={fmt in @current_alternatives}
                      class="checkbox checkbox-sm checkbox-primary"
                    />
                    <span class="label-text">{label}</span>
                  </label>
                <% end %>
              <% end %>
            </div>
            {render_error(@changeset, :alternative_formats)}
          </div>
        </div>
      <% end %>

      <%!-- Actions --%>
      <div class="flex justify-end gap-3">
        <.link
          navigate={PhoenixKit.Utils.Routes.path("/admin/settings/media/dimensions")}
          class="btn btn-ghost"
        >
          {gettext("Cancel")}
        </.link>
        <button type="submit" class="btn btn-primary" disabled={!@changeset.valid?}>
          <.icon name="hero-check" class="w-4 h-4" /> {@form_action}
        </button>
      </div>
    </.form>
  </div>
</PhoenixKitWeb.Components.LayoutWrapper.app_layout>
