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

// AGENT (+10000000002) is a master agent whose fixture referral code is
// ABCDEF01. AppFixtures wires no downline, so the roster test creates its own.

test.describe('Agent Portal — downline pages role guard', () => {
    for (const [path, testid] of [
        ['/agent/live-players', 'agent-live-players-title'],
        ['/agent/downline',     'agent-downline-title'],
        ['/agent/requests',     'agent-requests-title'],
    ] as const) {
        test(`non-agent USER cannot access ${path}`, async ({ page }) => {
            await loginAs(page, USER.mobile, USER.password);
            await page.goto(`${BASE_URL}${path}`);
            await expect(page.locator(`[data-testid="${testid}"]`)).not.toBeVisible();
        });
    }
});

test.describe('Agent Portal — downline pages', () => {

    test.beforeEach(async ({ page }) => {
        await loginAs(page, AGENT.mobile, AGENT.password);
    });

    test('Live Players page loads (table or empty state)', async ({ page }) => {
        await page.goto(`${BASE_URL}/agent/live-players`);
        await expect(page.locator('[data-testid="agent-live-players-title"]')).toBeVisible();
        const table = page.locator('[data-testid="agent-live-players-table"]');
        const empty = page.locator('[data-testid="agent-live-players-empty"]');
        await expect(table.or(empty)).toBeVisible();
    });

    test('Downline page loads with search form and table', async ({ page }) => {
        await page.goto(`${BASE_URL}/agent/downline`);
        await expect(page.locator('[data-testid="agent-downline-title"]')).toBeVisible();
        await expect(page.locator('[data-testid="agent-downline-search-form"]')).toBeVisible();
        await expect(page.locator('[data-testid="agent-downline-table"]')).toBeVisible();
    });

    test('Deposits & Withdrawals page loads with status filters and type tabs', async ({ page }) => {
        await page.goto(`${BASE_URL}/agent/requests`);
        await expect(page.locator('[data-testid="agent-requests-title"]')).toBeVisible();
        await expect(page.locator('[data-testid="agent-requests-table"]')).toBeVisible();
        await expect(page.locator('[data-testid="filter-pending"]')).toBeVisible();
        await expect(page.locator('[data-testid="filter-approved"]')).toBeVisible();
        await expect(page.locator('[data-testid="filter-rejected"]')).toBeVisible();
        await expect(page.locator('[data-testid="agent-requests-tab-deposits"]')).toBeVisible();
        await expect(page.locator('[data-testid="agent-requests-tab-withdrawals"]')).toBeVisible();
    });

    test('status filter and type toggle update the URL', async ({ page }) => {
        await page.goto(`${BASE_URL}/agent/requests`);

        await page.click('[data-testid="filter-approved"]');
        await expect(page).toHaveURL(/status=approved/);

        await page.click('[data-testid="agent-requests-tab-withdrawals"]');
        await expect(page).toHaveURL(/type=withdrawals/);
    });

    test('the grouped nav dropdowns open and navigate', async ({ page }) => {
        await page.goto(`${BASE_URL}/agent/dashboard`);

        // Downline ▾ → Live Players
        await page.click('[data-testid="agent-nav-downline-menu"]');
        await page.click('[data-testid="agent-nav-live-players"]');
        await expect(page).toHaveURL(/\/agent\/live-players$/);

        // Payments ▾ → Deposits & Withdrawals
        await page.click('[data-testid="agent-nav-payments-menu"]');
        await page.click('[data-testid="agent-nav-requests"]');
        await expect(page).toHaveURL(/\/agent\/requests$/);
    });
});

test.describe('Agent Portal — downline roster', () => {
    test('a newly referred player appears in the agent downline table', async ({ page }) => {
        const { mobile } = await registerUserWithReferralCode(page, 'ABCDEF01');

        await loginAs(page, AGENT.mobile, AGENT.password);
        await page.goto(`${BASE_URL}/agent/downline`);

        await expect(page.locator('[data-testid="agent-downline-table"]')).toContainText(mobile);
    });
});
