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

type RoleScenario = {
  roleKey: string;
  schemaId: string;
  profession?: string;
  expectedBadge: string;
};

const ROLE_SCENARIOS: RoleScenario[] = [
  { roleKey: 'COMPANY', schemaId: 'company_onboarding_v1', expectedBadge: 'Company' },
  { roleKey: 'JOB_SEEKER', schemaId: 'jobseeker_onboarding_v1', expectedBadge: 'Job Seeker' },
  { roleKey: 'CUSTOMER', schemaId: 'customer_onboarding_v1', expectedBadge: 'Customer' },
  { roleKey: 'PHOTOGRAPHER', schemaId: 'photographer_onboarding_v1', profession: 'photographer', expectedBadge: 'Photographer' },
  { roleKey: 'MAKEUP_ARTIST', schemaId: 'makeup_artist_onboarding_v1', profession: 'makeup_artist', expectedBadge: 'Makeup Artist' },
  { roleKey: 'TUTOR', schemaId: 'tutor_onboarding_v1', profession: 'tutor', expectedBadge: 'Tutor' },
  { roleKey: 'DEVELOPER', schemaId: 'developer_onboarding_v1', profession: 'developer', expectedBadge: 'Developer' },
  { roleKey: 'VIDEO_EDITOR', schemaId: 'video_editor_onboarding_v1', profession: 'video_editor', expectedBadge: 'Video Editor' },
  { roleKey: 'GRAPHIC_DESIGNER', schemaId: 'graphic_designer_onboarding_v1', profession: 'graphic_designer', expectedBadge: 'Graphic Designer' },
  { roleKey: 'SOCIAL_MEDIA_MANAGER', schemaId: 'social_media_manager_onboarding_v1', profession: 'social_media_manager', expectedBadge: 'Social Media Manager' },
  { roleKey: 'FITNESS_TRAINER', schemaId: 'fitness_trainer_onboarding_v1', profession: 'fitness_trainer', expectedBadge: 'Fitness Trainer' },
  { roleKey: 'CATERING_SERVICES', schemaId: 'catering_services_onboarding_v1', profession: 'catering_services', expectedBadge: 'Catering Services' },
];

test.describe('External roles onboarding + dashboard load checks', () => {
  for (const role of ROLE_SCENARIOS) {
    test(`onboarding page loads for ${role.roleKey}`, async ({ page }) => {
      const params = new URLSearchParams({
        roleKey: role.roleKey,
        schemaId: role.schemaId,
      });
      if (role.profession) {
        params.set('profession', role.profession);
        params.set('intent', 'professional');
      } else if (role.roleKey === 'COMPANY') {
        params.set('intent', 'company');
      } else if (role.roleKey === 'CUSTOMER') {
        params.set('intent', 'customer');
      } else if (role.roleKey === 'JOB_SEEKER') {
        params.set('intent', 'job_seeker');
      }

      await page.goto(`/onboarding?${params.toString()}`, { waitUntil: 'domcontentloaded' });
      await page.waitForTimeout(1200);

      await expect(page).toHaveURL(new RegExp(`/onboarding\\?`));
      await expect(page.locator('body')).not.toContainText('TypeError');
      await expect(page.locator('body')).not.toContainText('Cannot read properties');
      await expect(page.locator('body')).not.toContainText('Not Found (role=');
      await expect(page.locator('body')).not.toContainText('No onboarding schema configured for role');

      const bodyText = await page.locator('body').innerText();
      expect(bodyText.trim().length).toBeGreaterThan(20);
    });

    test(`dashboard loads for ${role.roleKey} preview`, async ({ page }) => {
      await page.goto(`/dashboard?_preview=${encodeURIComponent(role.roleKey)}`, { waitUntil: 'domcontentloaded' });
      await page.waitForTimeout(900);

      await expect(page.locator('body')).not.toContainText('TypeError');
      await expect(page.locator('body')).not.toContainText('Cannot read properties');
      const bodyText = await page.locator('body').innerText();
      expect(bodyText.trim().length).toBeGreaterThan(20);
      await expect(page.locator('body')).toContainText(/dashboard|profile|settings|help center/i);
    });
  }
});
