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

const TEST_IMAGE = path.join(__dirname, '..', 'fixtures', 'test-banner.png');

test.describe('WFBO Live Racing Ad Banners', () => {

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

        test('admin can access live racing ad banners page', async ({ page }) => {
            await loginAs(page, ADMIN.mobile, ADMIN.password);
            await page.goto(`${BASE_URL}/wfbo/live-racing/ad-banners`);

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

        test('finance-only user cannot access WFBO live racing ad banners', async ({ page }) => {
            await loginAs(page, FINANCE.mobile, FINANCE.password);
            await page.goto(`${BASE_URL}/wfbo/live-racing/ad-banners`);

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

        test('regular user cannot access WFBO live racing ad banners', async ({ page }) => {
            await loginAs(page, USER.mobile, USER.password);
            await page.goto(`${BASE_URL}/wfbo/live-racing/ad-banners`);

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

        test('agent cannot access WFBO live racing ad banners', async ({ page }) => {
            await loginAs(page, AGENT.mobile, AGENT.password);
            await page.goto(`${BASE_URL}/wfbo/live-racing/ad-banners`);

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

        test('unauthenticated user is redirected away from live racing ad banners', async ({ page }) => {
            await page.context().clearCookies();
            await page.goto(`${BASE_URL}/wfbo/live-racing/ad-banners`);

            expect(page.url()).not.toContain('/wfbo/live-racing/ad-banners');
        });

    });

    test.describe('CRUD as admin', () => {

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

        test('ad banners page loads with Add New button', async ({ page }) => {
            await page.goto(`${BASE_URL}/wfbo/live-racing/ad-banners`);

            await expect(page.locator('[data-testid="wfbo-sidebar"]')).toBeVisible();
            await expect(page.locator('[data-testid="btn-new-live-racing-ad-banner"]')).toBeVisible();
        });

        test('new ad banner form loads', async ({ page }) => {
            await page.goto(`${BASE_URL}/wfbo/live-racing/ad-banners/new`);

            await expect(page.locator('[data-testid="live-racing-ad-banner-form"]')).toBeVisible();
            await expect(page.locator('[data-testid="input-title"]')).toBeVisible();
            await expect(page.locator('[data-testid="input-image"]')).toBeVisible();
            await expect(page.locator('[data-testid="input-link-url"]')).toBeVisible();
            await expect(page.locator('[data-testid="btn-submit"]')).toBeVisible();
        });

        test('can create a new ad banner', async ({ page }) => {
            const title = `Test Ad Banner ${Date.now()}`;

            await page.goto(`${BASE_URL}/wfbo/live-racing/ad-banners/new`);
            await page.fill('[data-testid="input-title"]', title);
            await page.setInputFiles('[data-testid="input-image"]', TEST_IMAGE);
            await page.fill('[data-testid="input-link-url"]', 'https://example.com/promo');
            await page.click('[data-testid="btn-submit"]');

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

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

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

        test('can edit an existing ad banner', async ({ page }) => {
            const title = `Edit Test ${Date.now()}`;
            await page.goto(`${BASE_URL}/wfbo/live-racing/ad-banners/new`);
            await page.fill('[data-testid="input-title"]', title);
            await page.setInputFiles('[data-testid="input-image"]', TEST_IMAGE);
            await page.click('[data-testid="btn-submit"]');

            await expect(page).toHaveURL(`${BASE_URL}/wfbo/live-racing/ad-banners`);

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

            await editBtn.click();
            await expect(page.locator('[data-testid="live-racing-ad-banner-form"]')).toBeVisible();

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

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

        test('can delete an ad banner', async ({ page }) => {
            const title = `Delete Test ${Date.now()}`;
            await page.goto(`${BASE_URL}/wfbo/live-racing/ad-banners/new`);
            await page.fill('[data-testid="input-title"]', title);
            await page.setInputFiles('[data-testid="input-image"]', TEST_IMAGE);
            await page.click('[data-testid="btn-submit"]');

            await expect(page).toHaveURL(`${BASE_URL}/wfbo/live-racing/ad-banners`);

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

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

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

        test('can disable and re-enable an ad banner', async ({ page }) => {
            const title = `Toggle Test ${Date.now()}`;
            await page.goto(`${BASE_URL}/wfbo/live-racing/ad-banners/new`);
            await page.fill('[data-testid="input-title"]', title);
            await page.setInputFiles('[data-testid="input-image"]', TEST_IMAGE);
            await page.click('[data-testid="btn-submit"]');

            await expect(page).toHaveURL(`${BASE_URL}/wfbo/live-racing/ad-banners`);

            const toggleBtn = page.locator('[data-testid^="btn-toggle-"]').last();
            if (await toggleBtn.count() === 0) {
                test.skip();
                return;
            }

            page.on('dialog', dialog => dialog.accept());
            await toggleBtn.click();
            await expect(page.locator('[data-testid="flash-success"]')).toBeVisible();
        });

        test('the Clicks column increments after a click is tracked', async ({ page }) => {
            const title = `Click Count Test ${Date.now()}`;
            await page.goto(`${BASE_URL}/wfbo/live-racing/ad-banners/new`);
            await page.fill('[data-testid="input-title"]', title);
            await page.setInputFiles('[data-testid="input-image"]', TEST_IMAGE);
            await page.fill('[data-testid="input-link-url"]', 'https://example.com/promo');
            await page.click('[data-testid="btn-submit"]');
            await expect(page).toHaveURL(`${BASE_URL}/wfbo/live-racing/ad-banners`);

            const row = page.locator('[data-testid^="live-racing-ad-banner-row-"]', { hasText: title });
            const rowId = (await row.getAttribute('data-testid'))?.replace('live-racing-ad-banner-row-', '');
            await expect(page.locator(`[data-testid="ad-banner-clicks-${rowId}"]`)).toHaveText('0');

            await page.request.get(`${BASE_URL}/play/live-racing/ad-banners/${rowId}/click`, { maxRedirects: 0 });

            await page.goto(`${BASE_URL}/wfbo/live-racing/ad-banners`);
            await expect(page.locator(`[data-testid="ad-banner-clicks-${rowId}"]`)).toHaveText('1');
        });

        test('WFBO sidebar has live racing ad banners nav link', async ({ page }) => {
            await page.goto(`${BASE_URL}/wfbo/live-racing/ad-banners`);

            await expect(page.locator('[data-testid="wfbo-nav-live-racing-ad-banners"]')).toBeVisible();
        });

    });

});
