#!/usr/bin/env node

/**
 * UI Tests for Collections Page
 * Tests the collections management functionality in the Library.
 */

const puppeteer = require('puppeteer');
const AuthHelper = require('../auth_helper');
const { getPuppeteerLaunchOptions } = require('../puppeteer_config');

const BASE_URL = process.env.TEST_BASE_URL || 'http://127.0.0.1:5000';
const TIMEOUT = 30000;

// Helper function for delays (Puppeteer doesn't have waitForTimeout like Playwright)
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));

async function runTests() {
    console.log('🧪 Testing Collections Page UI\n');

    let browser;
    let page;
    let testsPassed = 0;
    let testsFailed = 0;

    try {
        browser = await puppeteer.launch(getPuppeteerLaunchOptions());

        // Test 1: Display collections page
        try {
            console.log('Test 1: Should display collections page');
            page = await browser.newPage();
            page.setDefaultTimeout(TIMEOUT);
            const authHelper = new AuthHelper(page, BASE_URL);

            await page.goto(BASE_URL, { waitUntil: 'domcontentloaded' });
            await authHelper.ensureAuthenticatedWithTimeout();
            await page.goto(`${BASE_URL}/library/`, { waitUntil: 'domcontentloaded' });

            const pageTitle = await page.title();
            if (!pageTitle.toLowerCase().includes('library') && !pageTitle.toLowerCase().includes('collection')) {
                throw new Error(`Expected page title to contain 'library' or 'collection', got: ${pageTitle}`);
            }

            await page.close();
            console.log('✅ Test 1 passed\n');
            testsPassed++;
        } catch (error) {
            console.error('❌ Test 1 failed:', error.message, '\n');
            testsFailed++;
            if (page) await page.close();
        }

        // Test 2: Show default Library collection
        try {
            console.log('Test 2: Should show default Library collection');
            page = await browser.newPage();
            page.setDefaultTimeout(TIMEOUT);
            const authHelper = new AuthHelper(page, BASE_URL);

            await page.goto(BASE_URL, { waitUntil: 'domcontentloaded' });
            await authHelper.ensureAuthenticatedWithTimeout();
            await page.goto(`${BASE_URL}/library/`, { waitUntil: 'domcontentloaded' });

            // Wait a bit for page to fully load
            await delay(1000);

            const pageContent = await page.content();
            if (!pageContent.toLowerCase().includes('library') && !pageContent.toLowerCase().includes('collection')) {
                throw new Error('Expected page to contain library/collection content');
            }

            await page.close();
            console.log('✅ Test 2 passed\n');
            testsPassed++;
        } catch (error) {
            console.error('❌ Test 2 failed:', error.message, '\n');
            testsFailed++;
            if (page) await page.close();
        }

        // Test 3: Display collection metadata
        try {
            console.log('Test 3: Should display collection metadata');
            page = await browser.newPage();
            page.setDefaultTimeout(TIMEOUT);
            const authHelper = new AuthHelper(page, BASE_URL);

            await page.goto(BASE_URL, { waitUntil: 'domcontentloaded' });
            await authHelper.ensureAuthenticatedWithTimeout();
            await page.goto(`${BASE_URL}/library/`, { waitUntil: 'domcontentloaded' });

            await delay(1000);
            const pageContent = await page.content();

            if (pageContent.length < 100) {
                throw new Error('Page content appears to be empty');
            }

            await page.close();
            console.log('✅ Test 3 passed\n');
            testsPassed++;
        } catch (error) {
            console.error('❌ Test 3 failed:', error.message, '\n');
            testsFailed++;
            if (page) await page.close();
        }

        // Test 4: Open create collection modal
        try {
            console.log('Test 4: Should open create collection modal');
            page = await browser.newPage();
            page.setDefaultTimeout(TIMEOUT);
            const authHelper = new AuthHelper(page, BASE_URL);

            await page.goto(BASE_URL, { waitUntil: 'domcontentloaded' });
            await authHelper.ensureAuthenticatedWithTimeout();
            await page.goto(`${BASE_URL}/library/`, { waitUntil: 'domcontentloaded' });

            await delay(1000);

            const createButton = await page.$(
                'button[data-action="create-collection"], .create-collection-btn, [onclick*="createCollection"], button.btn-primary'
            );

            if (createButton) {
                await createButton.click();
                await page.waitForSelector('.modal, [role="dialog"]', { timeout: 5000 });
                console.log('✅ Test 4 passed\n');
                testsPassed++;
            } else {
                console.log('⚠️  Test 4 skipped: Create button not found (may not be available in this view)\n');
            }

            await page.close();
        } catch (error) {
            console.log('⚠️  Test 4 skipped:', error.message, '\n');
            if (page) await page.close();
        }

        // Test 5: Collection details
        try {
            console.log('Test 5: Should open collection details');
            page = await browser.newPage();
            page.setDefaultTimeout(TIMEOUT);
            const authHelper = new AuthHelper(page, BASE_URL);

            await page.goto(BASE_URL, { waitUntil: 'domcontentloaded' });
            await authHelper.ensureAuthenticatedWithTimeout();
            await page.goto(`${BASE_URL}/library/`, { waitUntil: 'domcontentloaded' });

            await delay(1000);

            const collectionLink = await page.$('.collection-item a, [data-collection-id], a[href*="collection"]');
            if (collectionLink) {
                await Promise.race([
                    collectionLink.click(),
                    delay(2000)
                ]);
                await delay(1000);

                console.log('✅ Test 5 passed\n');
                testsPassed++;
            } else {
                console.log('⚠️  Test 5 skipped: No collection links found\n');
            }

            await page.close();
        } catch (error) {
            console.log('⚠️  Test 5 skipped:', error.message, '\n');
            if (page) await page.close();
        }

        // Test 6: Documents list
        try {
            console.log('Test 6: Should display documents list');
            page = await browser.newPage();
            page.setDefaultTimeout(TIMEOUT);
            const authHelper = new AuthHelper(page, BASE_URL);

            await page.goto(BASE_URL, { waitUntil: 'domcontentloaded' });
            await authHelper.ensureAuthenticatedWithTimeout();
            await page.goto(`${BASE_URL}/library/`, { waitUntil: 'domcontentloaded' });

            await delay(1000);

            const pageLoaded = await page.$('body');
            if (!pageLoaded) {
                throw new Error('Page did not load');
            }

            await page.close();
            console.log('✅ Test 6 passed\n');
            testsPassed++;
        } catch (error) {
            console.error('❌ Test 6 failed:', error.message, '\n');
            testsFailed++;
            if (page) await page.close();
        }

    } catch (error) {
        console.error('💥 Fatal error:', error);
        testsFailed++;
    } finally {
        if (browser) {
            await browser.close();
        }
    }

    // Print summary
    console.log('='.repeat(50));
    console.log(`✅ Tests passed: ${testsPassed}`);
    console.log(`❌ Tests failed: ${testsFailed}`);
    console.log('='.repeat(50));

    if (testsFailed > 0) {
        process.exit(1);
    }
}

// Run tests
if (require.main === module) {
    runTests().catch((error) => {
        console.error('Fatal error running tests:', error);
        process.exit(1);
    });
}

module.exports = { runTests };
