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

test.describe('Agent Dashboard', () => {

    test('agent can access dashboard and see title', async ({ page }) => {
        await loginAs(page, AGENT.mobile, AGENT.password);
        await page.goto(`${BASE_URL}/agent/dashboard`);

        await expect(page.locator('[data-testid="agent-dashboard-title"]')).toBeVisible();
        await expect(page.locator('[data-testid="agent-dashboard-title"]')).toContainText('Agent Dashboard');
    });

    test('agent dashboard shows referral code in hex format', async ({ page }) => {
        await loginAs(page, AGENT.mobile, AGENT.password);
        await page.goto(`${BASE_URL}/agent/dashboard`);

        const codeInput = page.locator('[data-testid="referral-code-display"]');
        await expect(codeInput).toBeVisible();
        const code = await codeInput.inputValue();
        expect(code).toMatch(/^[A-F0-9]{8}$/);
    });

    test('agent dashboard shows copy code and copy link buttons', async ({ page }) => {
        await loginAs(page, AGENT.mobile, AGENT.password);
        await page.goto(`${BASE_URL}/agent/dashboard`);

        await expect(page.locator('[data-testid="btn-copy-code"]')).toContainText('Copy Code');
        await expect(page.locator('[data-testid="btn-copy-link"]')).toBeVisible();
    });

    test('referral link contains /register?ref=', async ({ page }) => {
        await loginAs(page, AGENT.mobile, AGENT.password);
        await page.goto(`${BASE_URL}/agent/dashboard`);

        const linkInput = page.locator('[data-testid="referral-link-display"]');
        await expect(linkInput).toBeVisible();
        const link = await linkInput.inputValue();
        expect(link).toContain('/register?ref=');
    });

    test('referred count is visible and numeric', async ({ page }) => {
        await loginAs(page, AGENT.mobile, AGENT.password);
        await page.goto(`${BASE_URL}/agent/dashboard`);

        const countEl = page.locator('[data-testid="referred-count"]');
        await expect(countEl).toBeVisible();
        const text = await countEl.textContent();
        expect(Number(text?.trim())).toBeGreaterThanOrEqual(0);
    });

    test('agent navbar shows agent dashboard link', async ({ page }) => {
        await loginAs(page, AGENT.mobile, AGENT.password);
        await page.click('[data-testid="navbar-user-menu"]');
        await expect(page.locator('[data-testid="nav-link-agent-dashboard"]')).toBeVisible();
    });

    test('commission rate stat card is visible on dashboard', async ({ page }) => {
        await loginAs(page, AGENT.mobile, AGENT.password);
        await page.goto(`${BASE_URL}/agent/dashboard`);
        await expect(page.locator('[data-testid="stat-agent-commissionRate"]')).toBeVisible();
    });

    test('total downline deposits stat card is visible on dashboard', async ({ page }) => {
        await loginAs(page, AGENT.mobile, AGENT.password);
        await page.goto(`${BASE_URL}/agent/dashboard`);
        await expect(page.locator('[data-testid="stat-agent-downlineDeposits"]')).toBeVisible();
    });

    test('non-agent cannot see agent dashboard content', async ({ page }) => {
        await loginAs(page, USER.mobile, USER.password);
        await page.goto(`${BASE_URL}/agent/dashboard`);

        // Authenticated user without ROLE_MASTER_AGENT gets a 403 — dashboard title must not appear
        await expect(page.locator('[data-testid="agent-dashboard-title"]')).not.toBeVisible();
    });

});
