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

const departmentHeadUser = {
  id: '00000000-0000-4000-8000-000000000010',
  name: 'Department Head',
  email: 'head@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: [
    {
      id: '00000000-0000-4000-8000-000000000020',
      company_id: '00000000-0000-4000-8000-000000000002',
      name: 'IT',
      is_active: true,
      is_department_head: true,
      is_manager: true,
      created_at: new Date().toISOString(),
      updated_at: new Date().toISOString(),
    },
  ],
  roles: [],
  permissions: ['department.settings.manage', 'department.users.manage'],
  is_active: true,
  onboarding_step: 'portal',
  onboarding_completed_at: null,
  dismissed_tours: [],
  created_at: new Date().toISOString(),
  updated_at: new Date().toISOString(),
};

test.describe('Onboarding wizard 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/me', async (route) => {
      await route.fulfill({
        status: 200,
        contentType: 'application/json',
        body: JSON.stringify({ data: departmentHeadUser }),
      });
    });

    await page.route('**/api/v1/onboarding/status', async (route) => {
      await route.fulfill({
        status: 200,
        contentType: 'application/json',
        body: JSON.stringify({
          data: {
            persona: 'department_head',
            department_id: '00000000-0000-4000-8000-000000000020',
            onboarding_step: 'portal',
            onboarding_completed_at: null,
            dismissed_tours: [],
            should_show_wizard: true,
            checklist: {
              persona: 'department_head',
              department_id: '00000000-0000-4000-8000-000000000020',
              completed: 0,
              total: 5,
              items: [],
            },
          },
        }),
      });
    });

    await page.route('**/api/v1/onboarding/step', async (route) => {
      if (route.request().method() === 'PATCH') {
        await route.fulfill({
          status: 200,
          contentType: 'application/json',
          body: JSON.stringify({
            data: {
              onboarding_step: 'portal',
              onboarding_completed_at: null,
            },
          }),
        });
        return;
      }

      await route.continue();
    });

    await page.route('**/api/v1/onboarding/complete', async (route) => {
      await route.fulfill({
        status: 200,
        contentType: 'application/json',
        body: JSON.stringify({
          data: {
            onboarding_step: null,
            onboarding_completed_at: new Date().toISOString(),
          },
        }),
      });
    });

    await page.route('**/api/v1/departments/**', async (route) => {
      await route.fulfill({
        status: 200,
        contentType: 'application/json',
        body: JSON.stringify({
          data: {
            id: '00000000-0000-4000-8000-000000000020',
            company_id: '00000000-0000-4000-8000-000000000002',
            name: 'IT',
            code: 'IT',
            slug: 'it',
            portal_enabled: true,
            portal_description_en: '',
            portal_description_ar: '',
            is_active: true,
            users_count: 1,
            created_at: new Date().toISOString(),
            updated_at: new Date().toISOString(),
          },
        }),
      });
    });

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

  test('department head can close wizard and resume without completing', async ({ page }) => {
    await page.goto('/dashboard');

    await expect(page.getByRole('dialog')).toBeVisible();
    await expect(page.getByText("Let's set up your department")).toBeVisible();

    await page.getByRole('button', { name: 'Save and exit setup' }).click();

    await expect(page.getByRole('dialog')).toBeHidden();
    await expect(page.getByText('Continue setup')).toBeVisible();

    await page.reload();

    await expect(page.getByRole('dialog')).toBeVisible();
    await expect(page.getByText('Department portal')).toBeVisible();
  });
});
