import { test, expect } from '@playwright/test'

test.describe('Settings Page', () => {
    test.beforeEach(async ({ page }) => {
        await page.goto('/settings')
        await page.waitForLoadState('networkidle')
    })

    test('displays settings page title', async ({ page }) => {
        await expect(page.getByRole('heading', { name: /settings/i })).toBeVisible()
    })

    test('shows exchange configuration section', async ({ page }) => {
        await expect(page.getByText(/exchange/i).first()).toBeVisible()
    })

    test('shows alert configuration section', async ({ page }) => {
        await expect(page.getByText(/alert/i).first()).toBeVisible()
    })

    test('has tabs for different settings categories', async ({ page }) => {
        // Look for tab navigation
        const tabs = page.getByRole('tab')
        await expect(tabs.first()).toBeVisible()
    })
})

test.describe('Settings - Exchange Configuration', () => {
    test.beforeEach(async ({ page }) => {
        await page.goto('/settings')
        await page.waitForLoadState('networkidle')
    })

    test('shows add exchange option', async ({ page }) => {
        // Look for add exchange button or option
        const addExchange = page.getByRole('button', { name: /add.*exchange|new.*exchange|configure/i })
        await expect(addExchange.first()).toBeVisible()
    })
})

test.describe('Settings - Performance', () => {
    test('page loads within reasonable time', async ({ page }) => {
        const startTime = Date.now()

        await page.goto('/settings')
        await page.waitForLoadState('domcontentloaded')

        const loadTime = Date.now() - startTime

        // Page should load within 5 seconds
        expect(loadTime).toBeLessThan(5000)
    })

    test('no console errors on load', async ({ page }) => {
        const errors: string[] = []

        page.on('console', (msg) => {
            if (msg.type() === 'error') {
                errors.push(msg.text())
            }
        })

        await page.goto('/settings')
        await page.waitForLoadState('networkidle')

        // Filter out known non-critical errors
        const criticalErrors = errors.filter(
            (e) => !e.includes('WebSocket') && !e.includes('Failed to load resource')
        )

        expect(criticalErrors).toHaveLength(0)
    })
})
