/**
 * Login feature tests — covers multiple roles.
 *
 * These run WITHOUT saved auth state (fresh browser).
 */
import { test, expect } from '@playwright/test';
import { waitForApp } from './helpers';

const SUPERVISOR = { email: 'supervisor@ramaiahcare.com', password: 'MrRamaiah@2026' };

test.describe('Login', () => {

    test('login page renders with branding', async ({ page }) => {
        await page.goto('/login');

        await expect(page.locator('img[alt]').first()).toBeVisible();
        await expect(page.locator('h1').first()).toBeVisible();
        await expect(page.locator('form')).toBeVisible();
        await expect(page.locator('input[type="email"], input[placeholder*="@"]').first()).toBeVisible();
        await expect(page.locator('input[type="password"]').first()).toBeVisible();
        await expect(page.getByRole('button', { name: 'Sign in', exact: true })).toBeVisible();
    });

    test('invalid credentials show error', async ({ page }) => {
        await page.goto('/login');

        await page.locator('input[type="email"], input[placeholder*="@"]').first().fill('bad@bad.com');
        await page.locator('input[type="password"]').first().fill('wrongpassword');
        await page.getByRole('button', { name: 'Sign in', exact: true }).click();

        await expect(
            page.locator('text=/not found|invalid|incorrect|failed|error|unauthorized/i').first(),
        ).toBeVisible({ timeout: 10_000 });
        await expect(page).toHaveURL(/\/login/);
    });

    test('Supervisor login → lands on app', async ({ page }) => {
        await page.goto('/login');

        await page.locator('input[type="email"], input[placeholder*="@"]').first().fill(SUPERVISOR.email);
        await page.locator('input[type="password"]').first().fill(SUPERVISOR.password);
        await page.getByRole('button', { name: 'Sign in', exact: true }).click();

        await expect(page).not.toHaveURL(/\/login/, { timeout: 20_000 });
        await waitForApp(page);

        // Sidebar should be visible
        await expect(page.locator('aside').first()).toBeVisible();
    });

    test('unauthenticated user redirected to login', async ({ page }) => {
        await page.context().clearCookies();
        await page.goto('/patients');
        await expect(page).toHaveURL(/\/login/, { timeout: 10_000 });
    });
});
