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

const mockUser = {
  id: '00000000-0000-4000-8000-000000000001',
  name: 'E2E User',
  email: 'e2e@mountainview-eg.com',
  locale: 'en',
  company_id: '00000000-0000-4000-8000-000000000002',
  company: {
    id: '00000000-0000-4000-8000-000000000002',
    name: 'Mountain View',
    domain: 'mountainview-eg.com',
    is_active: true,
    created_at: new Date().toISOString(),
    updated_at: new Date().toISOString(),
  },
  departments: [],
  roles: [],
  permissions: ['department.tickets.view'],
  is_active: true,
  created_at: new Date().toISOString(),
  updated_at: new Date().toISOString(),
};

test.describe('Auth and dashboard smoke', () => {
  test.beforeEach(async ({ page }) => {
    await page.route('**/sanctum/csrf-cookie', async (route) => {
      await route.fulfill({ status: 204, body: '' });
    });

    await page.route('**/api/v1/auth/request-otp', async (route) => {
      await route.fulfill({
        status: 200,
        contentType: 'application/json',
        body: JSON.stringify({
          message: 'If your email is registered, you will receive a login code shortly.',
        }),
      });
    });

    await page.route('**/api/v1/auth/verify-otp', async (route) => {
      await route.fulfill({
        status: 200,
        contentType: 'application/json',
        body: JSON.stringify({ data: mockUser }),
      });
    });

    await page.route('**/api/v1/auth/me', async (route) => {
      await route.fulfill({
        status: 200,
        contentType: 'application/json',
        body: JSON.stringify({ data: mockUser }),
      });
    });

    await page.route('**/api/v1/dashboard/overview**', async (route) => {
      await route.fulfill({
        status: 200,
        contentType: 'application/json',
        body: JSON.stringify({
          data: {
            stats: { total: 0, open: 0, resolved: 0, overdue: 0 },
            charts: {},
            recent_activity: [],
          },
        }),
      });
    });

    await page.goto('/login');
  });

  test('OTP login reaches dashboard', async ({ page }) => {
    await page.getByTestId('login-email').fill('e2e@mountainview-eg.com');
    await page.getByTestId('login-send-otp').click();
    await page.getByTestId('login-otp').locator('input[type="text"]').first().pressSequentially('123456');
    await page.getByTestId('login-verify-otp').click();
    await expect(page).toHaveURL(/\/dashboard/);
    await expect(page.getByRole('heading', { name: /dashboard/i })).toBeVisible();
  });
});
