<%= drawer(reload_on_close: true) do %>
  <%= render "account/transactions/header", entry: @entry %>

  <div class="space-y-2">
    <!-- Overview Section -->
    <%= disclosure t(".overview") do %>
      <div class="pb-4">
        <%= styled_form_with model: @entry,
              url: account_transaction_path(@entry),
              class: "space-y-2",
              data: { controller: "auto-submit-form" } do |f| %>

          <%= f.text_field @entry.enriched_at.present? ? :enriched_name : :name,
                label: t(".name_label"),
                "data-auto-submit-form-target": "auto" %>

          <%= f.date_field :date,
                label: t(".date_label"),
                max: Date.current,
                "data-auto-submit-form-target": "auto" %>

          <% unless @entry.account_transaction.transfer? %>
            <div class="flex items-center gap-2">
              <%= f.select :nature,
                    [["Expense", "outflow"], ["Income", "inflow"]],
                    { container_class: "w-1/3", label: t(".nature"), selected: @entry.amount.negative? ? "inflow" : "outflow" },
                    { data: { "auto-submit-form-target": "auto" } } %>

              <%= f.money_field :amount, label: t(".amount"),
                    container_class: "w-2/3",
                    auto_submit: true,
                    min: 0,
                    value: @entry.amount.abs %>
            </div>

            <%= f.fields_for :entryable do |ef| %>
              <%= ef.collection_select :category_id,
                      Current.family.categories.alphabetically,
                    :id, :name,
                    { label: t(".category_label"),
                      class: "text-subdued", include_blank: t(".uncategorized") },
                    "data-auto-submit-form-target": "auto" %>
            <% end %>
          <% end %>

        <% end %>
      </div>
    <% end %>

    <!-- Details Section -->
    <%= disclosure t(".details"), default_open: false do %>
      <div class="pb-4">
        <%= styled_form_with model: @entry,
              url: account_transaction_path(@entry),
              class: "space-y-2",
              data: { controller: "auto-submit-form" } do |f| %>
          <% unless @entry.account_transaction.transfer? %>
            <%= f.select :account,
                options_for_select(
                  Current.family.accounts.alphabetically.pluck(:name, :id),
                  @entry.account_id
                ),
                { label: t(".account_label") },
                { disabled: true } %>

            <%= f.fields_for :entryable do |ef| %>

              <%= ef.collection_select :merchant_id,
                      Current.family.merchants.alphabetically,
                    :id, :name,
                    { include_blank: t(".none"),
                      label: t(".merchant_label"),
                      class: "text-subdued" },
                    "data-auto-submit-form-target": "auto" %>

              <%= ef.select :tag_ids,
                    Current.family.tags.alphabetically.pluck(:name, :id),
                    {
                      include_blank: t(".none"),
                      multiple: true,
                      label:    t(".tags_label"),
                      container_class: "h-40"
                    },
                  { "data-auto-submit-form-target": "auto" } %>
            <% end %>
          <% end %>

          <%= f.text_area :notes,
                  label: t(".note_label"),
                  placeholder: t(".note_placeholder"),
                  rows: 5,
                  "data-auto-submit-form-target": "auto" %>

        <% end %>
      </div>
    <% end %>

    <!-- Settings Section -->
    <%= disclosure t(".settings") do %>
      <div class="pb-4">

        <%= styled_form_with model: @entry,
              url: account_transaction_path(@entry),
              class: "p-3",
              data: { controller: "auto-submit-form" } do |f| %>
          <div class="flex cursor-pointer items-center gap-4 justify-between">
            <div class="text-sm space-y-1">
              <h4 class="text-primary">One-time <%= @entry.amount.negative? ? "Income" : "Expense" %></h4>
              <p class="text-secondary">One-time transactions will be excluded from certain budgeting calculations and reports to help you see what's really important.</p>
            </div>

            <div class="relative inline-block select-none">
              <%= f.check_box :excluded,
                      class: "sr-only peer",
                      "data-auto-submit-form-target": "auto" %>
              <label for="account_entry_excluded"
                         class="switch"></label>
            </div>
          </div>
        <% end %>

        <div class="flex items-center justify-between gap-4 p-3">
          <div class="text-sm space-y-1">
            <h4 class="text-primary">Transfer or Debt Payment?</h4>
            <p class="text-secondary">Transfers and payments are special types of transactions that indicate money movement between 2 accounts.</p>
          </div>

          <%= link_to new_account_transaction_transfer_match_path(@entry), class: "btn btn--outline flex items-center gap-2", data: { turbo_frame: :modal } do %>
            <%= lucide_icon "arrow-left-right", class: "w-4 h-4 shrink-0" %>
            <span class="whitespace-nowrap">Open matcher</span>
          <% end %>
        </div>

        <!-- Delete Transaction Form -->
        <div class="flex items-center justify-between gap-2 p-3">
          <div class="text-sm space-y-1">
            <h4 class="text-primary"><%= t(".delete_title") %></h4>
            <p class="text-secondary"><%= t(".delete_subtitle") %></p>
          </div>

          <%= button_to t(".delete"),
                  account_entry_path(@entry),
                  method: :delete,
                  class:  "rounded-lg px-3 py-2 text-red-500 text-sm
                           font-medium border border-secondary",
                  data:   { turbo_confirm: true, turbo_frame: "_top" } %>
        </div>
      </div>
    <% end %>
  </div>
<% end %>
