import { test, expect } from '@playwright/test';

test.describe('CCLI Bookmarklet', () => {
    test('Settings page shows CCLI section with bookmarklet drag link', async ({ page }) => {
        await page.goto('/settings');
        await page.waitForLoadState('networkidle');

        const ccliBtn = page.locator('[data-testid="settings-submenu-ccli"]').first();
        await ccliBtn.waitFor({ state: 'visible', timeout: 10000 });
        await ccliBtn.click();
        await page.waitForTimeout(500);

        await expect(page.getByTestId('default-translation-language')).toBeVisible();
        await expect(page.getByTestId('ccli-bookmarklet-drag-link')).toBeVisible();
    });

    test('bookmarklet endpoint returns valid JS', async ({ request }) => {
        const res = await request.get('/bookmarklets/ccli-import.js');
        expect(res.status()).toBe(200);
        expect(res.headers()['content-type']).toContain('text/javascript');

        const body = await res.text();
        expect(body).toMatch(/^javascript:/);
        expect(body).toContain('import-from-ccli-paste');
        expect(body).toContain('songselect.ccli.com');
    });

    test('bookmarklet redirect page renders with valid base64 prefill', async ({ page }) => {
        const payload = JSON.stringify({
            title: 'Amazing Grace',
            author: 'John Newton',
            ccliId: '4760',
            sourceUrl: 'https://songselect.ccli.com/Songs/4760',
            rawText: 'Amazing Grace\nJohn Newton\n\nVerse 1\nAmazing grace how sweet the sound\n\n© 1779\nCCLI # 4760',
        });
        const encoded = Buffer.from(payload).toString('base64');

        await page.goto(`/songs/import-from-ccli-paste?prefill=${encoded}`);
        await page.waitForLoadState('networkidle');

        await expect(page.getByTestId('ccli-paste-textarea')).toBeVisible();
    });

    test('bookmarklet redirect page shows error for invalid base64', async ({ page }) => {
        await page.goto('/songs/import-from-ccli-paste?prefill=NOT_VALID_BASE64!!!');
        await page.waitForLoadState('networkidle');

        await expect(page.getByTestId('ccli-prefill-error-message')).toBeVisible();
        await expect(page.getByTestId('ccli-paste-textarea')).toBeVisible();
    });

    test('default language dropdown persists selection', async ({ page }) => {
        await page.goto('/settings');
        await page.waitForLoadState('networkidle');

        const ccliBtn = page.locator('[data-testid="settings-submenu-ccli"]').first();
        await ccliBtn.waitFor({ state: 'visible', timeout: 10000 });
        await ccliBtn.click();
        await page.waitForTimeout(500);

        const select = page.getByTestId('default-translation-language');
        await select.selectOption('EN');
        await page.waitForTimeout(1000);

        await page.reload();
        await page.waitForLoadState('networkidle');
        const ccliBtn2 = page.locator('[data-testid="settings-submenu-ccli"]').first();
        await ccliBtn2.waitFor({ state: 'visible', timeout: 10000 });
        await ccliBtn2.click();
        await page.waitForTimeout(500);

        await expect(page.getByTestId('default-translation-language')).toHaveValue('EN');

        await page.getByTestId('default-translation-language').selectOption('DE');
        await page.waitForTimeout(500);
    });
});
