<%# locals: (account:, return_to: nil) %>

<% is_default = Current.user&.default_account_id == account.id %>

<%= turbo_frame_tag dom_id(account) do %>
  <div class="relative p-4 flex items-center justify-between gap-3 group/account hover:bg-surface-hover">
    <div class="flex items-center gap-3">
      <%= render "accounts/logo", account: account, size: "md" %>

      <div>
        <% if account.pending_deletion? %>
          <p class="text-sm font-medium text-primary">
            <span>
              <%= account.name %>
            </span>
            <span class="text-red-500 animate-pulse">
              (deletion in progress...)
            </span>
          </p>
        <% else %>
          <div class="flex items-center gap-1.5">
            <%= link_to account.name, account, class: [(account.active? ? "text-primary" : "text-subdued"), "text-sm font-medium hover:underline"], data: { turbo_frame: "_top" } %>

            <% if account.shared? %>
              <%= icon("users", class: "w-3.5 h-3.5 text-secondary", title: account.owned_by?(Current.user) ? nil : account.owner&.display_name) %>
            <% end %>

            <% if account.institution_name.present? %>
              <span class="hidden sm:inline text-secondary">• <%= account.institution_name %></span>
            <% end %>
          </div>
          <% if account.long_subtype_label %>
            <p class="text-sm text-secondary truncate"><%= account.long_subtype_label %></p>
          <% end %>
          <% if account.supports_default? && is_default %>
            <p class="text-xs text-secondary opacity-50"><%= t("accounts.account.default_label") %></p>
          <% end %>
          <% if account.institution_name.present? %>
            <p class="sm:hidden text-sm text-secondary truncate"><%= account.institution_name %></p>
          <% end %>
        <% end %>
      </div>
    </div>
    <div class="flex items-center gap-4">
      <% if account.draft? %>
        <!-- Balance hidden for draft accounts -->
      <% elsif account.syncing? %>
        <div class="w-16 h-6 bg-loader rounded-full animate-pulse"></div>
      <% else %>
        <p class="text-sm font-medium privacy-sensitive <%= account.active? ? "text-primary" : "text-subdued" %>">
          <%= format_money account.balance_money %>
        </p>
      <% end %>

      <% if account.draft? %>
        <%= render DS::Link.new(
          text: "Complete setup",
          href: edit_account_path(account, return_to: return_to),
          variant: :outline,
          frame: :modal
        ) %>
      <% elsif !account.pending_deletion? %>
        <% permission = account.permission_for(Current.user) %>
        <%= render DS::Menu.new(icon_vertical: true, mobile_fullwidth: false, max_width: "280px") do |menu| %>
          <% if permission.in?([ :owner, :full_control ]) %>
            <% menu.with_item(variant: "link", text: t("accounts.account.edit"), href: edit_account_path(account, return_to: return_to), icon: "pencil-line", data: { turbo_frame: :modal }) %>
          <% end %>
          <% menu.with_item(variant: "link", text: t("accounts.account.sharing"), href: account_sharing_path(account), icon: "users", data: { turbo_frame: :modal }) %>

          <% if Current.user&.admin? %>
            <% if !account.linked? && %w[Depository CreditCard Investment Crypto].include?(account.accountable_type) %>
              <% menu.with_item(variant: "link", text: t("accounts.account.link_provider"), href: select_provider_account_path(account), icon: "link", data: { turbo_frame: :modal }) %>
            <% elsif account.linked? %>
              <% menu.with_item(variant: "link", text: t("accounts.account.unlink_provider"), href: confirm_unlink_account_path(account), icon: "unlink", data: { turbo_frame: :modal }) %>
            <% end %>
          <% end %>

          <% if permission.in?([ :owner, :full_control ]) %>
            <% menu.with_item(variant: "divider") %>

            <% if account.active? %>
              <% menu.with_item(variant: "button", text: t("accounts.account.disable"), href: toggle_active_account_path(account), method: :patch, icon: "toggle-right", data: { turbo_frame: :_top }) %>
            <% elsif account.disabled? %>
              <% menu.with_item(variant: "button", text: t("accounts.account.enable"), href: toggle_active_account_path(account), method: :patch, icon: "toggle-left", data: { turbo_frame: :_top }) %>
            <% end %>
          <% end %>

          <% if is_default %>
            <% menu.with_item(variant: "button", text: t("accounts.account.remove_default"), href: remove_default_account_path(account), method: :patch, icon: "star-off", data: { turbo_frame: :_top }) %>
          <% elsif account.eligible_for_transaction_default? %>
            <% menu.with_item(variant: "button", text: t("accounts.account.set_default"), href: set_default_account_path(account), method: :patch, icon: "star", data: { turbo_frame: :_top }) %>
          <% end %>

          <% if account.owned_by?(Current.user) && !account.linked? %>
            <% menu.with_item(variant: "divider") %>
            <% menu.with_item(variant: "button", text: t("accounts.account.delete"), href: account_path(account), method: :delete, icon: "trash-2", confirm: CustomConfirm.for_resource_deletion("account", high_severity: true), data: { turbo_frame: :_top }) %>
          <% end %>
        <% end %>
      <% end %>
    </div>
  </div>
<% end %>
