import { test as base } from '@playwright/test';

type DbFixture = {
    resetDb: () => Promise<void>;
    seedHistory: (data: any[]) => Promise<void>;
    getHistory: () => Promise<any[]>;
    getSyncQueue: () => Promise<any[]>;
};

export const test = base.extend<{ db: DbFixture }>({
    db: async ({ page }, use) => {
        const dbFixture = {
            resetDb: async () => {
                await page.evaluate(async () => {
                    // Assuming 'Dexie' is globally available or we use indexedDB directly
                    // Using indexedDB directly to be safe as Dexie might be bundled
                    const req = indexedDB.deleteDatabase('Test01DB'); // Match your DB name
                    return new Promise((resolve, reject) => {
                        req.onsuccess = () => resolve(true);
                        req.onerror = () => reject(req.error);
                        req.onblocked = () => console.warn('Delete DB blocked');
                    });
                });
            },
            seedHistory: async (data: any[]) => {
                // This is tricky if logic is encapsulated. 
                // We might need to expose a hidden window helper or just rely on UI for seeding in E2E 
                // OR use a specialized seeding script.
                // For Integration/Component, we might assume we can import the DB client directly if running in same context.
                // Since playwight runs in browser context, we'd need to expose it.
                // For now, let's assume we can invoke a window helper if useful, or just implement specific logic
                await page.evaluate(async (items) => {
                    // We'll need the app to expose a way to seed, or we assume specific DB structure
                    // This requires the app to have the DB initialized. 
                    // Implementation deferred to actual test via page.evaluate if needed
                }, data);
            },
            getHistory: async () => {
                // Placeholder for getting history from DB
                return [];
            },
            getSyncQueue: async () => {
                // Placeholder
                return [];
            }
        };

        await use(dbFixture);
    },
});
