Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby
99.2 KB
Raw Blame
require "test_helper"

class TradeRepublicAccountActivitiesProcessorTest < ActiveSupport::TestCase
  setup do
    @family = families(:dylan_family)
    @item = trade_republic_items(:configured_item)
    @item.trade_republic_accounts.destroy_all

    @tr_account = @item.trade_republic_accounts.create!(
      name: "Processor Test",
      trade_republic_account_id: "DEPROC1",
      currency: "EUR"
    )
    @account = @family.accounts.create!(
      name: "Trade Republic Processor Test",
      balance: 0,
      cash_balance: 0,
      currency: "EUR",
      accountable: Investment.new
    )
    @tr_account.ensure_account_provider!(@account)
    @tr_account.reload
  end

  test "buy is imported as a trade that spends cash, never as spending income" do
    import_event(order_execution_detail(quantity: "13.439945", isin: "US0378331005", amount: "2472.14"))

    trade = find_trade("trade_republic_event_evt_buy")
    assert_not_nil trade
    assert_equal BigDecimal("13.439945"), trade.entryable.qty
    assert_equal "Buy", trade.entryable.investment_activity_label
    assert_equal BigDecimal("2472.14"), trade.amount
  end

  test "crypto trades resolve to the same crypto security as crypto holdings" do
    Setting.stubs(:enabled_securities_providers).returns(%w[twelve_data binance_public])
    @tr_account.update!(raw_positions_payload: [
      { "isin" => "XF000BTC0017", "name" => "Bitcoin", "category" => "crypto_wallet", "quantity" => "0.000134", "price" => "72688.64" }
    ])
    TradeRepublicAccount::HoldingsProcessor.new(@tr_account.reload).process

    import_event(order_execution_detail(quantity: "0.000134", isin: "XF000BTC0017", amount: "11.00")
      .deep_merge(detail: { name: "Bitcoin", symbol: "BTCEUR", exchange_slug: "BHS" }))

    trade = find_trade("trade_republic_event_evt_buy").entryable
    assert_equal "CRYPTO:BTC", trade.security.ticker
    assert_equal @account.holdings.sole.security, trade.security
  end

  test "fractional quantity retains exact precision" do
    import_event(order_execution_detail(quantity: "13.439945", isin: "US0378331005", amount: "2472.14"))

    trade = find_trade("trade_republic_event_evt_buy")
    assert_equal BigDecimal("13.439945"), trade.entryable.qty
    assert_equal BigDecimal("13.439945").to_s, trade.entryable.qty.to_s
  end

  test "trade names use quantity and instrument without buy or sell wording" do
    Security.create!(ticker: "ISPA", exchange_operating_mic: "XETR", name: "Old security name")
    event = order_execution_detail(
      event_id: "evt_trade_name", quantity: "0.623752", isin: "IE00B3RBWM25", amount: "25.00"
    ).deep_merge(detail: {
      name: "STOXX Global Dividend 100 EUR (Dist)", symbol: "ISPA", exchange_slug: "XETR"
    })

    import_event(event)

    entry = find_trade("trade_republic_event_evt_trade_name")
    assert_equal "ISPA · 0.623752x STOXX Global Dividend 100 EUR (Dist)", entry.name

    import_event(event)

    assert_equal entry.id, find_trade("trade_republic_event_evt_trade_name").id
    assert_equal "ISPA · 0.623752x STOXX Global Dividend 100 EUR (Dist)", entry.reload.name
  end

  test "sell names fall back to the ticker without repeating it" do
    event = order_execution_detail(
      event_id: "evt_ticker_name", quantity: "-20.0", isin: "IE00B3RBWM25", amount: "80.00"
    ).deep_merge(detail: { name: nil })

    import_event(event)

    assert_equal "IE00B3RBWM25 · 20x", find_trade("trade_republic_event_evt_ticker_name").name
  end

  test "trade reuses exchange symbol fetched for its portfolio position" do
    Security.stubs(:search_provider).returns([])
    @tr_account.update!(raw_positions_payload: [
      {
        "isin" => "DE000BASF111",
        "name" => "BASF",
        "quantity" => "5",
        "price" => "45.12",
        "symbol" => "BAS",
        "exchange_slug" => "XETR"
      }
    ])

    import_event(order_execution_detail(
      event_id: "evt_symbol",
      quantity: "2",
      isin: "DE000BASF111",
      amount: "90.24"
    ))

    trade = find_trade("trade_republic_event_evt_symbol")
    assert_equal "BAS", trade.entryable.security.ticker
    assert_equal "XETR", trade.entryable.security.exchange_operating_mic
    assert_not trade.entryable.security.offline?
  end

  test "trade with symbol in detail resolves sold ISIN to exchange ticker" do
    Security.stubs(:search_provider).returns([])

    import_event(order_execution_detail(
      event_id: "evt_sold_symbol",
      quantity: "-10",
      isin: "NL0000303709",
      amount: "150.00"
    ).deep_merge(detail: { symbol: "ABN", exchange_slug: "XETR", name: "ABN AMRO" }))

    trade = find_trade("trade_republic_event_evt_sold_symbol")
    assert_equal "ABN", trade.entryable.security.ticker
    assert_equal "XETR", trade.entryable.security.exchange_operating_mic
    assert_not trade.entryable.security.offline?
    assert_not_includes trade.name, "NL0000303709"
    assert_includes trade.name, "ABN"
  end

  test "reprocessing rematches prior ISIN trades when detail gains an exchange symbol" do
    Security.stubs(:search_provider).returns([])

    import_event(order_execution_detail(
      event_id: "evt_rematch",
      quantity: "-3",
      isin: "DE000A0F5UJ7",
      amount: "90.00"
    ))

    first = find_trade("trade_republic_event_evt_rematch")
    assert_equal "DE000A0F5UJ7", first.entryable.security.ticker
    assert first.entryable.security.offline?
    assert_includes first.name, "DE000A0F5UJ7"

    import_event(order_execution_detail(
      event_id: "evt_rematch",
      quantity: "-3",
      isin: "DE000A0F5UJ7",
      amount: "90.00"
    ).deep_merge(detail: { symbol: "EXV1", exchange_slug: "XETR", name: "STOXX Banks" }))

    rematched = find_trade("trade_republic_event_evt_rematch")
    assert_equal "EXV1", rematched.entryable.security.ticker
    assert_equal "XETR", rematched.entryable.security.exchange_operating_mic
    assert_not rematched.entryable.security.offline?
    assert_not_includes rematched.name, "DE000A0F5UJ7"
    assert_includes rematched.name, "EXV1"
  end

  test "rematch moves ISIN trades even when split history left negative exchange holdings" do
    Security.stubs(:search_provider).returns([])
    event = order_execution_detail(event_id: "evt_split_history", quantity: "2", isin: "IE00B5BMR087", amount: "1024.92")

    import_event(event)
    isin_security = find_trade("trade_republic_event_evt_split_history").entryable.security
    exchange_security = Security.create!(ticker: "SXR8", exchange_operating_mic: "XETR", name: "Core S&P 500")
    date = Date.new(2024, 3, 28)
    @account.holdings.create!(security: isin_security, date: date, qty: 2, price: 511.96, amount: 1023.92, currency: "EUR")
    @account.holdings.create!(security: exchange_security, date: date, qty: 0, price: 511.96, amount: 0, currency: "EUR")
      .update_columns(qty: -0.5, amount: -255.98)

    import_event(event.deep_merge(detail: { symbol: "SXR8", exchange_slug: "XETR", name: "Core S&P 500" }))

    trade = find_trade("trade_republic_event_evt_split_history")
    assert_equal exchange_security.id, trade.entryable.security_id
    assert_includes trade.name, "SXR8"
    assert_not @account.holdings.where(security: isin_security).exists?
  end

  test "sell imports negative quantity and a cash inflow amount" do
    import_event(order_execution_detail(
      event_id: "evt_sell",
      quantity: "-2.500000",
      isin: "US0378331005",
      amount: "459.85"
    ))

    trade = find_trade("trade_republic_event_evt_sell")
    assert_not_nil trade
    assert_equal BigDecimal("-2.5"), trade.entryable.qty
    assert_equal "Sell", trade.entryable.investment_activity_label
    assert_equal BigDecimal("-459.85"), trade.amount
  end

  test "trade derives a missing amount from quantity and price" do
    import_event(order_execution_detail(
      event_id: "evt_price_only",
      quantity: "2.5",
      isin: "US0378331005",
      amount: nil
    ).deep_merge(detail: { price: "184.00" }))

    trade = find_trade("trade_republic_event_evt_price_only")
    assert_not_nil trade
    assert_equal BigDecimal("184.00"), trade.entryable.price
    assert_equal BigDecimal("460.00"), trade.amount
  end

  test "syncing the same events twice creates no duplicates" do
    events = [
      order_execution_detail(quantity: "1.5", isin: "US0378331005", amount: "100.00"),
      deposit_event
    ]

    @tr_account.update!(raw_timeline_payload: events)
    TradeRepublicAccount::ActivitiesProcessor.new(@tr_account).process

    assert_difference "Entry.where(source: 'trade_republic').count", 0 do
      TradeRepublicAccount::ActivitiesProcessor.new(@tr_account.reload).process
    end
  end

  test "deposit maps to Contribution with negative amount" do
    import_event(deposit_event)

    entry = Entry.find_by(external_id: "trade_republic_event_evt_dep")
    assert_not_nil entry
    assert_equal BigDecimal("-500.00"), entry.amount
    assert_equal "Contribution", entry.transaction.investment_activity_label
  end

  test "withdrawal maps to Withdrawal with positive amount" do
    import_event({
      id: "evt_wd",
      timestamp: "2026-08-01T10:00:00Z",
      category: "POC_CREATED",
      detail: { amount: "300.00", currency: "EUR" }
    })

    entry = Entry.find_by(external_id: "trade_republic_event_evt_wd")
    assert_not_nil entry
    assert_equal BigDecimal("300.00"), entry.amount
    assert_equal "Withdrawal", entry.transaction.investment_activity_label
  end

  test "dividend maps to Dividend with negative amount" do
    import_event({
      id: "evt_dividend",
      timestamp: "2026-08-01T10:00:00Z",
      category: "DIVIDEND",
      detail: { amount: "25.50", currency: "EUR" }
    })

    entry = Entry.find_by(external_id: "trade_republic_event_evt_dividend")
    assert_not_nil entry
    assert_equal BigDecimal("-25.50"), entry.amount
    assert_equal "Dividend", entry.transaction.investment_activity_label
  end

  test "order executions carry canonical activity labels under a non-English locale" do
    I18n.with_locale(:de) do
      import_event({
        id: "evt_buy_de",
        timestamp: "2026-08-01T10:00:00Z",
        category: "orderExecution",
        detail: { isin: "US0378331005", name: "Apple", quantity: "2", amount: "300.00", currency: "EUR" }
      })
    end

    entry = Entry.find_by(external_id: "trade_republic_event_evt_buy_de")
    assert_not_nil entry, "Trade::ACTIVITY_LABELS are canonical English, so order executions must import under any locale"
    assert_equal "Buy", entry.trade.investment_activity_label
  end

  test "cash movements store canonical activity labels under a non-English locale" do
    I18n.with_locale(:nl) do
      @tr_account.update!(raw_timeline_payload: [
        deposit_event,
        { id: "evt_wd_nl", timestamp: "2026-08-01T10:00:00Z", category: "POC_CREATED", detail: { amount: "300.00", currency: "EUR" } },
        { id: "evt_interest_nl", timestamp: "2026-08-01T10:00:00Z", category: "INTEREST_PAYOUT_CREATED", detail: { amount: "1.20", currency: "EUR" } },
        { id: "evt_fee_nl", timestamp: "2026-08-01T10:00:00Z", eventType: "CARD_ORDER_FEE", category: "POC_CREATED", detail: { amount: "5.00", currency: "EUR" } }
      ])
      TradeRepublicAccount::ActivitiesProcessor.new(@tr_account.reload).process
    end

    labels = %w[evt_dep evt_wd_nl evt_interest_nl evt_fee_nl].map do |id|
      Entry.find_by!(external_id: "trade_republic_event_#{id}").transaction.investment_activity_label
    end
    assert_equal %w[Contribution Withdrawal Interest Fee], labels
    assert_equal I18n.t("trade_republic_items.activities.labels.interest", locale: :nl),
                 Entry.find_by!(external_id: "trade_republic_event_evt_interest_nl").name
  end

  test "cash account card, deposit and withdrawal activity carries no activity label under any locale" do
    cash_account, cash_sure = create_linked_cash_account!
    I18n.with_locale(:nl) do
      cash_account.update!(raw_timeline_payload: [
        deposit_event.merge(id: "evt_cash_dep_nl"),
        { id: "evt_cash_wd_nl", timestamp: "2026-08-01T10:00:00Z", category: "POC_CREATED", detail: { amount: "300.00", currency: "EUR" } },
        { id: "evt_cash_card_nl", timestamp: "2026-08-01T10:00:00Z", eventType: "CARD_TRANSACTION", category: "POC_CREATED", detail: { amount: "42.00", currency: "EUR" } }
      ])
      TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process
    end

    %w[evt_cash_dep_nl evt_cash_wd_nl evt_cash_card_nl].each do |id|
      assert_nil cash_sure.entries.find_by!(external_id: "trade_republic_event_#{id}").transaction.investment_activity_label, id
    end
  end

  test "card events are named after the card activity but carry no activity label" do
    import_event({
      id: "evt_card_payment",
      timestamp: "2026-08-01T10:00:00Z",
      eventType: "CARD_TRANSACTION",
      category: "POC_CREATED",
      detail: { amount: "42.00", currency: "EUR" }
    })

    entry = Entry.find_by(external_id: "trade_republic_event_evt_card_payment")
    assert_equal "Card payment", entry.name
    assert_nil entry.transaction.investment_activity_label
  end

  test "a transfer's payment reference becomes the transaction note, not the status subtitle" do
    import_event({
      id: "evt_transfer_note",
      timestamp: "2026-08-01T10:00:00Z",
      eventType: "INCOMING_TRANSFER",
      category: "PAYMENT_RECEIVED",
      title: "Max Mustermann",
      subtitle: "Completed",
      detail: { amount: "50.00", currency: "EUR", note: "Invoice 2026-0001" }
    })

    entry = Entry.find_by(external_id: "trade_republic_event_evt_transfer_note")
    assert_equal "Invoice 2026-0001", entry.notes
    assert_equal "Completed", entry.transaction.extra.dig("trade_republic", "subtitle")
    assert_equal "Invoice 2026-0001", entry.transaction.extra.dig("trade_republic", "provider_detail", "note")
  end

  test "a transfer without a payment reference gets no note" do
    import_event({
      id: "evt_transfer_no_note",
      timestamp: "2026-08-01T10:00:00Z",
      eventType: "OUTGOING_TRANSFER",
      category: "POC_CREATED",
      title: "Erika Musterfrau",
      subtitle: "Sent",
      detail: { amount: "20.00", currency: "EUR", note_checked_at: "2026-08-02T10:00:00Z" }
    })

    entry = Entry.find_by(external_id: "trade_republic_event_evt_transfer_no_note")
    assert_nil entry.notes
    assert_nil entry.transaction.extra.dig("trade_republic", "provider_detail", "note_checked_at")
  end

  test "a payment reference replaces the provider's earlier note but not one the user edited" do
    event = {
      id: "evt_transfer_edit",
      timestamp: "2026-08-01T10:00:00Z",
      eventType: "INCOMING_TRANSFER",
      category: "PAYMENT_RECEIVED",
      title: "Max Mustermann",
      subtitle: "Completed",
      detail: { amount: "50.00", currency: "EUR" }
    }
    import_event(event)
    entry = Entry.find_by(external_id: "trade_republic_event_evt_transfer_edit")
    entry.update!(notes: "Completed")

    import_event(event.merge(detail: event[:detail].merge(note: "Invoice 2026-0001")))
    assert_equal "Invoice 2026-0001", entry.reload.notes

    entry.update!(notes: "my own note")
    entry.mark_user_modified!
    import_event(event.merge(detail: event[:detail].merge(note: "Invoice 2026-0002")))
    assert_equal "my own note", entry.reload.notes
  end

  test "a status note from an earlier sync is cleared when no payment reference exists" do
    event = {
      id: "evt_status_note",
      timestamp: "2026-08-01T10:00:00Z",
      eventType: "OUTGOING_TRANSFER",
      category: "POC_CREATED",
      title: "Erika Musterfrau",
      subtitle: "Sent",
      detail: { amount: "20.00", currency: "EUR" }
    }
    import_event(event)
    entry = Entry.find_by(external_id: "trade_republic_event_evt_status_note")
    entry.update!(notes: "Sent")

    import_event(event)
    assert_nil entry.reload.notes

    entry.update!(notes: "my own note")
    import_event(event)
    assert_equal "my own note", entry.reload.notes
  end

  test "a status note the user edited is kept when no payment reference exists" do
    event = {
      id: "evt_status_note_locked",
      timestamp: "2026-08-01T10:00:00Z",
      eventType: "OUTGOING_TRANSFER",
      category: "POC_CREATED",
      title: "Erika Musterfrau",
      subtitle: "Sent",
      detail: { amount: "20.00", currency: "EUR" }
    }
    import_event(event)
    entry = Entry.find_by(external_id: "trade_republic_event_evt_status_note_locked")
    entry.update!(notes: "Sent")
    entry.mark_user_modified!

    import_event(event)

    assert_equal "Sent", entry.reload.notes
  end

  test "a subtitle note on a non-transfer cash event is cleared, a user note on it is kept" do
    event = {
      id: "evt_card_status_note",
      timestamp: "2026-08-01T10:00:00Z",
      eventType: "CARD_TRANSACTION",
      category: "PAYMENT_RECEIVED",
      title: "Marktkauf",
      subtitle: "Card purchase",
      detail: { amount: "12.30", signed_amount: "-12.30", currency: "EUR" }
    }
    import_event(event)
    entry = Entry.find_by(external_id: "trade_republic_event_evt_card_status_note")
    entry.update!(notes: "Card purchase")

    import_event(event)
    assert_nil entry.reload.notes
    assert_equal "Card purchase", entry.transaction.extra.dig("trade_republic", "subtitle")

    entry.update!(notes: "bought for the party")
    import_event(event)
    assert_equal "bought for the party", entry.reload.notes
  end

  test "card cash back events that are purchases are imported as expenses with merchant details" do
    import_event({
      id: "evt_card_cash_back",
      timestamp: "2026-08-01T10:00:00Z",
      eventType: "CARD_CASH_BACK",
      category: "PAYMENT_RECEIVED",
      title: "Marktkauf",
      subtitle: "Card purchase",
      detail: { amount: "204.18", signed_amount: "-204.18", currency: "EUR" }
    })

    entry = Entry.find_by(external_id: "trade_republic_event_evt_card_cash_back")
    assert_equal BigDecimal("204.18"), entry.amount
    assert_equal "Marktkauf", entry.name
    assert_nil entry.transaction.investment_activity_label
    assert_equal "CARD_CASH_BACK", entry.transaction.extra.dig("trade_republic", "event_type")
    assert_equal "Card purchase", entry.transaction.extra.dig("trade_republic", "subtitle")
  end

  test "card funding transactions are imported as card payments" do
    import_event({
      id: "evt_card_aft",
      timestamp: "2026-02-21T11:13:04Z",
      eventType: "CARD_AFT",
      title: "Revolut",
      status: "EXECUTED",
      detail: { amount: -100.0, signed_amount: -100.0, currency: "EUR" }
    })

    entry = Entry.find_by!(external_id: "trade_republic_event_evt_card_aft")
    assert_equal BigDecimal("100.0"), entry.amount
    assert_equal "Revolut", entry.name
  end

  test "card credit transactions are imported as money in" do
    import_event({
      id: "evt_card_oct",
      timestamp: "2026-03-16T18:01:01Z",
      eventType: "CARD_OCT",
      title: "Refund Globalblue.com",
      status: "EXECUTED",
      detail: { amount: 2.96, signed_amount: 2.96, currency: "EUR" }
    })

    assert_equal BigDecimal("-2.96"), Entry.find_by!(external_id: "trade_republic_event_evt_card_oct").amount
  end

  test "credit card top-ups are imported as money in" do
    import_event({
      id: "evt_credit_card_cash_in",
      timestamp: "2024-12-11T10:00:00Z",
      eventType: "PAYMENT_INBOUND_CREDIT_CARD",
      title: "Cash In",
      status: "EXECUTED",
      detail: { amount: 500.0, signed_amount: 500.0, currency: "EUR" }
    })

    assert_equal BigDecimal("-500"), Entry.find_by!(external_id: "trade_republic_event_evt_credit_card_cash_in").amount
  end

  test "a stamp duty cancellation books nothing" do
    # Trade Republic turns the charge's timeline item into its cancellation.
    # The statement books the charge and the refund, which cancel out.
    @tr_account.update!(raw_timeline_payload: [
      stamp_duty_event(id: "evt_stamp_duty_cancel", timestamp: "2026-02-18T09:37:01Z", amount: -15.40, cancellation: true),
      stamp_duty_event(id: "evt_stamp_duty_new", timestamp: "2026-02-18T14:34:02Z", amount: -16.10)
    ])
    TradeRepublicAccount::ActivitiesProcessor.new(@tr_account.reload).process

    assert_nil Entry.find_by(external_id: "trade_republic_event_evt_stamp_duty_cancel")
    assert_equal BigDecimal("16.10"), Entry.find_by!(external_id: "trade_republic_event_evt_stamp_duty_new").amount
  end

  test "a stamp duty cancellation does not refund an earlier charge of the same amount" do
    # Recurring stamp duty often repeats its amount, so an earlier charge that
    # matches is not evidence of the one the cancellation replaced.
    @tr_account.update!(raw_timeline_payload: [
      stamp_duty_event(id: "evt_stamp_duty", timestamp: "2025-10-07T06:16:00Z", amount: -15.40),
      stamp_duty_event(id: "evt_stamp_duty_cancel", timestamp: "2026-02-18T09:37:01Z", amount: -15.40, cancellation: true, status: nil)
    ])
    TradeRepublicAccount::ActivitiesProcessor.new(@tr_account.reload).process

    assert_equal BigDecimal("15.40"), Entry.find_by!(external_id: "trade_republic_event_evt_stamp_duty").amount
    assert_nil Entry.find_by(external_id: "trade_republic_event_evt_stamp_duty_cancel")
  end

  test "removes a refund an earlier sync booked for a stamp duty cancellation" do
    Account::ProviderImportAdapter.new(@account).import_transaction(
      external_id: "trade_republic_event_evt_stamp_duty_cancel",
      amount: BigDecimal("-15.40"),
      currency: "EUR",
      date: Date.parse("2026-02-18"),
      name: "Stamp duty (Portfolio)",
      source: "trade_republic"
    )

    import_event(stamp_duty_event(id: "evt_stamp_duty_cancel", timestamp: "2026-02-18T09:37:01Z", amount: -15.40, cancellation: true))

    assert_nil Entry.find_by(external_id: "trade_republic_event_evt_stamp_duty_cancel")
  end

  test "a voided stamp duty charge is not imported as a refund" do
    import_event({
      id: "evt_stamp_duty_voided",
      timestamp: "2026-02-18T09:37:01Z",
      eventType: "STAMP_DUTY_TAX_PAID",
      title: "Stamp duty (Portfolio)",
      subtitle: "Cancelled",
      detail: { amount: -20.99, signed_amount: -20.99, currency: "EUR" }
    })

    assert_nil Entry.find_by(external_id: "trade_republic_event_evt_stamp_duty_voided")
  end

  test "removes a stamp duty charge whose item Trade Republic later voids" do
    import_event(stamp_duty_event(id: "evt_stamp_duty", timestamp: "2026-01-12T10:58:47Z", amount: -15.40))
    assert_equal BigDecimal("15.40"), Entry.find_by!(external_id: "trade_republic_event_evt_stamp_duty").amount

    voided = stamp_duty_event(id: "evt_stamp_duty", timestamp: "2026-01-12T10:58:47Z", amount: -15.40, status: nil)
    import_event(voided.merge(subtitle: "Cancelled"))

    assert_nil Entry.find_by(external_id: "trade_republic_event_evt_stamp_duty")
  end

  test "keeps a card payment flagged only by its subtitle" do
    import_event({
      id: "evt_card", timestamp: "2026-01-12T10:58:47Z", eventType: "CARD_TRANSACTION",
      title: "Cancelled Cafe", status: "EXECUTED", detail: { amount: -4.50, signed_amount: -4.50, currency: "EUR" }
    })
    import_event({
      id: "evt_card", timestamp: "2026-01-12T10:58:47Z", eventType: "CARD_TRANSACTION",
      title: "Cancelled Cafe", subtitle: "Cancelled", detail: { amount: -4.50, signed_amount: -4.50, currency: "EUR" }
    })

    assert Entry.exists?(external_id: "trade_republic_event_evt_card")
  end

  test "category direction wins over the provider signed amount" do
    import_event({
      id: "evt_incoming_signed",
      timestamp: "2026-08-01T10:00:00Z",
      category: "PAYMENT_RECEIVED",
      detail: { amount: "500.00", signed_amount: "500.00", currency: "EUR" }
    })

    assert_equal BigDecimal("-500.00"), Entry.find_by(external_id: "trade_republic_event_evt_incoming_signed").amount
  end

  test "unknown category is skipped without guessing a transaction" do
    assert_no_difference "Entry.count" do
      import_event({
        id: "evt_card",
        timestamp: "2026-08-01T10:00:00Z",
        category: "card_payment",
        detail: { amount: "42.00", currency: "EUR" }
      })
    end
  end

  test "unknown event does not prevent valid events from importing" do
    @tr_account.update!(raw_timeline_payload: [
      { id: "evt_unknown", timestamp: "2026-08-01T10:00:00Z", category: "future_event", detail: { amount: "42.00", currency: "EUR" } },
      deposit_event
    ])

    assert_difference "Entry.where(source: 'trade_republic').count", 1 do
      TradeRepublicAccount::ActivitiesProcessor.new(@tr_account).process
    end

    assert Entry.exists?(external_id: "trade_republic_event_evt_dep")
    assert_not Entry.exists?(external_id: "trade_republic_event_evt_unknown")
  end

  test "trade fee is imported on the trade and excluded from per-share price" do
    import_event(order_execution_detail(
      quantity: "2",
      isin: "IE00B5BMR087",
      amount: "1024.92"
    ).deep_merge(detail: { fees: "1.00", price: "511.96", name: "Core S&P 500" }))

    assert_equal 1, Entry.where(source: "trade_republic").count
    trade = find_trade("trade_republic_event_evt_buy")
    assert_equal BigDecimal("511.96"), trade.entryable.price
    assert_equal BigDecimal("1.00"), trade.entryable.fee
    assert_equal BigDecimal("1024.92"), trade.amount
    assert_equal "1.00", trade.entryable.extra.dig("trade_republic", "fees")
  end

  test "trade derives share price from amount net of fees when price is missing" do
    import_event(order_execution_detail(
      quantity: "2",
      isin: "IE00B5BMR087",
      amount: "1024.92"
    ).deep_merge(detail: { fees: "1.00" }))

    trade = find_trade("trade_republic_event_evt_buy")
    assert_equal BigDecimal("511.96"), trade.entryable.price
    assert_equal BigDecimal("1.00"), trade.entryable.fee
  end

  test "sell fallback amount subtracts fee from proceeds" do
    Security.stubs(:search_provider).returns([])

    import_event(order_execution_detail(
      event_id: "evt_sell_fee",
      quantity: "-2",
      isin: "IE00B5BMR087",
      amount: nil
    ).deep_merge(detail: { fees: "1.00", price: "511.96", name: "Core S&P 500" }))

    trade = find_trade("trade_republic_event_evt_sell_fee")
    assert_not_nil trade
    assert_equal BigDecimal("511.96"), trade.entryable.price
    assert_equal BigDecimal("1.00"), trade.entryable.fee
    assert_equal BigDecimal("-1022.92"), trade.amount
    assert_equal "Sell", trade.entryable.investment_activity_label
  end

  test "sell derives share price from proceeds plus fee when price is missing" do
    import_event(order_execution_detail(
      event_id: "evt_sell_derive",
      quantity: "-2",
      isin: "IE00B5BMR087",
      amount: "1022.92"
    ).deep_merge(detail: { fees: "1.00" }))

    trade = find_trade("trade_republic_event_evt_sell_derive")
    assert_equal BigDecimal("511.96"), trade.entryable.price
    assert_equal BigDecimal("1.00"), trade.entryable.fee
    assert_equal BigDecimal("-1022.92"), trade.amount
  end

  test "buy and sell fallbacks include taxes alongside fees" do
    Security.stubs(:search_provider).returns([])

    import_event(order_execution_detail(
      event_id: "evt_buy_tax",
      quantity: "2",
      isin: "IE00B5BMR087",
      amount: nil
    ).deep_merge(detail: { fees: "1.00", taxes: "0.50", price: "511.96", name: "Core S&P 500" }))

    buy = find_trade("trade_republic_event_evt_buy_tax")
    assert_equal BigDecimal("511.96"), buy.entryable.price
    assert_equal BigDecimal("1.00"), buy.entryable.fee
    assert_equal BigDecimal("1025.42"), buy.amount # 2*511.96 + 1 + 0.50

    import_event(order_execution_detail(
      event_id: "evt_sell_tax",
      quantity: "-2",
      isin: "IE00B5BMR087",
      amount: "1022.42"
    ).deep_merge(detail: { fees: "1.00", taxes: "0.50" }))

    sell = find_trade("trade_republic_event_evt_sell_tax")
    # Sell cash 1022.42 = gross - fee - tax → price (1022.42+1.50)/2
    assert_equal BigDecimal("511.96"), sell.entryable.price
    assert_equal BigDecimal("1.00"), sell.entryable.fee
    assert_equal BigDecimal("-1022.42"), sell.amount
  end

  test "reprocessing updates fee and share price on an existing trade" do
    import_event(order_execution_detail(
      event_id: "evt_fee_update",
      quantity: "2",
      isin: "IE00B5BMR087",
      amount: "1024.92"
    ))

    first = find_trade("trade_republic_event_evt_fee_update")
    assert_equal BigDecimal("512.46"), first.entryable.price
    assert_equal 0, first.entryable.fee.to_d

    import_event(order_execution_detail(
      event_id: "evt_fee_update",
      quantity: "2",
      isin: "IE00B5BMR087",
      amount: "1024.92"
    ).deep_merge(detail: { fees: "1.00", price: "511.96" }))

    updated = find_trade("trade_republic_event_evt_fee_update")
    assert_equal BigDecimal("511.96"), updated.entryable.price
    assert_equal BigDecimal("1.00"), updated.entryable.fee
  end

  test "event without normalized detail is skipped" do
    assert_no_difference "Entry.count" do
      import_event({
        id: "evt_nodetail",
        timestamp: "2026-08-01T10:00:00Z",
        category: "orderExecution"
      })
    end
  end

  test "split portfolio processing preserves legacy cash transactions when cash snapshot is empty" do
    @item.trade_republic_accounts.create!(
      name: "Cash",
      kind: "cash",
      trade_republic_account_id: "cash:DEPROC1",
      currency: "EUR",
      raw_timeline_payload: []
    )
    Account::ProviderImportAdapter.new(@account).import_transaction(
      external_id: "trade_republic_event_legacy_cash",
      amount: BigDecimal("-25.00"),
      currency: "EUR",
      date: Date.current,
      name: "Legacy card payment",
      source: "trade_republic"
    )

    TradeRepublicAccount::ActivitiesProcessor.new(@tr_account.reload).process

    assert Entry.exists?(external_id: "trade_republic_event_legacy_cash")
  end

  test "split portfolio processing preserves cash events when the cash account is unlinked" do
    @item.trade_republic_accounts.create!(
      name: "Cash",
      kind: "cash",
      trade_republic_account_id: "cash:DEPROC1",
      currency: "EUR",
      raw_timeline_payload: [ { "id" => "cash_evt" } ]
    )
    adapter = Account::ProviderImportAdapter.new(@account)
    adapter.import_transaction(
      external_id: "trade_republic_event_cash_evt",
      amount: BigDecimal("-25.00"),
      currency: "EUR",
      date: Date.current,
      name: "Cash payment",
      source: "trade_republic"
    )
    adapter.import_transaction(
      external_id: "trade_republic_event_unknown_evt",
      amount: BigDecimal("-10.00"),
      currency: "EUR",
      date: Date.current,
      name: "Unseen payment",
      source: "trade_republic"
    )

    TradeRepublicAccount::ActivitiesProcessor.new(@tr_account.reload).process

    assert Entry.exists?(external_id: "trade_republic_event_cash_evt")
    assert Entry.exists?(external_id: "trade_republic_event_unknown_evt")
  end

  test "saveback imports as a portfolio trade and never as a cash transaction when split" do
    cash_account, cash_sure = create_linked_cash_account!

    saveback = saveback_event
    @tr_account.update!(raw_timeline_payload: [ saveback ])
    cash_account.update!(raw_timeline_payload: [ saveback ])

    TradeRepublicAccount::ActivitiesProcessor.new(@tr_account.reload).process
    TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process

    trade = find_trade("trade_republic_event_evt_saveback")
    assert_not_nil trade
    assert_equal "Trade", trade.entryable_type
    assert_equal BigDecimal("0.09329"), trade.entryable.qty
    assert_equal "Buy", trade.entryable.investment_activity_label
    # Trade Republic pays for Saveback, so the portfolio's cash must not move;
    # the value stays in the price for the cost basis.
    assert_equal 0, trade.amount
    assert_in_delta BigDecimal("3.74"), trade.entryable.qty * trade.entryable.price, BigDecimal("0.01")
    assert_equal "SAVEBACK_AGGREGATE", trade.entryable.extra.dig("trade_republic", "event_type")

    assert_not Entry.exists?(account: cash_sure, external_id: "trade_republic_event_evt_saveback")
  end

  test "a stock bonus imports as a portfolio trade without a cash leg" do
    cash_account, cash_sure = create_linked_cash_account!

    bonus = stock_bonus_event
    @tr_account.update!(raw_timeline_payload: [ bonus ])
    cash_account.update!(raw_timeline_payload: [ bonus ])

    TradeRepublicAccount::ActivitiesProcessor.new(@tr_account.reload).process
    TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process

    trade = find_trade("trade_republic_event_evt_stock_bonus")
    assert_equal "Trade", trade.entryable_type
    assert_equal BigDecimal("0.055"), trade.entryable.qty
    # No cash moved, so the portfolio's cash must not move either; the value
    # stays in the price for the cost basis.
    assert_equal 0, trade.amount
    assert_in_delta BigDecimal("10.04"), trade.entryable.qty * trade.entryable.price, BigDecimal("0.01")
    assert_equal "ACQUISITION_TRADE_PERK", trade.entryable.extra.dig("trade_republic", "event_type")

    assert_not Entry.exists?(account: cash_sure, external_id: "trade_republic_event_evt_stock_bonus")
    assert_not @account.entries.exists?(external_id: "trade_republic_settlement_evt_stock_bonus")
  end

  test "a securities transfer in imports as a portfolio trade without a cash leg" do
    cash_account, cash_sure = create_linked_cash_account!

    transfer = securities_transfer_event(id: "evt_tr_in", event_type: "SSP_SECURITIES_TRANSFER_INCOMING", quantity: "10")
    @tr_account.update!(raw_timeline_payload: [ transfer ])
    cash_account.update!(raw_timeline_payload: [ transfer ])

    TradeRepublicAccount::ActivitiesProcessor.new(@tr_account.reload).process
    TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process

    trade = find_trade("trade_republic_event_evt_tr_in")
    assert_not_nil trade
    assert_equal "Trade", trade.entryable_type
    assert_equal BigDecimal("10"), trade.entryable.qty
    assert_equal "Transfer", trade.entryable.investment_activity_label
    # A transfer moves shares between brokers the user owns, so no cash
    # changes hands; the value stays in the price for the cost basis.
    assert_equal 0, trade.amount
    assert_in_delta BigDecimal("451.20"), trade.entryable.qty * trade.entryable.price, BigDecimal("0.01")
    assert_equal "SSP_SECURITIES_TRANSFER_INCOMING", trade.entryable.extra.dig("trade_republic", "event_type")

    assert_not Entry.exists?(account: cash_sure, external_id: "trade_republic_event_evt_tr_in")
  end

  test "a securities transfer out imports as a negative portfolio trade without a cash leg" do
    cash_account, cash_sure = create_linked_cash_account!

    transfer = securities_transfer_event(id: "evt_tr_out", event_type: "SSP_SECURITIES_TRANSFER_OUTGOING", quantity: "-10")
    @tr_account.update!(raw_timeline_payload: [ transfer ])
    cash_account.update!(raw_timeline_payload: [ transfer ])

    TradeRepublicAccount::ActivitiesProcessor.new(@tr_account.reload).process
    TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process

    trade = find_trade("trade_republic_event_evt_tr_out")
    assert_not_nil trade
    assert_equal "Trade", trade.entryable_type
    assert_equal BigDecimal("-10"), trade.entryable.qty
    # Labelled a transfer, not a sell, so it is not treated as a realized gain.
    assert_equal "Transfer", trade.entryable.investment_activity_label
    assert_equal 0, trade.amount
    assert_in_delta BigDecimal("451.20"), trade.entryable.qty.abs * trade.entryable.price, BigDecimal("0.01")
    assert_equal "SSP_SECURITIES_TRANSFER_OUTGOING", trade.entryable.extra.dig("trade_republic", "event_type")

    assert_not Entry.exists?(account: cash_sure, external_id: "trade_republic_event_evt_tr_out")
  end

  test "Private Markets bonus units import as a portfolio trade without a cash leg" do
    cash_account, cash_sure = create_linked_cash_account!
    @tr_account.update!(raw_timeline_payload: [ private_markets_event(id: "evt_pm_buy", subtitle: "Buy Order", quantity: "20.0", amount: "2001.0"), private_markets_event ])

    TradeRepublicAccount::ActivitiesProcessor.new(@tr_account.reload).process
    TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process

    bonus = find_trade("trade_republic_event_evt_pm_bonus")
    assert_equal BigDecimal("0.2"), bonus.entryable.qty
    # The statement credits the bonus and buys the units with it; the
    # value stays in the price for the cost basis.
    assert_equal 0, bonus.amount
    assert_in_delta BigDecimal("20.0"), bonus.entryable.qty * bonus.entryable.price, BigDecimal("0.01")
    assert_not Entry.exists?(account: cash_sure, external_id: "trade_republic_event_evt_pm_bonus")
    assert_not @account.entries.exists?(external_id: "trade_republic_settlement_evt_pm_bonus")

    # The order the bonus belongs to still settles against cash.
    assert_equal BigDecimal("2001.0"), cash_sure.entries.find_by!(external_id: "trade_republic_event_evt_pm_buy").amount
    assert @account.entries.exists?(external_id: "trade_republic_settlement_evt_pm_buy")
  end

  test "removes the cash leg an earlier sync booked for Private Markets bonus units" do
    cash_account, cash_sure = create_linked_cash_account!
    # Earlier syncs settled the bonus like the order it belongs to.
    @tr_account.update!(raw_timeline_payload: [ private_markets_event(subtitle: "Buy Order") ])
    TradeRepublicAccount::ActivitiesProcessor.new(@tr_account.reload).process
    TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process
    assert cash_sure.entries.exists?(external_id: "trade_republic_event_evt_pm_bonus")
    assert @account.entries.exists?(external_id: "trade_republic_settlement_evt_pm_bonus")

    @tr_account.update!(raw_timeline_payload: [ private_markets_event ])
    # Until the portfolio pass rebooks the trade without cash (it runs first,
    # but can fail), the cash leg stays so the portfolio's cash nets to zero.
    TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process
    assert cash_sure.entries.exists?(external_id: "trade_republic_event_evt_pm_bonus")
    assert @account.entries.exists?(external_id: "trade_republic_settlement_evt_pm_bonus")

    TradeRepublicAccount::ActivitiesProcessor.new(@tr_account.reload).process
    TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process

    assert_equal 0, find_trade("trade_republic_event_evt_pm_bonus").amount
    assert_not cash_sure.entries.exists?(external_id: "trade_republic_event_evt_pm_bonus")
    assert_not @account.entries.exists?(external_id: "trade_republic_settlement_evt_pm_bonus")
  end

  test "round up imports as a portfolio trade and a cash outflow when split" do
    cash_account, cash_sure = create_linked_cash_account!

    round_up = round_up_event
    @tr_account.update!(raw_timeline_payload: [ round_up ])
    cash_account.update!(raw_timeline_payload: [ round_up ])

    TradeRepublicAccount::ActivitiesProcessor.new(@tr_account.reload).process
    TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process

    trade = find_trade("trade_republic_event_evt_round_up")
    assert_not_nil trade
    assert_equal "Trade", trade.entryable_type
    assert_equal BigDecimal("0.009977"), trade.entryable.qty
    assert_equal BigDecimal("0.40"), trade.amount
    assert_equal "SPARE_CHANGE_AGGREGATE", trade.entryable.extra.dig("trade_republic", "event_type")

    cash_entry = Entry.find_by(account: cash_sure, external_id: "trade_republic_event_evt_round_up")
    assert_not_nil cash_entry
    assert_equal "Transaction", cash_entry.entryable_type
    assert_equal BigDecimal("0.40"), cash_entry.amount
    assert_equal "Buy", cash_entry.transaction.investment_activity_label
    assert_equal "investment_contribution", cash_entry.transaction.kind
    assert_nil cash_entry.transaction.category_id
    assert_equal "SPARE_CHANGE_AGGREGATE", cash_entry.transaction.extra.dig("trade_republic", "event_type")

    counterpart = @account.entries.find_by!(external_id: "trade_republic_settlement_evt_round_up")
    assert_equal BigDecimal("-0.40"), counterpart.amount
    assert_equal counterpart.transaction, cash_entry.transaction.transfer.inflow_transaction
  end

  test "round up and saveback remain idempotent across repeated processing" do
    cash_account, cash_sure = create_linked_cash_account!
    events = [ saveback_event, round_up_event ]

    @tr_account.update!(raw_timeline_payload: events)
    cash_account.update!(raw_timeline_payload: events)

    TradeRepublicAccount::ActivitiesProcessor.new(@tr_account.reload).process
    TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process

    assert_difference -> { @account.entries.where(source: "trade_republic").count }, 0 do
      assert_difference -> { cash_sure.entries.where(source: "trade_republic").count }, 0 do
        TradeRepublicAccount::ActivitiesProcessor.new(@tr_account.reload).process
        TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process
      end
    end

    assert_equal 2, @account.entries.where(entryable_type: "Trade", source: "trade_republic").count
    assert_equal 1, cash_sure.entries.where(entryable_type: "Transaction", source: "trade_republic").count
  end

  test "incomplete saveback details are skipped on portfolio and never become cash" do
    cash_account, cash_sure = create_linked_cash_account!
    incomplete = saveback_event.deep_merge(detail: { isin: nil, quantity: nil })

    @tr_account.update!(raw_timeline_payload: [ incomplete ])
    cash_account.update!(raw_timeline_payload: [ incomplete ])

    assert_no_difference "Entry.where(source: 'trade_republic').count" do
      TradeRepublicAccount::ActivitiesProcessor.new(@tr_account.reload).process
      TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process
    end

    assert_not Entry.exists?(account: @account, external_id: "trade_republic_event_evt_saveback")
    assert_not Entry.exists?(account: cash_sure, external_id: "trade_republic_event_evt_saveback")
  end

  test "incomplete round up details still import cash but skip the portfolio trade" do
    cash_account, cash_sure = create_linked_cash_account!
    incomplete = round_up_event.deep_merge(detail: { isin: nil, quantity: nil })

    @tr_account.update!(raw_timeline_payload: [ incomplete ])
    cash_account.update!(raw_timeline_payload: [ incomplete ])

    TradeRepublicAccount::ActivitiesProcessor.new(@tr_account.reload).process
    TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process

    assert_not Entry.exists?(account: @account, external_id: "trade_republic_event_evt_round_up")

    cash_entry = Entry.find_by(account: cash_sure, external_id: "trade_republic_event_evt_round_up")
    assert_not_nil cash_entry
    assert_equal BigDecimal("0.40"), cash_entry.amount
    assert_equal "Buy", cash_entry.transaction.investment_activity_label
  end

  test "logs order executions whose detail has no ISIN or quantity yet" do
    # Shaped like the stored 2025 executions in #4040: only the list amount.
    savings_plan = {
      id: "evt_old_plan", timestamp: "2025-07-02T08:00:00Z", eventType: "TRADING_SAVINGSPLAN_EXECUTED",
      category: "orderExecution", detail: { amount: "25.00", signed_amount: -25.0, currency: "EUR" }
    }
    trade = {
      id: "evt_old_trade", timestamp: "2025-06-13T06:36:46Z", eventType: "TRADING_TRADE_EXECUTED",
      category: "orderExecution", detail: { amount: "1001.00", signed_amount: -1001.0, currency: "EUR" }
    }
    complete = order_execution_detail(event_id: "evt_complete", quantity: "2.0", isin: "US0378331005", amount: "460.00")
    saveback = saveback_event.deep_merge(detail: { quantity: nil })
    @tr_account.update!(raw_timeline_payload: [ savings_plan, trade, complete, saveback ])

    assert_difference -> { incomplete_execution_logs.count }, 1 do
      TradeRepublicAccount::ActivitiesProcessor.new(@tr_account.reload).process
    end

    log = incomplete_execution_logs.order(:created_at).last
    assert_equal "warn", log.level
    assert_equal "3 Trade Republic order executions not imported yet: the detail has no ISIN or quantity", log.message
    assert_equal 3, log.metadata["incomplete_count"]
    executions = log.metadata["executions"].index_by { |execution| execution["event_id"] }
    assert_equal(
      { "event_id" => "evt_old_plan", "event_type" => "TRADING_SAVINGSPLAN_EXECUTED", "date" => "2025-07-02", "missing" => [ "isin", "quantity" ] },
      executions["evt_old_plan"]
    )
    assert_equal "TRADING_TRADE_EXECUTED", executions["evt_old_trade"]["event_type"]
    assert_equal [ "quantity" ], executions["evt_saveback"]["missing"]
    assert_not_nil find_trade("trade_republic_event_evt_complete")
  end

  test "logs nothing when every order execution has its detail" do
    assert_no_difference -> { incomplete_execution_logs.count } do
      import_event(order_execution_detail(event_id: "evt_complete", quantity: "2.0", isin: "US0378331005", amount: "460.00"))
    end
  end

  test "portfolio-only saveback and round up import as trades without cash entries" do
    @tr_account.update!(raw_timeline_payload: [ saveback_event, round_up_event ])

    TradeRepublicAccount::ActivitiesProcessor.new(@tr_account.reload).process

    assert_equal 2, @account.entries.where(entryable_type: "Trade", source: "trade_republic").count
    assert_equal 0, @account.entries.where(entryable_type: "Transaction", source: "trade_republic").count
  end

  test "cash-only round up imports as cash while saveback is skipped" do
    @tr_account.destroy
    cash_provider = @item.trade_republic_accounts.create!(
      name: "Cash only",
      kind: "cash",
      trade_republic_account_id: "cash:DEPROC1",
      currency: "EUR",
      raw_timeline_payload: [ saveback_event, round_up_event ]
    )
    cash_sure = @family.accounts.create!(
      name: "Trade Republic Cash Only",
      balance: 0,
      cash_balance: 0,
      currency: "EUR",
      accountable: Depository.new
    )
    cash_provider.ensure_account_provider!(cash_sure)

    TradeRepublicAccount::ActivitiesProcessor.new(cash_provider.reload).process

    assert_not Entry.exists?(account: cash_sure, external_id: "trade_republic_event_evt_saveback")
    cash_entry = Entry.find_by(account: cash_sure, external_id: "trade_republic_event_evt_round_up")
    assert_not_nil cash_entry
    assert_equal BigDecimal("0.40"), cash_entry.amount
  end

  test "reconciles unprotected legacy saveback cash transactions" do
    cash_account, cash_sure = create_linked_cash_account!
    saveback = saveback_event

    Account::ProviderImportAdapter.new(cash_sure).import_transaction(
      external_id: "trade_republic_event_evt_saveback",
      amount: BigDecimal("3.74"),
      currency: "EUR",
      date: Date.parse("2026-09-02"),
      name: "STOXX Global Dividend 100 EUR (Dist)",
      source: "trade_republic",
      investment_activity_label: "Withdrawal",
      extra: {
        trade_republic: {
          event_id: "trade_republic_event_evt_saveback",
          event_type: "SAVEBACK_AGGREGATE",
          title: "STOXX Global Dividend 100 EUR (Dist)",
          subtitle: "Saveback"
        }
      }
    )

    cash_account.update!(raw_timeline_payload: [ saveback ])
    @tr_account.update!(raw_timeline_payload: [ saveback ])

    TradeRepublicAccount::ActivitiesProcessor.new(@tr_account.reload).process
    TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process

    assert Entry.exists?(account: @account, external_id: "trade_republic_event_evt_saveback", entryable_type: "Trade")
    assert_not Entry.exists?(account: cash_sure, external_id: "trade_republic_event_evt_saveback")
  end

  test "keeps legacy saveback cash until the portfolio trade is imported" do
    cash_account, cash_sure = create_linked_cash_account!
    incomplete = saveback_event.deep_merge(detail: { isin: nil, quantity: nil })

    Account::ProviderImportAdapter.new(cash_sure).import_transaction(
      external_id: "trade_republic_event_evt_saveback",
      amount: BigDecimal("3.74"),
      currency: "EUR",
      date: Date.parse("2026-09-02"),
      name: "STOXX Global Dividend 100 EUR (Dist)",
      source: "trade_republic",
      investment_activity_label: "Withdrawal",
      extra: {
        trade_republic: {
          event_id: "trade_republic_event_evt_saveback",
          event_type: "SAVEBACK_AGGREGATE",
          title: "STOXX Global Dividend 100 EUR (Dist)",
          subtitle: "Saveback"
        }
      }
    )

    cash_account.update!(raw_timeline_payload: [ incomplete ])
    @tr_account.update!(raw_timeline_payload: [ incomplete ])

    TradeRepublicAccount::ActivitiesProcessor.new(@tr_account.reload).process
    TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process

    assert_not Entry.exists?(account: @account, external_id: "trade_republic_event_evt_saveback")
    assert Entry.exists?(account: cash_sure, external_id: "trade_republic_event_evt_saveback", entryable_type: "Transaction")
  end

  test "preserves protected legacy saveback cash transactions during reconciliation" do
    cash_account, cash_sure = create_linked_cash_account!
    saveback = saveback_event

    entry = Account::ProviderImportAdapter.new(cash_sure).import_transaction(
      external_id: "trade_republic_event_evt_saveback",
      amount: BigDecimal("3.74"),
      currency: "EUR",
      date: Date.parse("2026-09-02"),
      name: "STOXX Global Dividend 100 EUR (Dist)",
      source: "trade_republic",
      investment_activity_label: "Withdrawal",
      extra: {
        trade_republic: {
          event_id: "trade_republic_event_evt_saveback",
          event_type: "SAVEBACK_AGGREGATE"
        }
      }
    )
    entry.mark_user_modified!

    # Portfolio trade is already present so the missing-trade guard would not
    # keep this cash row — protection alone must preserve it.
    cash_account.update!(raw_timeline_payload: [ saveback ])
    @tr_account.update!(raw_timeline_payload: [ saveback ])
    TradeRepublicAccount::ActivitiesProcessor.new(@tr_account.reload).process
    assert Entry.exists?(account: @account, external_id: "trade_republic_event_evt_saveback", entryable_type: "Trade")

    TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process

    assert Entry.exists?(account: cash_sure, external_id: "trade_republic_event_evt_saveback")
    assert entry.reload.user_modified?
  end

  test "a linked Crypto account takes crypto trades and their settlements from the portfolio" do
    cash_account, cash_sure = create_linked_cash_account!
    stock = order_execution_detail(event_id: "evt_stock", quantity: "1", isin: "US0378331005", amount: "150.00")
      .deep_merge(detail: { signed_amount: -150.0 })
    bitcoin = order_execution_detail(event_id: "evt_btc", quantity: "0.000134", isin: "XF000BTC0017", amount: "11.00")
      .deep_merge(title: "Bitcoin", detail: { name: "Bitcoin", signed_amount: -11.0 })
    @tr_account.update!(raw_timeline_payload: [ stock, bitcoin ])
    process_all(@tr_account, cash_account)
    assert @account.entries.exists?(external_id: "trade_republic_settlement_evt_btc")

    crypto_account, crypto_sure = create_linked_crypto_account!
    process_all(@tr_account, crypto_account, cash_account)

    assert_equal %w[trade_republic_event_evt_stock trade_republic_settlement_evt_stock],
      @account.entries.order(:external_id).pluck(:external_id)
    assert_equal 0, @account.entries.sum(:amount)

    crypto_trade = crypto_sure.entries.find_by!(external_id: "trade_republic_event_evt_btc")
    assert_equal BigDecimal("11.0"), crypto_trade.amount
    counterpart = crypto_sure.entries.find_by!(external_id: "trade_republic_settlement_evt_btc")
    settlement = cash_sure.entries.find_by!(external_id: "trade_republic_event_evt_btc")
    assert_equal counterpart.transaction, settlement.transaction.transfer.inflow_transaction
    assert_equal 0, crypto_sure.entries.sum(:amount)

    assert_no_difference [ -> { crypto_sure.entries.count }, -> { @account.entries.count }, -> { Transfer.count } ] do
      process_all(@tr_account, crypto_account, cash_account)
    end
  end

  test "an edited crypto trade stays on the portfolio when the Crypto account is linked" do
    bitcoin = order_execution_detail(event_id: "evt_btc", quantity: "0.000134", isin: "XF000BTC0017", amount: "11.00")
    import_event(bitcoin)
    edited = find_trade("trade_republic_event_evt_btc")
    edited.update!(user_modified: true)

    crypto_account, crypto_sure = create_linked_crypto_account!
    process_all(@tr_account, crypto_account)

    assert @account.entries.exists?(edited.id)
    assert_not crypto_sure.entries.exists?
  end

  test "a crypto trade with an edited settlement counterpart stays on the portfolio" do
    cash_account, _cash_sure = create_linked_cash_account!
    bitcoin = order_execution_detail(event_id: "evt_btc", quantity: "0.000134", isin: "XF000BTC0017", amount: "11.00")
      .deep_merge(detail: { signed_amount: -11.0 })
    @tr_account.update!(raw_timeline_payload: [ bitcoin ])
    process_all(@tr_account, cash_account)
    @account.entries.find_by!(external_id: "trade_republic_settlement_evt_btc").update!(user_modified: true)

    crypto_account, crypto_sure = create_linked_crypto_account!
    process_all(@tr_account, crypto_account, cash_account)

    assert find_trade("trade_republic_event_evt_btc")
    assert_not crypto_sure.entries.exists?
  end

  test "trades of a position listed under crypto go to the Crypto account whatever its ISIN" do
    @tr_account.update!(raw_positions_payload: [
      { "isin" => "XS0000000001", "name" => "Some Coin", "category" => "crypto_wallet", "quantity" => "1", "price" => "10" }
    ])
    coin = order_execution_detail(event_id: "evt_coin", quantity: "1", isin: "XS0000000001", amount: "10.00")
    @tr_account.update!(raw_timeline_payload: [ coin ])

    crypto_account, crypto_sure = create_linked_crypto_account!
    process_all(@tr_account, crypto_account)

    assert_not find_trade("trade_republic_event_evt_coin")
    assert crypto_sure.entries.exists?(external_id: "trade_republic_event_evt_coin")
  end

  test "trades of a closed crypto position stay on the Crypto account" do
    crypto_position = { "isin" => "XS0000000001", "name" => "Some Coin", "category" => "crypto_wallet", "quantity" => "1", "price" => "10" }
    coin = order_execution_detail(event_id: "evt_coin", quantity: "1", isin: "XS0000000001", amount: "10.00")
    @tr_account.update!(raw_positions_payload: [ crypto_position ], raw_timeline_payload: [ coin ])
    crypto_account, crypto_sure = create_linked_crypto_account!
    process_all(@tr_account, crypto_account)

    @tr_account.update!(raw_positions_payload: [])
    process_all(@tr_account)
    assert_not find_trade("trade_republic_event_evt_coin")

    process_all(crypto_account)
    assert crypto_sure.entries.exists?(external_id: "trade_republic_event_evt_coin")
  end

  test "crypto trades stay on the portfolio while the Crypto account is unlinked" do
    @item.trade_republic_accounts.create!(name: "Crypto", kind: "crypto", trade_republic_account_id: "crypto:DEPROC1", currency: "EUR")

    import_event(order_execution_detail(event_id: "evt_btc", quantity: "0.000134", isin: "XF000BTC0017", amount: "11.00"))

    assert find_trade("trade_republic_event_evt_btc")
  end

  test "a sale's cash leg uses the detail total when the timeline amount differs" do
    cash_account, cash_sure = create_linked_cash_account!
    # A sale with withheld tax (#4057): the timeline list says 81.33, while the
    # detail total and the account statement say 80.87.
    sell = net_sale_event(detail_amount: "80.87")
    @tr_account.update!(raw_timeline_payload: [ sell ])

    assert_difference -> { DebugLogEntry.where(message: SETTLEMENT_MISMATCH_MESSAGE).count }, 1 do
      TradeRepublicAccount::ActivitiesProcessor.new(@tr_account.reload).process
      TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process
    end

    assert_equal BigDecimal("-80.87"), find_trade("trade_republic_event_evt_net_sell").amount
    assert_equal BigDecimal("-80.87"), cash_sure.entries.find_by!(external_id: "trade_republic_event_evt_net_sell").amount
    assert_equal BigDecimal("80.87"), @account.entries.find_by!(external_id: "trade_republic_settlement_evt_net_sell").amount
    assert_equal 0, @account.entries.sum(:amount)

    log = DebugLogEntry.where(message: SETTLEMENT_MISMATCH_MESSAGE).order(:created_at).last
    assert_equal "81.33", log.metadata["timeline_amount"]
    assert_equal "80.87", log.metadata["detail_amount"]

    assert_no_difference -> { DebugLogEntry.where(message: SETTLEMENT_MISMATCH_MESSAGE).count } do
      TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process
    end
  end

  test "a sale booked before its detail arrived is corrected to the detail total" do
    cash_account, cash_sure = create_linked_cash_account!
    # Without the detail, the amount is the timeline list amount.
    @tr_account.update!(raw_timeline_payload: [ net_sale_event(detail_amount: "81.33") ])

    assert_no_difference -> { DebugLogEntry.where(message: SETTLEMENT_MISMATCH_MESSAGE).count } do
      TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process
    end
    assert_equal BigDecimal("-81.33"), cash_sure.entries.find_by!(external_id: "trade_republic_event_evt_net_sell").amount

    @tr_account.update!(raw_timeline_payload: [ net_sale_event(detail_amount: "80.87") ])
    TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process

    assert_equal BigDecimal("-80.87"), cash_sure.entries.find_by!(external_id: "trade_republic_event_evt_net_sell").amount
    assert_equal 1, cash_sure.entries.where(external_id: "trade_republic_event_evt_net_sell").count
  end

  test "split accounts settle buys and sells against the cash account" do
    cash_account, cash_sure = create_linked_cash_account!
    buy = order_execution_detail(event_id: "evt_settle_buy", quantity: "2.0", isin: "IE00B5BMR087", amount: "1024.92")
      .deep_merge(title: "Core S&P 500 USD (Acc)", subtitle: "Buy Order", detail: { signed_amount: -1024.92, fees: "1.0", price: "511.96" })
    sell = order_execution_detail(event_id: "evt_settle_sell", quantity: "-1.0", isin: "IE00B5BMR087", amount: "529.00")
      .deep_merge(title: "Core S&P 500 USD (Acc)", subtitle: "Sell Order", detail: { signed_amount: 529.0, fees: "1.0" })
    @tr_account.update!(raw_timeline_payload: [ buy, sell ])

    TradeRepublicAccount::ActivitiesProcessor.new(@tr_account.reload).process
    TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process

    assert_equal BigDecimal("1024.92"), find_trade("trade_republic_event_evt_settle_buy").amount
    assert_equal 2, @account.entries.where(entryable_type: "Transaction").count

    buy_cash = cash_sure.entries.find_by!(external_id: "trade_republic_event_evt_settle_buy")
    assert_equal BigDecimal("1024.92"), buy_cash.amount
    assert_equal "Core S&P 500 USD (Acc)", buy_cash.name
    assert_equal "Buy", buy_cash.transaction.investment_activity_label
    assert_equal "investment_contribution", buy_cash.transaction.kind
    assert_nil buy_cash.transaction.category_id

    sell_cash = cash_sure.entries.find_by!(external_id: "trade_republic_event_evt_settle_sell")
    assert_equal BigDecimal("-529.0"), sell_cash.amount
    assert_equal "Sell", sell_cash.transaction.investment_activity_label
    assert_equal "funds_movement", sell_cash.transaction.kind
    assert_nil sell_cash.transaction.category_id

    buy_counterpart = @account.entries.find_by!(external_id: "trade_republic_settlement_evt_settle_buy")
    assert_equal BigDecimal("-1024.92"), buy_counterpart.amount
    assert_equal buy_cash.date, buy_counterpart.date
    assert_equal "Transfer from #{cash_sure.name}", buy_counterpart.name
    assert_equal "funds_movement", buy_counterpart.transaction.kind
    assert_equal "Transfer", buy_counterpart.transaction.investment_activity_label
    buy_transfer = buy_cash.transaction.transfer
    assert buy_transfer.confirmed?
    assert_equal buy_counterpart.transaction, buy_transfer.inflow_transaction
    assert_equal buy_cash.transaction, buy_transfer.outflow_transaction

    sell_counterpart = @account.entries.find_by!(external_id: "trade_republic_settlement_evt_settle_sell")
    assert_equal BigDecimal("529.0"), sell_counterpart.amount
    assert_equal "Transfer to #{cash_sure.name}", sell_counterpart.name
    sell_transfer = sell_cash.transaction.transfer
    assert_equal sell_cash.transaction, sell_transfer.inflow_transaction
    assert_equal sell_counterpart.transaction, sell_transfer.outflow_transaction

    # Each trade's cash impact is offset by its counterpart, so the
    # portfolio's own cash stays at zero.
    assert_equal 0, @account.entries.sum(:amount)

    assert_no_difference [ -> { cash_sure.entries.count }, -> { @account.entries.count }, -> { Transfer.count } ] do
      TradeRepublicAccount::ActivitiesProcessor.new(@tr_account.reload).process
      TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process
    end
    assert_equal buy_transfer, buy_cash.transaction.reload.transfer
  end

  test "settlement counterpart is removed with its trade and never relinked after the user unlinks it" do
    cash_account, cash_sure = create_linked_cash_account!
    buy = order_execution_detail(event_id: "evt_counterpart", quantity: "2.0", isin: "IE00B5BMR087", amount: "1024.92")
    @tr_account.update!(raw_timeline_payload: [ buy ])

    TradeRepublicAccount::ActivitiesProcessor.new(@tr_account.reload).process
    TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process
    cash_entry = cash_sure.entries.find_by!(external_id: "trade_republic_event_evt_counterpart")
    counterpart = @account.entries.find_by!(external_id: "trade_republic_settlement_evt_counterpart")

    cash_entry.transaction.transfer.reject!
    TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process
    assert_nil cash_entry.transaction.reload.transfer
    assert_nil counterpart.transaction.reload.transfer

    @tr_account.update!(raw_timeline_payload: [ buy.merge(deleted: true) ])
    TradeRepublicAccount::ActivitiesProcessor.new(@tr_account.reload).process
    TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process

    assert_not @account.entries.exists?(external_id: "trade_republic_settlement_evt_counterpart")
    assert_not cash_sure.entries.exists?(external_id: "trade_republic_event_evt_counterpart")
  end

  test "cash settlement without a portfolio trade books no counterpart" do
    cash_account, cash_sure = create_linked_cash_account!
    buy = order_execution_detail(event_id: "evt_no_trade", quantity: "2.0", isin: "IE00B5BMR087", amount: "1024.92")
    @tr_account.update!(raw_timeline_payload: [ buy ])

    TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process

    cash_entry = cash_sure.entries.find_by!(external_id: "trade_republic_event_evt_no_trade")
    assert_nil cash_entry.transaction.transfer
    assert_not @account.entries.exists?(external_id: "trade_republic_settlement_evt_no_trade")
  end

  test "cash settlement in another currency than the portfolio books no counterpart" do
    cash_account, cash_sure = create_linked_cash_account!
    buy = order_execution_detail(event_id: "evt_other_currency", quantity: "2.0", isin: "IE00B5BMR087", amount: "1024.92")
    @tr_account.update!(raw_timeline_payload: [ buy ])
    TradeRepublicAccount::ActivitiesProcessor.new(@tr_account.reload).process
    @account.update!(currency: "USD")

    assert_difference -> { DebugLogEntry.where(level: "warn", account: @account).count }, 1 do
      TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process
    end

    cash_entry = cash_sure.entries.find_by!(external_id: "trade_republic_event_evt_other_currency")
    assert_nil cash_entry.transaction.transfer
    assert_not @account.entries.exists?(external_id: "trade_republic_settlement_evt_other_currency")
  end

  test "cash settlement is skipped when the trade direction is unknown" do
    cash_account, cash_sure = create_linked_cash_account!
    event = order_execution_detail(event_id: "evt_settle_unknown", quantity: nil, isin: "IE00B5BMR087", amount: "1024.92")
    @tr_account.update!(raw_timeline_payload: [ event ])

    TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process

    assert_not cash_sure.entries.exists?(external_id: "trade_republic_event_evt_settle_unknown")
  end

  test "cash settlement is removed when Trade Republic deletes the trade" do
    cash_account, cash_sure = create_linked_cash_account!
    buy = order_execution_detail(event_id: "evt_settle_deleted", quantity: "2.0", isin: "IE00B5BMR087", amount: "1024.92")
    @tr_account.update!(raw_timeline_payload: [ buy ])
    TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process
    assert cash_sure.entries.exists?(external_id: "trade_republic_event_evt_settle_deleted")

    @tr_account.update!(raw_timeline_payload: [ buy.merge(deleted: true) ])
    TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process

    assert_not cash_sure.entries.exists?(external_id: "trade_republic_event_evt_settle_deleted")
  end

  test "a stale cash snapshot does not hide a trade deleted on the portfolio" do
    cash_account, cash_sure = create_linked_cash_account!
    buy = order_execution_detail(event_id: "evt_settle_stale", quantity: "2.0", isin: "IE00B5BMR087", amount: "1024.92")
    @tr_account.update!(raw_timeline_payload: [ buy ])
    cash_account.update!(raw_timeline_payload: [ buy ])
    TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process
    assert cash_sure.entries.exists?(external_id: "trade_republic_event_evt_settle_stale")

    @tr_account.update!(raw_timeline_payload: [ buy.merge(deleted: true) ])
    TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process

    assert_not cash_sure.entries.exists?(external_id: "trade_republic_event_evt_settle_stale")
  end

  test "cash account deposits are standard transactions, not transfers or investment contributions" do
    cash_account, cash_sure = create_linked_cash_account!
    wallet_top_up = deposit_event.merge(id: "evt_google_pay", eventType: "PAYMENT_INBOUND_GOOGLE_PAY", title: "Cash in")
    bank_credit = deposit_event.merge(id: "evt_bank_credit", eventType: "BANK_TRANSACTION_INCOMING", title: "Employer")
    cash_account.update!(raw_timeline_payload: [ wallet_top_up, bank_credit ])

    TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process

    top_up = cash_sure.entries.find_by!(external_id: "trade_republic_event_evt_google_pay").transaction
    assert_equal "standard", top_up.kind
    assert_nil top_up.category_id

    credit = cash_sure.entries.find_by!(external_id: "trade_republic_event_evt_bank_credit").transaction
    assert_equal "standard", credit.kind
    assert_nil credit.category_id
  end

  test "resets deposits that earlier syncs stored as investment contributions" do
    cash_account, cash_sure = create_linked_cash_account!
    category = @family.investment_contributions_category
    legacy = Account::ProviderImportAdapter.new(cash_sure).import_transaction(
      external_id: "trade_republic_event_evt_legacy_dep",
      amount: BigDecimal("-500.00"),
      currency: "EUR",
      date: Date.parse("2026-08-01"),
      name: "Cash in",
      source: "trade_republic",
      kind: "investment_contribution",
      category_id: category.id,
      investment_activity_label: "Contribution"
    )
    assert_equal "investment_contribution", legacy.transaction.kind
    assert_equal category.id, legacy.transaction.category_id

    cash_account.update!(raw_timeline_payload: [
      deposit_event.merge(id: "evt_legacy_dep", eventType: "PAYMENT_INBOUND_GOOGLE_PAY", title: "Cash in")
    ])
    TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process

    legacy.transaction.reload
    assert_equal "standard", legacy.transaction.kind
    assert_nil legacy.transaction.category_id
    assert_nil legacy.transaction.investment_activity_label
  end

  test "keeps a rule-assigned category and label on a deposit that earlier syncs stored as an investment contribution" do
    cash_account, cash_sure = create_linked_cash_account!
    category = @family.investment_contributions_category
    legacy = Account::ProviderImportAdapter.new(cash_sure).import_transaction(
      external_id: "trade_republic_event_evt_rule_legacy",
      amount: BigDecimal("-500.00"),
      currency: "EUR",
      date: Date.parse("2026-08-01"),
      name: "Cash in",
      source: "trade_republic",
      kind: "investment_contribution"
    )
    assert_nil legacy.transaction.category_id
    assert legacy.transaction.enrich_attribute(:category_id, category.id, source: "rule")
    assert legacy.transaction.enrich_attribute(:investment_activity_label, "Contribution", source: "rule")

    cash_account.update!(raw_timeline_payload: [
      deposit_event.merge(id: "evt_rule_legacy", eventType: "PAYMENT_INBOUND_GOOGLE_PAY", title: "Cash in")
    ])
    TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process

    legacy.transaction.reload
    assert_equal "standard", legacy.transaction.kind
    assert_equal category.id, legacy.transaction.category_id
    assert_equal "Contribution", legacy.transaction.investment_activity_label
  end

  test "resets payments that earlier syncs stored as fund movements" do
    cash_account, cash_sure = create_linked_cash_account!
    adapter = Account::ProviderImportAdapter.new(cash_sure)
    deposit = adapter.import_transaction(
      external_id: "trade_republic_event_evt_legacy_in", amount: BigDecimal("-2000.00"), currency: "EUR",
      date: Date.parse("2026-08-01"), name: "Niels Eulink", source: "trade_republic", kind: "funds_movement"
    )
    withdrawal = adapter.import_transaction(
      external_id: "trade_republic_event_evt_legacy_out", amount: BigDecimal("300.00"), currency: "EUR",
      date: Date.parse("2026-08-01"), name: "Payout", source: "trade_republic", kind: "funds_movement"
    )

    cash_account.update!(raw_timeline_payload: [
      deposit_event.merge(id: "evt_legacy_in", eventType: "PAYMENT_INBOUND", title: "Niels Eulink", detail: { amount: "2000.00", currency: "EUR" }),
      { id: "evt_legacy_out", timestamp: "2026-08-01T10:00:00Z", eventType: "PAYMENT_OUTBOUND", category: "POC_CREATED",
        title: "Payout", detail: { amount: "300.00", currency: "EUR" } }
    ])
    TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process

    assert_equal "standard", deposit.transaction.reload.kind
    assert_equal "standard", withdrawal.transaction.reload.kind
  end

  test "keeps fund movements of matched transfers, edited rows and sale settlements" do
    cash_account, cash_sure = create_linked_cash_account!
    bank = @family.accounts.create!(name: "Bank", balance: 0, currency: "EUR", accountable: Depository.new)
    adapter = Account::ProviderImportAdapter.new(cash_sure)
    matched = adapter.import_transaction(
      external_id: "trade_republic_event_evt_matched", amount: BigDecimal("-2000.00"), currency: "EUR",
      date: Date.parse("2026-08-01"), name: "Cash in", source: "trade_republic", kind: "funds_movement"
    )
    bank_outflow = bank.entries.create!(
      name: "To Trade Republic", amount: 2000, currency: "EUR", date: Date.parse("2026-08-01"),
      entryable: Transaction.new(kind: "funds_movement")
    )
    Transfer.create!(inflow_transaction: matched.transaction, outflow_transaction: bank_outflow.transaction)
    edited = adapter.import_transaction(
      external_id: "trade_republic_event_evt_edited", amount: BigDecimal("-100.00"), currency: "EUR",
      date: Date.parse("2026-08-01"), name: "Cash in", source: "trade_republic", kind: "funds_movement"
    )
    edited.update!(user_modified: true)

    sell = order_execution_detail(event_id: "evt_sell_keep", quantity: "-1", isin: "US0378331005", amount: "150.00")
    @tr_account.update!(raw_timeline_payload: [ sell ])
    cash_account.update!(raw_timeline_payload: [
      deposit_event.merge(id: "evt_matched", eventType: "PAYMENT_INBOUND_GOOGLE_PAY", detail: { amount: "2000.00", currency: "EUR" }),
      deposit_event.merge(id: "evt_edited", eventType: "PAYMENT_INBOUND_GOOGLE_PAY", detail: { amount: "100.00", currency: "EUR" })
    ])
    TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process

    assert_equal "funds_movement", matched.transaction.reload.kind
    assert_equal "funds_movement", edited.transaction.reload.kind
    assert_equal "funds_movement", cash_sure.entries.find_by!(external_id: "trade_republic_event_evt_sell_keep").transaction.kind
  end

  test "keeps the settlement kind on a sale that earlier syncs stored as an investment contribution" do
    cash_account, cash_sure = create_linked_cash_account!
    legacy = Account::ProviderImportAdapter.new(cash_sure).import_transaction(
      external_id: "trade_republic_event_evt_legacy_sell", amount: BigDecimal("-150.00"), currency: "EUR",
      date: Date.parse("2026-08-01"), name: "Apple", source: "trade_republic", kind: "investment_contribution"
    )
    assert_equal "investment_contribution", legacy.transaction.kind

    sell = order_execution_detail(event_id: "evt_legacy_sell", quantity: "-1", isin: "US0378331005", amount: "150.00")
    @tr_account.update!(raw_timeline_payload: [ sell ])
    TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process

    assert_equal "funds_movement", legacy.transaction.reload.kind
  end

  test "a standard deposit is matched to the bank transfer that funded it" do
    cash_account, cash_sure = create_linked_cash_account!
    bank = @family.accounts.create!(name: "Bank", balance: 0, currency: "EUR", accountable: Depository.new)
    bank_outflow = bank.entries.create!(
      name: "To Trade Republic", amount: 2000, currency: "EUR", date: Date.parse("2026-08-01"),
      entryable: Transaction.new
    )
    cash_account.update!(raw_timeline_payload: [
      deposit_event.merge(id: "evt_funded", eventType: "PAYMENT_INBOUND", detail: { amount: "2000.00", currency: "EUR" })
    ])
    TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process

    @family.auto_match_transfers!

    deposit = cash_sure.entries.find_by!(external_id: "trade_republic_event_evt_funded").transaction
    assert_equal bank_outflow.transaction, deposit.transfer&.outflow_transaction
    assert_equal "standard", deposit.reload.kind

    deposit.transfer.confirm!

    assert_equal "funds_movement", deposit.reload.kind
  end

  test "keeps a rule-assigned investment contributions category and label on cash account deposits" do
    cash_account, cash_sure = create_linked_cash_account!
    category = @family.investment_contributions_category
    cash_account.update!(raw_timeline_payload: [
      deposit_event.merge(id: "evt_rule_dep", eventType: "BANK_TRANSACTION_INCOMING", title: "Employer")
    ])
    TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process

    transaction = cash_sure.entries.find_by!(external_id: "trade_republic_event_evt_rule_dep").transaction
    assert_equal "standard", transaction.kind
    assert transaction.enrich_attribute(:category_id, category.id, source: "rule")
    assert transaction.enrich_attribute(:investment_activity_label, "Contribution", source: "rule")
    assert_not transaction.entry.reload.protected_from_sync?

    TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process

    transaction.reload
    assert_equal "standard", transaction.kind
    assert_equal category.id, transaction.category_id
    assert_equal "Contribution", transaction.investment_activity_label
  end

  test "savings-plan invoice imports as a portfolio trade and a cash outflow" do
    cash_account, cash_sure = create_linked_cash_account!
    event = savings_plan_invoice_event

    @tr_account.update!(raw_timeline_payload: [ event ])
    cash_account.update!(raw_timeline_payload: [ event ])

    TradeRepublicAccount::ActivitiesProcessor.new(@tr_account.reload).process
    TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process

    trade = find_trade("trade_republic_event_evt_savings_plan")
    assert_not_nil trade
    assert_equal "Trade", trade.entryable_type
    assert_equal BigDecimal("0.25"), trade.entryable.qty
    assert_equal "Buy", trade.entryable.investment_activity_label
    assert_equal BigDecimal("25.00"), trade.amount
    assert_equal "SAVINGS_PLAN_INVOICE_CREATED", trade.entryable.extra.dig("trade_republic", "event_type")

    cash_entry = Entry.find_by!(account: cash_sure, external_id: "trade_republic_event_evt_savings_plan")
    assert_equal "Transaction", cash_entry.entryable_type
    assert_equal BigDecimal("25.00"), cash_entry.amount
  end

  test "savings-plan invoice remains idempotent across repeated processing" do
    cash_account, cash_sure = create_linked_cash_account!
    event = savings_plan_invoice_event

    @tr_account.update!(raw_timeline_payload: [ event ])
    cash_account.update!(raw_timeline_payload: [ event ])

    TradeRepublicAccount::ActivitiesProcessor.new(@tr_account.reload).process
    TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process

    assert_difference -> { @account.entries.where(source: "trade_republic").count }, 0 do
      assert_difference -> { cash_sure.entries.where(source: "trade_republic").count }, 0 do
        TradeRepublicAccount::ActivitiesProcessor.new(@tr_account.reload).process
        TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process
      end
    end

    assert_equal 1, @account.entries.where(entryable_type: "Trade", source: "trade_republic").count
    assert_equal 1, cash_sure.entries.where(entryable_type: "Transaction", source: "trade_republic").count
  end

  test "savings-plan invoice duplicated by an execution event imports once" do
    invoice = savings_plan_invoice_event
    execution = savings_plan_invoice_event.merge(
      id: "evt_savings_execution",
      timestamp: "2026-06-17T10:00:05Z",
      eventType: "TRADING_SAVINGSPLAN_EXECUTED"
    )

    @tr_account.update!(raw_timeline_payload: [ invoice, execution ])
    TradeRepublicAccount::ActivitiesProcessor.new(@tr_account.reload).process

    assert_nil find_trade("trade_republic_event_evt_savings_plan")
    assert_not_nil find_trade("trade_republic_event_evt_savings_execution")
  end

  test "failed savings-plan executions remain ignored" do
    cash_account, = create_linked_cash_account!
    failed = {
      id: "evt_savings_failed",
      timestamp: "2026-06-17T10:00:00Z",
      eventType: "TRADING_SAVINGSPLAN_EXECUTION_FAILED",
      title: "MSCI World",
      detail: { amount: -25.0, currency: "EUR" }
    }

    @tr_account.update!(raw_timeline_payload: [ failed ])
    cash_account.update!(raw_timeline_payload: [ failed ])

    assert_no_difference "Entry.where(source: 'trade_republic').count" do
      TradeRepublicAccount::ActivitiesProcessor.new(@tr_account.reload).process
      TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process
    end

    assert_not Entry.exists?(external_id: "trade_republic_event_evt_savings_failed")
  end

  test "incomplete savings-plan event is skipped until backfill enriches the same stored event" do
    incomplete = savings_plan_invoice_event.deep_merge(detail: { isin: nil, quantity: nil })
    @tr_account.update!(raw_timeline_payload: [ incomplete ])

    assert_no_difference "Entry.where(source: 'trade_republic').count" do
      TradeRepublicAccount::ActivitiesProcessor.new(@tr_account.reload).process
    end
    assert_not Entry.exists?(account: @account, external_id: "trade_republic_event_evt_savings_plan")

    @tr_account.update!(raw_timeline_payload: [ savings_plan_invoice_event ])
    TradeRepublicAccount::ActivitiesProcessor.new(@tr_account.reload).process

    trade = find_trade("trade_republic_event_evt_savings_plan")
    assert_not_nil trade
    assert_equal BigDecimal("0.25"), trade.entryable.qty
    assert_equal BigDecimal("25.00"), trade.amount
  end

  test "executed card payments import while declined cards are skipped" do
    cash_account, cash_sure = create_linked_cash_account!
    cash_account.update!(raw_timeline_payload: [
      {
        id: "evt_card_ok",
        timestamp: "2026-08-01T10:00:00Z",
        eventType: "CARD_TRANSACTION",
        category: "POC_CREATED",
        status: "EXECUTED",
        title: "Coffee",
        detail: { amount: "4.50", currency: "EUR" }
      },
      {
        id: "evt_card_declined",
        timestamp: "2026-08-01T11:00:00Z",
        eventType: "CARD_TRANSACTION",
        category: "POC_CREATED",
        status: "DECLINED",
        title: "Blocked",
        detail: { amount: "99.00", currency: "EUR" }
      }
    ])

    TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process

    assert Entry.exists?(account: cash_sure, external_id: "trade_republic_event_evt_card_ok")
    assert_not Entry.exists?(account: cash_sure, external_id: "trade_republic_event_evt_card_declined")
  end

  test "blank status with declined subtitle skips import without matching security titles" do
    cash_account, cash_sure = create_linked_cash_account!
    cash_account.update!(raw_timeline_payload: [
      {
        id: "evt_subtitle_declined",
        timestamp: "2026-08-01T10:00:00Z",
        eventType: "CARD_TRANSACTION",
        category: "POC_CREATED",
        title: "Coffee Shop",
        subtitle: "Payment declined",
        detail: { amount: "12.00", currency: "EUR" }
      },
      {
        id: "evt_title_cancel_ok",
        timestamp: "2026-08-01T11:00:00Z",
        eventType: "CARD_TRANSACTION",
        category: "POC_CREATED",
        title: "Cancellation Fee Shop",
        subtitle: "Card payment",
        detail: { amount: "3.00", currency: "EUR" }
      }
    ])

    TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process

    assert_not Entry.exists?(account: cash_sure, external_id: "trade_republic_event_evt_subtitle_declined")
    assert Entry.exists?(account: cash_sure, external_id: "trade_republic_event_evt_title_cancel_ok")
  end

  test "blank status declined subtitle keeps prior imports" do
    cash_account, cash_sure = create_linked_cash_account!
    Account::ProviderImportAdapter.new(cash_sure).import_transaction(
      external_id: "trade_republic_event_evt_later_declined",
      amount: BigDecimal("12.00"),
      currency: "EUR",
      date: Date.parse("2026-08-01"),
      name: "Pending card",
      source: "trade_republic",
      investment_activity_label: "Card payment"
    )

    cash_account.update!(raw_timeline_payload: [ {
      id: "evt_later_declined",
      timestamp: "2026-08-01T10:00:00Z",
      eventType: "CARD_TRANSACTION",
      category: "POC_CREATED",
      title: "Coffee",
      subtitle: "Transaction failed",
      detail: { amount: "12.00", currency: "EUR" }
    } ])

    assert_difference -> { DebugLogEntry.where("message LIKE ?", "%flagged only by its subtitle%").count }, 1 do
      TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process
    end

    assert Entry.exists?(account: cash_sure, external_id: "trade_republic_event_evt_later_declined")
  end

  test "hidden events remove unprotected prior imports" do
    cash_account, cash_sure = create_linked_cash_account!
    Account::ProviderImportAdapter.new(cash_sure).import_transaction(
      external_id: "trade_republic_event_evt_card_hidden",
      amount: BigDecimal("12.00"),
      currency: "EUR",
      date: Date.parse("2026-08-01"),
      name: "Hidden card",
      source: "trade_republic",
      investment_activity_label: "Card payment"
    )

    cash_account.update!(raw_timeline_payload: [ {
      id: "evt_card_hidden",
      timestamp: "2026-08-01T10:00:00Z",
      eventType: "CARD_TRANSACTION",
      category: "POC_CREATED",
      title: "Coffee",
      hidden: true,
      detail: { amount: "12.00", currency: "EUR" }
    } ])

    TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process

    assert_not Entry.exists?(account: cash_sure, external_id: "trade_republic_event_evt_card_hidden")
  end

  test "deleted financial events are skipped and unprotected prior imports are removed" do
    cash_account, cash_sure = create_linked_cash_account!
    Account::ProviderImportAdapter.new(cash_sure).import_transaction(
      external_id: "trade_republic_event_evt_card_deleted",
      amount: BigDecimal("12.00"),
      currency: "EUR",
      date: Date.parse("2026-08-01"),
      name: "Ghost payment",
      source: "trade_republic",
      investment_activity_label: "Card payment"
    )

    cash_account.update!(raw_timeline_payload: [ {
      id: "evt_card_deleted",
      timestamp: "2026-08-01T10:00:00Z",
      eventType: "CARD_TRANSACTION",
      category: "POC_CREATED",
      deleted: true,
      title: "Ghost payment",
      detail: { amount: "12.00", currency: "EUR" }
    } ])

    TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process

    assert_not Entry.exists?(account: cash_sure, external_id: "trade_republic_event_evt_card_deleted")
  end

  test "hidden financial events are skipped and unprotected prior imports are removed" do
    cash_account, cash_sure = create_linked_cash_account!
    Account::ProviderImportAdapter.new(cash_sure).import_transaction(
      external_id: "trade_republic_event_evt_card_hidden",
      amount: BigDecimal("12.00"),
      currency: "EUR",
      date: Date.parse("2026-08-01"),
      name: "Hidden payment",
      source: "trade_republic",
      investment_activity_label: "Card payment"
    )

    cash_account.update!(raw_timeline_payload: [ {
      id: "evt_card_hidden",
      timestamp: "2026-08-01T10:00:00Z",
      eventType: "CARD_TRANSACTION",
      category: "POC_CREATED",
      hidden: true,
      title: "Hidden payment",
      detail: { amount: "12.00", currency: "EUR" }
    } ])

    TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process

    assert_not Entry.exists?(account: cash_sure, external_id: "trade_republic_event_evt_card_hidden")
  end

  test "administrative and created-order events are ignored without logging or importing" do
    cash_account, = create_linked_cash_account!
    cash_account.update!(raw_timeline_payload: [
      {
        id: "evt_card_verify",
        timestamp: "2026-08-01T10:00:00Z",
        eventType: "CARD_VERIFICATION",
        title: "Card verification"
      },
      {
        id: "evt_limit_buy_created",
        timestamp: "2026-09-22T13:36:39Z",
        eventType: "TRADING_ORDER_CREATED",
        title: "SanDisk",
        subtitle: "Limit buy created"
      }
    ])

    assert_no_difference "DebugLogEntry.count" do
      assert_no_difference "Entry.where(source: 'trade_republic').count" do
        TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process
      end
    end
  end

  test "Google Pay inbound deposits import as standard deposits" do
    cash_account, cash_sure = create_linked_cash_account!
    cash_account.update!(raw_timeline_payload: [ {
      id: "evt_gpay",
      timestamp: "2024-03-28T08:29:35Z",
      eventType: "PAYMENT_INBOUND_GOOGLE_PAY",
      title: "Cash in",
      detail: { amount: "50.00", currency: "EUR" }
    } ])

    TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process

    entry = Entry.find_by!(account: cash_sure, external_id: "trade_republic_event_evt_gpay")
    assert_equal BigDecimal("-50.00"), entry.amount
    assert_equal "standard", entry.transaction.kind
    assert_nil entry.transaction.investment_activity_label
  end

  test "Legal documents title without event type is ignored silently" do
    cash_account, = create_linked_cash_account!
    cash_account.update!(raw_timeline_payload: [ {
      id: "evt_legal",
      timestamp: "2026-07-28T12:42:32Z",
      title: "Legal documents",
      subtitle: "Accepted"
    } ])

    assert_no_difference "DebugLogEntry.count" do
      assert_no_difference "Entry.where(source: 'trade_republic').count" do
        TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process
      end
    end
  end

  test "truly unknown financial mapping gaps are logged once without creating entries" do
    cash_account, = create_linked_cash_account!
    cash_account.update!(raw_timeline_payload: [ {
      id: "evt_mapping_gap",
      timestamp: "2026-08-01T10:00:00Z",
      eventType: "BRAND_NEW_MAPPING_GAP",
      title: "Mystery payout",
      status: "EXECUTED"
    } ])

    assert_difference "DebugLogEntry.count", 1 do
      assert_no_difference "Entry.where(source: 'trade_republic').count" do
        TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process
      end
    end

    log = DebugLogEntry.order(:created_at).last
    assert_match(/unsupported timeline event/i, log.message)
    assert_equal "BRAND_NEW_MAPPING_GAP", log.metadata["event_type"]
    assert_equal "EXECUTED", log.metadata["status"]
  end

  test "unknown events are logged once across split portfolio and cash accounts" do
    cash_account, = create_linked_cash_account!
    event = {
      id: "evt_split_gap",
      timestamp: "2026-08-01T10:00:00Z",
      eventType: "BRAND_NEW_MAPPING_GAP",
      title: "Mystery payout"
    }
    @tr_account.update!(raw_timeline_payload: [ event ])
    cash_account.update!(raw_timeline_payload: [ event ])

    assert_difference -> { DebugLogEntry.where("message LIKE ?", "%unsupported timeline event%").count }, 1 do
      TradeRepublicAccount::ActivitiesProcessor.new(@tr_account.reload).process
      TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process
    end
  end

  test "retry attempt markers stay out of the transaction provider detail" do
    markers = Provider::TradeRepublicClient::RETRY_MARKER_KEYS
    assert_includes markers, Provider::TradeRepublicClient::DETAIL_BACKFILL_ATTEMPTED_AT_KEY
    cash_account, cash_sure = create_linked_cash_account!
    cash_account.update!(raw_timeline_payload: [ deposit_event.deep_merge(
      detail: markers.index_with { "2026-08-01T10:00:00Z" }
    ) ])

    TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process

    entry = Entry.find_by!(account: cash_sure, external_id: "trade_republic_event_evt_dep")
    provider_detail = entry.entryable.extra.dig("trade_republic", "provider_detail")
    assert_empty provider_detail.keys & markers
  end

  test "preserves protected entries when reconciling declined upstream events" do
    cash_account, cash_sure = create_linked_cash_account!
    entry = Account::ProviderImportAdapter.new(cash_sure).import_transaction(
      external_id: "trade_republic_event_evt_card_protected",
      amount: BigDecimal("15.00"),
      currency: "EUR",
      date: Date.parse("2026-08-01"),
      name: "Kept payment",
      source: "trade_republic",
      investment_activity_label: "Card payment"
    )
    entry.mark_user_modified!

    cash_account.update!(raw_timeline_payload: [ {
      id: "evt_card_protected",
      timestamp: "2026-08-01T10:00:00Z",
      eventType: "CARD_TRANSACTION",
      category: "POC_CREATED",
      status: "DECLINED",
      title: "Kept payment",
      detail: { amount: "15.00", currency: "EUR" }
    } ])

    TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process

    assert Entry.exists?(account: cash_sure, external_id: "trade_republic_event_evt_card_protected")
    assert entry.reload.user_modified?
  end

  test "bond purchase books nominal and percent-of-par price on the bond's own security" do
    @tr_account.update!(raw_positions_payload: [
      {
        "isin" => "IT0005377152",
        "name" => "ITALIEN 19/40",
        "category" => "interest_products",
        "instrument_type" => "bond",
        "quantity" => "2677.95",
        "price" => "0.8404"
      }
    ])
    TradeRepublicAccount::HoldingsProcessor.new(@tr_account.reload).process

    import_event({
      id: "evt_bond",
      timestamp: "2025-11-18T11:19:31Z",
      category: "orderExecution",
      title: "März 2040",
      detail: {
        isin: "IT0005377152",
        name: "März 2040",
        quantity: "2677.95",
        price: "0.9267",
        amount: "2498.31",
        fees: "1.0",
        currency: "EUR",
        instrument_type: "bond"
      }
    })

    entry = find_trade("trade_republic_event_evt_bond")
    trade = entry.entryable
    assert_equal "IT0005377152", trade.security.ticker
    assert trade.security.offline?
    assert_equal @account.holdings.first.security, trade.security
    assert_equal BigDecimal("2677.95"), trade.qty
    assert_equal BigDecimal("0.9267"), trade.price
    assert_equal BigDecimal("1"), trade.fee
    assert_equal BigDecimal("2498.31"), entry.amount
    assert_equal "IT0005377152 · 2677.95x ITALIEN 19/40", entry.name
  end

  test "a sold bond's security takes the instrument name stamped on its trade" do
    import_event({
      id: "evt_sold_bond",
      timestamp: "2025-11-18T11:19:31Z",
      category: "orderExecution",
      title: "März 2040",
      detail: {
        isin: "IT0005377152",
        name: "März 2040",
        instrument_name: "ITALIEN 19/40",
        quantity: "-2677.95",
        price: "0.95",
        amount: "2544.05",
        currency: "EUR",
        instrument_type: "bond"
      }
    })

    entry = find_trade("trade_republic_event_evt_sold_bond")
    assert_equal "ITALIEN 19/40", entry.entryable.security.name
    assert_equal "IT0005377152 · 2677.95x ITALIEN 19/40", entry.name
  end

  test "stored bond trades on the legacy BOND listing resolve by ISIN" do
    Security.expects(:search_provider).never
    import_event({
      id: "evt_legacy_bond",
      timestamp: "2025-11-18T11:19:31Z",
      category: "orderExecution",
      detail: {
        isin: "IT0005377152",
        name: "März 2040",
        quantity: "2677.95",
        price: "0.9267",
        amount: "2498.31",
        currency: "EUR",
        symbol: "BOND",
        exchange_slug: "LSX"
      }
    })

    security = find_trade("trade_republic_event_evt_legacy_bond").entryable.security
    assert_equal "IT0005377152", security.ticker
    assert_nil security.exchange_operating_mic
  end

  private

    def create_linked_crypto_account!
      crypto_provider = @item.trade_republic_accounts.create!(
        name: "Crypto",
        kind: "crypto",
        trade_republic_account_id: "crypto:DEPROC1",
        currency: "EUR"
      )
      crypto_sure = @family.accounts.create!(
        name: "Trade Republic Crypto",
        balance: 0,
        cash_balance: 0,
        currency: "EUR",
        accountable: Crypto.new(subtype: "exchange")
      )
      crypto_provider.ensure_account_provider!(crypto_sure)
      [ crypto_provider.reload, crypto_sure ]
    end

    def process_all(*trade_republic_accounts)
      trade_republic_accounts.each do |trade_republic_account|
        TradeRepublicAccount::ActivitiesProcessor.new(trade_republic_account.reload).process
      end
    end

    def create_linked_cash_account!
      cash_provider = @item.trade_republic_accounts.create!(
        name: "Cash",
        kind: "cash",
        trade_republic_account_id: "cash:DEPROC1",
        currency: "EUR",
        raw_timeline_payload: []
      )
      cash_sure = @family.accounts.create!(
        name: "Trade Republic Cash",
        balance: 0,
        cash_balance: 0,
        currency: "EUR",
        accountable: Depository.new
      )
      cash_provider.ensure_account_provider!(cash_sure)
      [ cash_provider.reload, cash_sure ]
    end

    def saveback_event
      {
        id: "evt_saveback",
        timestamp: "2026-09-02T08:11:30.780+0000",
        eventType: "SAVEBACK_AGGREGATE",
        category: "POC_CREATED",
        title: "STOXX Global Dividend 100 EUR (Dist)",
        subtitle: "Saveback",
        detail: {
          amount: "3.74",
          signed_amount: "-3.74",
          currency: "EUR",
          quantity: "0.09329",
          isin: "DE000A0F5UH1",
          name: "STOXX Global Dividend 100 EUR (Dist)"
        }
      }
    end

    # Shaped like a stored stock bonus: no status, no amount on the timeline,
    # shares and value from the detail.
    def private_markets_event(id: "evt_pm_bonus", subtitle: "1 % Bonus", quantity: "0.2", amount: "20.0")
      {
        id: id,
        timestamp: "2025-09-15T13:36:00.000+0000",
        eventType: "PRIVATE_MARKET_FUND_TRADE_EXECUTED",
        category: "orderExecution",
        title: "Private Equity",
        subtitle: subtitle,
        status: "EXECUTED",
        detail: {
          amount: amount,
          signed_amount: "-#{amount}",
          currency: "EUR",
          quantity: quantity,
          isin: "LU0000000001",
          name: "Private Equity"
        }
      }
    end

    def stamp_duty_event(id:, timestamp:, amount:, cancellation: false, status: "EXECUTED", title: "Stamp duty (Portfolio)")
      {
        id: id,
        timestamp: timestamp,
        eventType: "STAMP_DUTY_TAX_PAID",
        title: title,
        subtitle: ("Cancellation of stamp duty" if cancellation),
        status: status,
        detail: { amount: amount, signed_amount: amount, currency: "EUR" }
      }.compact
    end

    def stock_bonus_event
      {
        id: "evt_stock_bonus",
        timestamp: "2025-06-25T14:52:12.282+0000",
        eventType: "ACQUISITION_TRADE_PERK",
        title: "Stock Bonus",
        subtitle: "Redeemed",
        detail: {
          amount: "10.04",
          currency: "EUR",
          quantity: "0.055",
          isin: "US0231351067",
          name: "Amazon.com"
        }
      }
    end

    # Shaped like a stored securities transfer: shares and price from the
    # detail, no cash amount. The quantity is already signed by the client
    # detail parser (negative for outgoing transfers).
    def securities_transfer_event(id:, event_type:, quantity:)
      {
        id: id,
        timestamp: "2026-10-20T09:00:00.000+0000",
        eventType: event_type,
        category: "orderExecution",
        title: "STOXX Global Dividend 100 EUR (Dist)",
        subtitle: "Securities transfer",
        detail: {
          currency: "EUR",
          quantity: quantity,
          price: "45.12",
          isin: "DE000A0F5UH1",
          name: "STOXX Global Dividend 100 EUR (Dist)"
        }
      }
    end

    def round_up_event
      {
        id: "evt_round_up",
        timestamp: "2026-09-02T08:11:30.780+0000",
        eventType: "SPARE_CHANGE_AGGREGATE",
        category: "POC_CREATED",
        title: "STOXX Global Dividend 100 EUR (Dist)",
        subtitle: "Round up",
        detail: {
          amount: "-0.40",
          signed_amount: "-0.40",
          currency: "EUR",
          quantity: "0.009977",
          isin: "DE000A0F5UH1",
          name: "STOXX Global Dividend 100 EUR (Dist)"
        }
      }
    end

    def savings_plan_invoice_event
      {
        id: "evt_savings_plan",
        timestamp: "2026-06-17T10:00:00Z",
        eventType: "SAVINGS_PLAN_INVOICE_CREATED",
        category: "orderExecution",
        title: "MSCI World",
        subtitle: "Savings plan",
        detail: {
          amount: "25.00",
          signed_amount: "-25.00",
          currency: "EUR",
          quantity: "0.25",
          isin: "IE00B4L5Y983",
          name: "MSCI World"
        }
      }
    end

    def import_event(event)
      @tr_account.update!(raw_timeline_payload: [ event ])
      TradeRepublicAccount::ActivitiesProcessor.new(@tr_account.reload).process
    end

    def incomplete_execution_logs
      DebugLogEntry.where("message LIKE ?", "%not imported yet%")
    end

    def order_execution_detail(event_id: "evt_buy", quantity:, isin:, amount:)
      {
        id: event_id,
        timestamp: "2026-07-15T09:30:00Z",
        category: "orderExecution",
        detail: {
          isin: isin,
          name: "Apple Inc.",
          quantity: quantity,
          amount: amount,
          currency: "EUR"
        }
      }
    end

    def deposit_event
      {
        id: "evt_dep",
        timestamp: "2026-08-01T10:00:00Z",
        category: "PAYMENT_RECEIVED",
        detail: { amount: "500.00", currency: "EUR" }
      }
    end

    def find_trade(external_id)
      @account.entries.find_by(external_id: external_id)
    end

    SETTLEMENT_MISMATCH_MESSAGE = "Trade Republic order booked at its detail total instead of the timeline amount"

    # The timeline list amount stays 81.33; detail_amount is what the event's
    # detail reports as its total.
    def net_sale_event(detail_amount:)
      order_execution_detail(event_id: "evt_net_sell", quantity: "-3.90625", isin: "GB00BD9G2S12", amount: detail_amount)
        .deep_merge(title: "Gates Industrial", subtitle: "Sell Order", detail: { signed_amount: 81.33, fees: "1.0", taxes: "1.72" })
    end
end