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

test.describe('Admin Login Page', () => {
  test('shows admin login form', async ({ page }) => {
    await page.goto('/login');
    await expect(page.getByText('Platform Admin')).toBeVisible();
    await expect(page.getByText('Sign in to access the admin dashboard')).toBeVisible();
    await expect(page.getByLabel('Email')).toBeVisible();
    await expect(page.getByLabel('Password')).toBeVisible();
  });

  test('shows demo credentials hint', async ({ page }) => {
    await page.goto('/login');
    await expect(page.getByText(/admin@example\.com/)).toBeVisible();
  });

  test('Sign In button is disabled when form is empty', async ({ page }) => {
    await page.goto('/login');
    const btn = page.getByRole('button', { name: 'Sign In' });
    await expect(btn).toBeDisabled();
  });

  test('shows validation hint for short password', async ({ page }) => {
    await page.goto('/login');
    await page.getByLabel('Email').fill('admin@example.com');
    await page.getByLabel('Password').fill('short');
    await expect(page.getByText(/at least 8 characters/i)).toBeVisible();
  });

  test('shows error for invalid credentials', async ({ page }) => {
    await page.goto('/login');
    await page.getByLabel('Email').fill('wrong@admin.com');
    await page.getByLabel('Password').fill('WrongPassword123!');
    await page.getByRole('button', { name: 'Sign In' }).click();
    await expect(page.getByText(/invalid|error/i)).toBeVisible({ timeout: 10000 });
  });

  test('enables Sign In when valid email and password entered', async ({ page }) => {
    await page.goto('/login');
    await page.getByLabel('Email').fill('admin@example.com');
    await page.getByLabel('Password').fill('Admin123!');
    const btn = page.getByRole('button', { name: 'Sign In' });
    await expect(btn).toBeEnabled();
  });
});
