import { test, expect } from '@playwright/test';
import { BASE_URL, USER, loginAs } from '../helpers';

test.describe('Transaction History', () => {

    test.beforeEach(async ({ page }) => {
        await loginAs(page, USER.mobile, USER.password);
        await page.goto(`${BASE_URL}/transactions`);
    });

    test('transactions page loads with title', async ({ page }) => {
        await expect(page.locator('[data-testid="transactions-page-title"]')).toBeVisible();
    });

    test('three tab buttons are visible', async ({ page }) => {
        await expect(page.locator('[data-testid="tab-deposits"]')).toBeVisible();
        await expect(page.locator('[data-testid="tab-adjustments"]')).toBeVisible();
        const withdrawalsTab = page.locator('[data-testid="tab-withdrawals"]');
        if (await withdrawalsTab.count() > 0) {
            await expect(withdrawalsTab).toBeVisible();
        }
    });

    test('deposits table is visible by default', async ({ page }) => {
        await expect(page.locator('[data-testid="transactions-table"]')).toBeVisible();
    });

    test('clicking the withdrawals tab shows withdrawals table', async ({ page }) => {
        const tab = page.locator('[data-testid="tab-withdrawals"]');
        if (await tab.count() === 0) {
            test.skip();
            return;
        }
        await tab.click();
        await expect(page.locator('[data-testid="withdrawals-table"]')).toBeVisible();
    });

    test('quick-link buttons for new deposit and new withdrawal are present', async ({ page }) => {
        await expect(page.locator('[data-testid="btn-new-deposit"]')).toBeVisible();
        const withdrawalBtn = page.locator('[data-testid="btn-new-withdrawal"]');
        if (await withdrawalBtn.count() > 0) {
            await expect(withdrawalBtn).toBeVisible();
        }
    });

    test('unauthenticated access redirects to /login', async ({ page }) => {
        await page.context().clearCookies();
        await page.goto(`${BASE_URL}/transactions`);
        expect(page.url()).toContain('/login');
    });

});
