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

test.describe('Authentication Flow', () => {
    test.beforeEach(async ({ page }) => {
        await page.goto('/');
    });

    test('should show login page by default', async ({ page }) => {
        // Check if redirected to login if not authenticated
        // The app might redirect to /api/auth/signin or a custom /login
        await expect(page).toHaveURL(/.*auth\/signin|login/);
        await expect(page.locator('button', { hasText: /Sign in|Login/i })).toBeVisible();
    });

    test('should allow user to navigate to registration (if exists)', async ({ page }) => {
        const signUpLink = page.locator('a', { hasText: /Sign up|Register/i });
        if (await signUpLink.isVisible()) {
            await signUpLink.click();
            await expect(page).toHaveURL(/.*signup|register/);
        }
    });

    test('should show error on invalid credentials', async ({ page }) => {
        await page.fill('input[name="email"]', 'wrong@example.com');
        await page.fill('input[name="password"]', 'wrongpassword');
        await page.click('button[type="submit"]');

        // Adjust selector based on actual error message implementation
        await expect(page.locator('text=/Invalid|Error|failed/i')).toBeVisible();
    });
});
