import { describe, it, expect, beforeEach } from 'vitest'
import { render, screen, fireEvent } from '@testing-library/react'
import { I18nProvider } from '../i18n'
import { ActivityPage } from './ActivityPage'
import { _resetActivity, addActivity } from '../activity/store'

const renderAct = (locale: 'en' | 'ar' = 'en') =>
  render(<I18nProvider initial={locale}><ActivityPage /></I18nProvider>)

describe('ActivityPage', () => {
  beforeEach(() => _resetActivity())

  it('labels the list as session-only (honest, not permanent history)', () => {
    renderAct()
    expect(screen.getByText('Activity from this session')).toBeInTheDocument()
    expect(screen.getByText(/not permanent account history/i)).toBeInTheDocument()
  })

  it('shows an empty state when there is no session activity', () => {
    renderAct()
    expect(screen.getByTestId('activity-empty')).toBeInTheDocument()
  })

  it('lists a recorded event and filters by tab', () => {
    addActivity({ kind: 'payment', title: 'Blue Bottle', status: 'succeeded', amount: '150.50 SAR' })
    addActivity({ kind: 'security-demo', title: 'New payee', status: 'deny', reasonCodes: ['NEW_PAYEE'] })
    renderAct()
    expect(screen.getAllByTestId('activity-item').length).toBe(2)
    fireEvent.click(screen.getByRole('tab', { name: 'Payments' }))
    expect(screen.getAllByTestId('activity-item').length).toBe(1)
    expect(screen.getByText('Blue Bottle')).toBeInTheDocument()
  })

  it('never renders full IBANs or capability text', () => {
    addActivity({ kind: 'payment', title: 'Blue Bottle', status: 'succeeded', reference: 'p1' })
    const { container } = renderAct()
    expect(container.textContent).not.toMatch(/SA\d{22}|CapabilityGrant|signature/i)
  })
})
