const { test, expect } = require('@playwright/test');

// Helper to clear localStorage and get a fresh editor
async function freshEditor(page) {
    // First load to clear state
    await page.goto('/', { waitUntil: 'domcontentloaded' });
    await page.evaluate(() => { localStorage.clear(); });
    // Second load with clean state
    await page.goto('/', { waitUntil: 'domcontentloaded' });
    // Wait for GrapesJS editor to be ready
    await page.waitForFunction(() => {
        try { return window.editor && typeof window.editor.getWrapper === 'function'; }
        catch(e) { return false; }
    }, { timeout: 50000, polling: 1000 });
    await page.waitForTimeout(2000);
}

test.describe('Feature #8: Block Icons', () => {
    test('all blocks should have visible icons', async ({ page }) => {
        await freshEditor(page);
        
        // Check blocks panel is visible
        const blocksContainer = page.locator('#blocks-container');
        await expect(blocksContainer).toBeVisible();
        
        // Check specific blocks exist with their icons
        // Section block
        const sectionBlock = page.locator('.gjs-block[title="Section"], .gjs-block:has-text("Section")').first();
        await expect(sectionBlock).toBeVisible();
        
        // Spacer block
        const spacerBlock = page.locator('.gjs-block:has-text("Spacer")').first();
        await expect(spacerBlock).toBeVisible();
        
        // Newsletter block
        const newsletterBlock = page.locator('.gjs-block:has-text("Newsletter")').first();
        // May need to scroll to Sections category
        if (await newsletterBlock.isVisible()) {
            await expect(newsletterBlock).toBeVisible();
        }
    });
});

test.describe('Feature #1: Anchor Points & Link System', () => {
    test('anchor point block exists in blocks panel', async ({ page }) => {
        await freshEditor(page);
        
        // Look for Anchor Point block
        const anchorBlock = page.locator('.gjs-block:has-text("Anchor Point")').first();
        // Scroll block panel if needed
        const blocksContainer = page.locator('#blocks-container');
        await blocksContainer.evaluate(el => el.scrollTop = el.scrollHeight);
        await page.waitForTimeout(300);
        
        // Check it exists somewhere in blocks
        const blockCount = await page.locator('.gjs-block:has-text("Anchor")').count();
        expect(blockCount).toBeGreaterThan(0);
    });

    test('link type selector appears for link elements', async ({ page }) => {
        await freshEditor(page);
        
        // Click on the Get Started button in default content
        const frame = page.frameLocator('.gjs-frame');
        const link = frame.locator('a').first();
        await link.click();
        await page.waitForTimeout(500);
        
        // Check link type selector exists
        const linkTypeSelect = page.locator('#link-type-select');
        await expect(linkTypeSelect).toBeVisible();
        
        // Check it has options
        const options = await linkTypeSelect.locator('option').allTextContents();
        expect(options).toContain('External URL');
        expect(options).toContain('Page Link');
        expect(options).toContain('Anchor on Page');
    });
});

test.describe('Feature #2: Asset Manager', () => {
    test('assets tab exists in left panel', async ({ page }) => {
        await freshEditor(page);
        
        const assetsTab = page.locator('.panel-tab:has-text("Assets")');
        await expect(assetsTab).toBeVisible();
        
        // Click it
        await assetsTab.click();
        await page.waitForTimeout(300);
        
        // Upload button should be visible
        const uploadBtn = page.locator('#asset-upload-btn');
        await expect(uploadBtn).toBeVisible();
        
        // URL input should be visible
        const urlInput = page.locator('#asset-url-input');
        await expect(urlInput).toBeVisible();
    });

    test('can add asset by URL', async ({ page }) => {
        await freshEditor(page);
        
        // Switch to assets tab
        await page.locator('.panel-tab:has-text("Assets")').click();
        await page.waitForTimeout(300);
        
        // Enter URL
        await page.fill('#asset-url-input', 'https://example.com/test-image.jpg');
        await page.click('#asset-add-url-btn');
        await page.waitForTimeout(300);
        
        // Check asset grid has item
        const assetItems = page.locator('#assets-grid > div');
        expect(await assetItems.count()).toBeGreaterThan(0);
    });
});

test.describe('Feature #4: Video Element Fix', () => {
    test('video block exists and has correct attributes', async ({ page }) => {
        await freshEditor(page);
        
        // Find video block in blocks panel
        const videoBlock = page.locator('.gjs-block:has-text("Video")').first();
        await expect(videoBlock).toBeVisible();
    });

    test('video wrapper component type is registered with traits', async ({ page }) => {
        await freshEditor(page);
        
        // Check that video-wrapper component type is registered
        const hasVideoType = await page.evaluate(() => {
            const types = window.editor.DomComponents.getTypes();
            return types.some(t => t.id === 'video-wrapper');
        });
        expect(hasVideoType).toBe(true);
        
        // Check that video-section type is also registered  
        const hasVideoSectionType = await page.evaluate(() => {
            const types = window.editor.DomComponents.getTypes();
            return types.some(t => t.id === 'video-section');
        });
        expect(hasVideoSectionType).toBe(true);
        
        // Check that the video block exists in BlockManager (registered as 'video-block')
        const hasVideoBlock = await page.evaluate(() => {
            const block = window.editor.BlockManager.get('video-block');
            return block !== null && block !== undefined;
        });
        expect(hasVideoBlock).toBe(true);
    });
});

test.describe('Feature #5: Delete Section', () => {
    test('delete section option exists in context menu HTML', async ({ page }) => {
        await freshEditor(page);
        
        // Check context menu has delete-section option
        const deleteSectionItem = page.locator('[data-action="delete-section"]');
        // It's hidden but should exist in DOM
        expect(await deleteSectionItem.count()).toBe(1);
    });
});

test.describe('Feature #6: Head/Site-wide Elements', () => {
    test('head tab exists in right panel', async ({ page }) => {
        await freshEditor(page);
        
        const headTab = page.locator('.panel-right .panel-tab:has-text("Head")');
        await expect(headTab).toBeVisible();
        
        await headTab.click();
        await page.waitForTimeout(300);
        
        // Head code textarea should be visible
        const headTextarea = page.locator('#head-code-textarea');
        await expect(headTextarea).toBeVisible();
        
        // Site-wide CSS textarea should be visible
        const cssTextarea = page.locator('#sitewide-css-textarea');
        await expect(cssTextarea).toBeVisible();
    });

    test('can save head code', async ({ page }) => {
        await freshEditor(page);
        
        await page.locator('.panel-right .panel-tab:has-text("Head")').click();
        await page.waitForTimeout(300);
        
        await page.fill('#head-code-textarea', '<meta name="test" content="value">');
        
        // Handle the alert
        page.on('dialog', dialog => dialog.accept());
        await page.click('#head-code-apply');
        await page.waitForTimeout(300);
        
        // Verify it's saved in localStorage
        const saved = await page.evaluate(() => localStorage.getItem('sitebuilder-head-code'));
        expect(saved).toContain('meta name="test"');
    });
});

test.describe('Feature #7: PDF/File Display Element', () => {
    test('file embed block exists', async ({ page }) => {
        await freshEditor(page);
        
        // Look for File/PDF block
        const fileBlock = page.locator('.gjs-block:has-text("File")').first();
        // Might need to check in Media category
        const blockCount = await page.locator('.gjs-block:has-text("PDF"), .gjs-block:has-text("File")').count();
        expect(blockCount).toBeGreaterThan(0);
    });
});

test.describe('Feature #9: Typography Advanced Settings', () => {
    test('advanced typography controls work', async ({ page }) => {
        await freshEditor(page);
        
        // Select a text element
        const frame = page.frameLocator('.gjs-frame');
        const heading = frame.locator('h1').first();
        await heading.click();
        await page.waitForTimeout(500);
        
        // Switch to Advanced mode
        await page.locator('#mode-advanced').click();
        await page.waitForTimeout(300);
        
        // Check Typography sector exists
        const typographySector = page.locator('.gjs-sm-sector:has-text("Typography")');
        if (await typographySector.count() > 0) {
            await typographySector.click();
            await page.waitForTimeout(300);
            
            // Check for font-family select
            const fontSelect = page.locator('#advanced-styles .gjs-sm-property:has-text("font-family"), #advanced-styles select').first();
            expect(await fontSelect.count()).toBeGreaterThanOrEqual(0); // May be rendered differently
        }
    });
});

test.describe('Feature #10: Logo Element Improvement', () => {
    test('logo block has traits for image and text modes', async ({ page }) => {
        await freshEditor(page);
        
        // Add a logo block
        await page.evaluate(() => {
            const block = window.editor.BlockManager.get('logo');
            if (block) {
                window.editor.addComponents(block.get('content'));
            }
        });
        await page.waitForTimeout(500);
        
        // Select the logo
        await page.evaluate(() => {
            const wrapper = window.editor.getWrapper();
            const logo = wrapper.find('.site-logo')[0];
            if (logo) window.editor.select(logo);
        });
        await page.waitForTimeout(500);
        
        // Switch to Settings tab
        await page.locator('.panel-right .panel-tab:has-text("Settings")').click();
        await page.waitForTimeout(300);
        
        // Check for logo traits
        const traitsContainer = page.locator('#traits-container');
        const hasLogoText = await traitsContainer.locator('text=Logo Text').count();
        const hasLogoImage = await traitsContainer.locator('text=Logo Image').count();
        const hasLogoMode = await traitsContainer.locator('text=Logo Mode').count();
        
        expect(hasLogoText).toBeGreaterThan(0);
        expect(hasLogoImage).toBeGreaterThan(0);
        expect(hasLogoMode).toBeGreaterThan(0);
    });
});

test.describe('Feature #3: Image Resize PHP Backend', () => {
    test('PHP resize script file exists', async ({ page }) => {
        await freshEditor(page);
        // Verify the file exists by checking a fetch response (not navigating)
        const status = await page.evaluate(async () => {
            try {
                const resp = await fetch('/api/image-resize.php', { method: 'HEAD' });
                return resp.status;
            } catch(e) { return -1; }
        });
        // 200 means the file exists (even if PHP isn't running, the file is served)
        expect(status).toBe(200);
    });
});

test.describe('Overall UI', () => {
    test('editor loads without errors', async ({ page }) => {
        const errors = [];
        page.on('pageerror', err => errors.push(err.message));
        
        await freshEditor(page);
        
        // Filter out non-critical errors
        const criticalErrors = errors.filter(e => 
            !e.includes('ResizeObserver') && 
            !e.includes('Script error') &&
            !e.includes('net::')
        );
        
        expect(criticalErrors.length).toBe(0);
    });

    test('all panel tabs work', async ({ page }) => {
        await freshEditor(page);
        
        // Left panel tabs
        for (const tabName of ['Blocks', 'Pages', 'Layers', 'Assets']) {
            const tab = page.locator(`.panel-left .panel-tab:has-text("${tabName}")`);
            if (await tab.count() > 0) {
                await tab.click();
                await page.waitForTimeout(200);
            }
        }
        
        // Right panel tabs
        for (const tabName of ['Styles', 'Settings', 'Head']) {
            const tab = page.locator(`.panel-right .panel-tab:has-text("${tabName}")`);
            if (await tab.count() > 0) {
                await tab.click();
                await page.waitForTimeout(200);
            }
        }
    });

    test('screenshot of editor with features', async ({ page }) => {
        await freshEditor(page);
        await page.screenshot({ path: 'tests/screenshots/editor-overview.png', fullPage: false });
        
        // Show assets tab
        await page.locator('.panel-tab:has-text("Assets")').click();
        await page.waitForTimeout(300);
        await page.screenshot({ path: 'tests/screenshots/assets-tab.png', fullPage: false });
        
        // Show head tab
        await page.locator('.panel-right .panel-tab:has-text("Head")').click();
        await page.waitForTimeout(300);
        await page.screenshot({ path: 'tests/screenshots/head-elements.png', fullPage: false });
        
        // Select a link element for link type selector
        await page.locator('.panel-tab:has-text("Blocks")').click();
        const frame = page.frameLocator('.gjs-frame');
        const link = frame.locator('a').first();
        await link.click();
        await page.waitForTimeout(500);
        await page.locator('.panel-right .panel-tab:has-text("Styles")').click();
        await page.waitForTimeout(300);
        await page.screenshot({ path: 'tests/screenshots/link-settings.png', fullPage: false });
    });
});
