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

test.describe('WFBO Karera', () => {

    test.describe('access control', () => {

        test('operations user can access the Karera hub', async ({ page }) => {
            await loginAs(page, OPERATIONS.mobile, OPERATIONS.password);
            await page.goto(`${BASE_URL}/wfbo/games/karera`);

            await expect(page.locator('[data-testid="wfbo-sidebar"]')).toBeVisible();
            await expect(page.locator('[data-testid="karera-hub-horses"]')).toBeVisible();
        });

        test('operations user can access horses list', async ({ page }) => {
            await loginAs(page, OPERATIONS.mobile, OPERATIONS.password);
            await page.goto(`${BASE_URL}/wfbo/karera/horses`);

            await expect(page.locator('[data-testid="wfbo-sidebar"]')).toBeVisible();
        });

        test('regular user cannot access WFBO Karera', async ({ page }) => {
            await loginAs(page, USER.mobile, USER.password);
            await page.goto(`${BASE_URL}/wfbo/karera/horses`);

            await expect(page.locator('[data-testid="wfbo-sidebar"]')).not.toBeVisible();
        });

        test('agent cannot access WFBO Karera', async ({ page }) => {
            await loginAs(page, AGENT.mobile, AGENT.password);
            await page.goto(`${BASE_URL}/wfbo/karera/horses`);

            await expect(page.locator('[data-testid="wfbo-sidebar"]')).not.toBeVisible();
        });

        test('unauthenticated user is redirected away from Karera', async ({ page }) => {
            await page.context().clearCookies();
            await page.goto(`${BASE_URL}/wfbo/karera/horses`);

            expect(page.url()).not.toContain('/wfbo/karera/horses');
        });

        test('WFBO sidebar has Karera nav link', async ({ page }) => {
            await loginAs(page, OPERATIONS.mobile, OPERATIONS.password);
            await page.goto(`${BASE_URL}/wfbo/games/karera`);

            await expect(page.locator('[data-testid="wfbo-nav-game-hub-kr"]')).toBeVisible();
        });

    });

    for (const entity of ['horse', 'jockey', 'trainer', 'arena'] as const) {
        const plural = `${entity}s`;

        test.describe(`CRUD as operations: ${entity}`, () => {

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

            test(`${plural} list page loads with Add New button`, async ({ page }) => {
                await page.goto(`${BASE_URL}/wfbo/karera/${plural}`);

                await expect(page.locator('[data-testid="wfbo-sidebar"]')).toBeVisible();
                await expect(page.locator(`[data-testid="btn-new-${entity}"]`)).toBeVisible();
            });

            test(`new ${entity} form loads`, async ({ page }) => {
                await page.goto(`${BASE_URL}/wfbo/karera/${plural}/new`);

                await expect(page.locator(`[data-testid="${entity}-form"]`)).toBeVisible();
                await expect(page.locator('[data-testid="input-name"]')).toBeVisible();
                await expect(page.locator('[data-testid="btn-submit"]')).toBeVisible();
            });

            test(`can create a new ${entity}`, async ({ page }) => {
                const name = `Test ${entity} ${Date.now()}`;

                await page.goto(`${BASE_URL}/wfbo/karera/${plural}/new`);
                await page.fill('[data-testid="input-name"]', name);
                await page.click('[data-testid="btn-submit"]');

                await expect(page).toHaveURL(`${BASE_URL}/wfbo/karera/${plural}`);
                await expect(page.locator('[data-testid="flash-success"]')).toBeVisible();
                await expect(page.locator(`[data-testid="${plural}-table"]`)).toBeVisible();
            });

            test('submitting empty form shows errors', async ({ page }) => {
                await page.goto(`${BASE_URL}/wfbo/karera/${plural}/new`);
                await page.click('[data-testid="btn-submit"]');

                await expect(page).toHaveURL(`${BASE_URL}/wfbo/karera/${plural}/new`);
                await expect(page.locator('[data-testid="flash-error"]').first()).toBeVisible();
            });

            test(`can edit an existing ${entity}`, async ({ page }) => {
                const name = `Edit Test ${entity} ${Date.now()}`;
                await page.goto(`${BASE_URL}/wfbo/karera/${plural}/new`);
                await page.fill('[data-testid="input-name"]', name);
                await page.click('[data-testid="btn-submit"]');

                await expect(page).toHaveURL(`${BASE_URL}/wfbo/karera/${plural}`);

                const row = page.locator('.table-row', { hasText: name });
                const editBtn = row.locator('[data-testid^="btn-edit-"]');
                if (await editBtn.count() === 0) {
                    test.skip();
                    return;
                }

                await editBtn.click();
                await expect(page.locator(`[data-testid="${entity}-form"]`)).toBeVisible();

                await page.fill('[data-testid="input-name"]', `${name} Updated`);
                await page.click('[data-testid="btn-submit"]');

                await expect(page).toHaveURL(`${BASE_URL}/wfbo/karera/${plural}`);
                await expect(page.locator('[data-testid="flash-success"]')).toBeVisible();
            });

            test(`can delete a ${entity}`, async ({ page }) => {
                const name = `Delete Test ${entity} ${Date.now()}`;
                await page.goto(`${BASE_URL}/wfbo/karera/${plural}/new`);
                await page.fill('[data-testid="input-name"]', name);
                await page.click('[data-testid="btn-submit"]');

                await expect(page).toHaveURL(`${BASE_URL}/wfbo/karera/${plural}`);

                const row = page.locator('.table-row', { hasText: name });
                const deleteBtn = row.locator('[data-testid^="btn-delete-"]');
                if (await deleteBtn.count() === 0) {
                    test.skip();
                    return;
                }

                page.once('dialog', dialog => dialog.accept());
                await deleteBtn.click();

                await expect(page).toHaveURL(`${BASE_URL}/wfbo/karera/${plural}`);
                await expect(page.locator('[data-testid="flash-success"]')).toBeVisible();
            });

        });
    }

});
