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

test.describe('WFBO Loaders page — ADMIN access', () => {

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

    test('page loads with the loaders breakdown table and a Total row', async ({ page }) => {
        await page.goto(`${BASE_URL}/wfbo/loaders`);
        await expect(page.locator('[data-testid="wfbo-page-title"]')).toHaveText('Loaders');

        const table = page.locator('[data-testid="loaders-breakdown"]');
        await expect(table).toBeVisible();
        await expect(table).toContainText('Approved Deposits');
        await expect(page.locator('[data-testid="loaders-row-unassigned"]')).toContainText('Unassigned / Other');

        const totalRow = page.locator('[data-testid="loaders-row-total"]');
        await expect(totalRow).toContainText('Total');
        await expect(totalRow).toContainText('₱');
    });

    test('period filter pills are present and default to Today', async ({ page }) => {
        await page.goto(`${BASE_URL}/wfbo/loaders`);
        await expect(page.locator('[data-testid="period-this_month"]').first()).toBeVisible();
        await expect(page.locator('[data-testid="period-today"]')).toHaveClass(/is-active/);
    });

    test('the Loaders nav link is under the Finance group', async ({ page }) => {
        await page.goto(`${BASE_URL}/wfbo/loaders`);
        await expect(page.locator('[data-testid="wfbo-nav-loaders"]')).toBeVisible();
    });

    test('the loaders total equals the sum of every row', async ({ page }) => {
        await page.goto(`${BASE_URL}/wfbo/loaders`);
        const peso = async (loc: ReturnType<typeof page.locator>) =>
            Number(((await loc.textContent()) ?? '').replace(/[^0-9.-]/g, '') || '0');

        const rowCells = page.locator('[data-testid^="loaders-row-"]:not([data-testid="loaders-row-total"]) td:last-child');
        let sum = 0;
        for (let i = 0; i < await rowCells.count(); i++) sum += await peso(rowCells.nth(i));

        const total = await peso(page.locator('[data-testid="loaders-row-total"] td:last-child'));
        expect(Math.round(total * 100)).toBe(Math.round(sum * 100));
    });

});

test.describe('WFBO Loaders page — role guard', () => {

    test('a FINANCE user has no Loaders nav link and is blocked from the page', async ({ page }) => {
        await loginAs(page, FINANCE.mobile, FINANCE.password);

        await page.goto(`${BASE_URL}/wfbo/deposits`);
        await expect(page.locator('[data-testid="wfbo-nav-deposits"]')).toBeVisible();
        await expect(page.locator('[data-testid="wfbo-nav-loaders"]')).toHaveCount(0);

        await page.goto(`${BASE_URL}/wfbo/loaders`);
        await expect(page.locator('[data-testid="loaders-breakdown"]')).not.toBeVisible();
    });

    test('an OPERATIONS user is blocked from the page', async ({ page }) => {
        await loginAs(page, OPERATIONS.mobile, OPERATIONS.password);
        await page.goto(`${BASE_URL}/wfbo/loaders`);
        await expect(page.locator('[data-testid="loaders-breakdown"]')).not.toBeVisible();
    });

});
