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

test.describe('Home Dashboard — USER', () => {

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

    test('sticky wallet bar is visible', async ({ page }) => {
        await expect(page.locator('[data-testid="wallet-bar"]')).toBeVisible();
    });

    test('wallet bar shows peso balance', async ({ page }) => {
        await expect(page.locator('[data-testid="wallet-bar-amount"]')).toHaveText(/₱[\d,]+\.\d{2}/);
    });

    test('wallet bar Add Funds link navigates to /deposit', async ({ page }) => {
        const href = await page.locator('[data-testid="wallet-bar-add-funds"]').getAttribute('href');
        await page.goto(`${BASE_URL}${href}`);
        await expect(page).toHaveURL(`${BASE_URL}/deposit`);
    });

    test('Video Karera game card is visible and links to /play/video-karera', async ({ page }) => {
        const card = page.locator('[data-testid="play-video-karera-card"]');
        await expect(card).toBeVisible();
        const href = await card.getAttribute('href');
        expect(href).toContain('/play/video-karera');
    });

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

});

test.describe('Home Dashboard — AGENT', () => {

    test('agent can access home page and sees wallet bar', async ({ page }) => {
        await loginAs(page, AGENT.mobile, AGENT.password);
        await page.goto(`${BASE_URL}/`);
        await expect(page.locator('[data-testid="wallet-bar"]')).toBeVisible();
    });

});

test.describe('Home Dashboard — Mines card', () => {

    async function ensureMinesEnabled(page: import('@playwright/test').Page) {
        await loginAs(page, ADMIN.mobile, ADMIN.password);
        await page.goto(`${BASE_URL}/wfbo/mines/settings`, { waitUntil: 'domcontentloaded' });
        const toggle = page.locator('[data-testid="toggle-mg-enabled"]');
        if (!(await toggle.isChecked())) {
            await toggle.check();
            await page.click('[data-testid="btn-save-mg-settings"]');
            await expect(page.locator('[data-testid="flash-success"]')).toBeVisible();
        }
    }

    test('Mines game card is visible and links to /play/mines when enabled', async ({ page }) => {
        await ensureMinesEnabled(page);
        await loginAs(page, USER.mobile, USER.password);
        await page.goto(`${BASE_URL}/`);

        const card = page.locator('[data-testid="play-mines-card"]');
        await expect(card).toBeVisible();
        const href = await card.getAttribute('href');
        expect(href).toContain('/play/mines');
    });

});

test.describe('Home Dashboard — Game card responsive layout', () => {

    test('mobile viewport hides game title/sub-label, keeps Play button visible', async ({ page }) => {
        await page.setViewportSize({ width: 375, height: 812 });
        await loginAs(page, USER.mobile, USER.password);
        await page.goto(`${BASE_URL}/`);

        const card = page.locator('[data-testid="play-video-karera-card"]');
        await expect(card).toBeVisible();
        await expect(card.locator('[data-testid="game-card-title"]')).toBeHidden();
        await expect(card.locator('[data-testid="game-card-sublabel"]')).toBeHidden();
        await expect(card.getByText('Play →')).toBeVisible();
    });

    test('desktop viewport shows game title and sub-label', async ({ page }) => {
        await page.setViewportSize({ width: 1280, height: 800 });
        await loginAs(page, USER.mobile, USER.password);
        await page.goto(`${BASE_URL}/`);

        const card = page.locator('[data-testid="play-video-karera-card"]');
        await expect(card).toBeVisible();
        await expect(card.locator('[data-testid="game-card-title"]')).toBeVisible();
        await expect(card.locator('[data-testid="game-card-sublabel"]')).toBeVisible();
        await expect(card.getByText('Play →')).toBeVisible();
    });

});

test.describe('Home — Pending Status API', () => {

    test('GET /home/pending-status returns pendingCount and pendingWithdrawalCount', async ({ page }) => {
        await loginAs(page, USER.mobile, USER.password);
        const response = await page.request.get(`${BASE_URL}/home/pending-status`);
        expect(response.status()).toBe(200);
        const body = await response.json();
        expect(body).toHaveProperty('pendingCount');
        expect(body).toHaveProperty('pendingWithdrawalCount');
        expect(typeof body.pendingCount).toBe('number');
        expect(typeof body.pendingWithdrawalCount).toBe('number');
    });

});
