import fs from 'node:fs/promises';
import path from 'node:path';
import { test, expect } from '@playwright/test';

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

const ROLE_SCENARIOS: RoleScenario[] = [
  { roleKey: 'COMPANY', schemaId: 'company_onboarding_v1' },
  { roleKey: 'JOB_SEEKER', schemaId: 'jobseeker_onboarding_v1' },
  { roleKey: 'CUSTOMER', schemaId: 'customer_onboarding_v1' },
  { roleKey: 'PHOTOGRAPHER', schemaId: 'photographer_onboarding_v1', profession: 'photographer' },
  { roleKey: 'MAKEUP_ARTIST', schemaId: 'makeup_artist_onboarding_v1', profession: 'makeup_artist' },
  { roleKey: 'TUTOR', schemaId: 'tutor_onboarding_v1', profession: 'tutor' },
  { roleKey: 'DEVELOPER', schemaId: 'developer_onboarding_v1', profession: 'developer' },
  { roleKey: 'VIDEO_EDITOR', schemaId: 'video_editor_onboarding_v1', profession: 'video_editor' },
  { roleKey: 'GRAPHIC_DESIGNER', schemaId: 'graphic_designer_onboarding_v1', profession: 'graphic_designer' },
  { roleKey: 'SOCIAL_MEDIA_MANAGER', schemaId: 'social_media_manager_onboarding_v1', profession: 'social_media_manager' },
  { roleKey: 'FITNESS_TRAINER', schemaId: 'fitness_trainer_onboarding_v1', profession: 'fitness_trainer' },
  { roleKey: 'CATERING_SERVICES', schemaId: 'catering_services_onboarding_v1', profession: 'catering_services' },
];

const OUTPUT_ROOT = path.join(process.cwd(), 'tests', 'visual-artifacts', 'external-roles');

async function ensureFolders() {
  await fs.mkdir(path.join(OUTPUT_ROOT, 'onboarding'), { recursive: true });
  await fs.mkdir(path.join(OUTPUT_ROOT, 'dashboard'), { recursive: true });
}

function roleSlug(roleKey: string) {
  return roleKey.toLowerCase();
}

test('capture onboarding + dashboard screenshots for all external roles', async ({ page }) => {
  test.setTimeout(240_000);
  await ensureFolders();
  await page.setViewportSize({ width: 1440, height: 900 });

  for (const role of ROLE_SCENARIOS) {
    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(1400);
    await expect(page.locator('body')).not.toContainText('TypeError');
    await page.screenshot({
      path: path.join(OUTPUT_ROOT, 'onboarding', `${roleSlug(role.roleKey)}.png`),
      fullPage: true,
    });

    await page.goto(`/dashboard?_preview=${encodeURIComponent(role.roleKey)}`, { waitUntil: 'domcontentloaded' });
    await page.waitForTimeout(1000);
    await expect(page.locator('body')).not.toContainText('TypeError');
    await page.screenshot({
      path: path.join(OUTPUT_ROOT, 'dashboard', `${roleSlug(role.roleKey)}.png`),
      fullPage: true,
    });
  }
});
