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

const MOCK_PATIENT = {
  id: 'test-patient-uuid',
  name_family: 'Robot',
  name_given: ['Robbert'],
  birth_date: '1990-01-01',
  identifier_client_number: '99001',
};

const FHIR_PATIENT_RESPONSE = {
  entry: [
    {
      resource: {
        id: MOCK_PATIENT.id,
        name: [{ family: MOCK_PATIENT.name_family, given: MOCK_PATIENT.name_given }],
        birthDate: MOCK_PATIENT.birth_date,
        identifier: [
          { system: 'http://example.com/client/999.7.6', value: MOCK_PATIENT.identifier_client_number },
        ],
      },
    },
  ],
};

test.describe('Agenda', () => {
  test.beforeEach(async ({ page }) => {
    await page.goto('/epd/agenda');
    await page.waitForLoadState('networkidle');
  });

  test('laadt de agendapagina', async ({ page }) => {
    await expect(page.getByRole('heading', { name: 'Agenda' })).toBeVisible();
    await expect(page.getByRole('button', { name: /Nieuwe Afspraak|Nieuw/i })).toBeVisible();
  });

  test('toont navigatieknoppen en weergave-switcher', async ({ page }) => {
    await expect(page.getByRole('button', { name: 'Vandaag' })).toBeVisible();
    await expect(page.getByRole('button', { name: 'Dag' })).toBeVisible();
  });
});

test.describe('Afspraak maken', () => {
  test.beforeEach(async ({ page }) => {
    // Mock patient search
    await page.route('**/api/fhir/Patient**', async (route) => {
      await route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify(FHIR_PATIENT_RESPONSE) });
    });

    // Mock afspraak aanmaken (voorkom echte database write)
    await page.route('**/epd/agenda**', async (route) => {
      if (route.request().method() === 'POST') {
        await route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ success: true }) });
      } else {
        await route.continue();
      }
    });

    await page.goto('/epd/agenda');
    await page.waitForLoadState('networkidle');
  });

  test('opent de "Nieuwe Afspraak" modal', async ({ page }) => {
    await page.getByRole('button', { name: /Nieuwe Afspraak|Nieuw/i }).click();

    await expect(page.getByRole('dialog')).toBeVisible();
    await expect(page.getByRole('heading', { name: 'Nieuwe Afspraak' })).toBeVisible();
  });

  test('modal toont alle verplichte velden', async ({ page }) => {
    await page.getByRole('button', { name: /Nieuwe Afspraak|Nieuw/i }).click();

    const dialog = page.getByRole('dialog');
    await expect(dialog.getByPlaceholder(/Zoek op naam/i)).toBeVisible();
    await expect(dialog.locator('input[type="date"]')).toBeVisible();
    await expect(dialog.locator('input[type="time"]').first()).toBeVisible();
    await expect(dialog.getByRole('button', { name: 'Afspraak maken' })).toBeVisible();
  });

  test('zoeken naar patiënt toont resultaat', async ({ page }) => {
    await page.getByRole('button', { name: /Nieuwe Afspraak|Nieuw/i }).click();

    const dialog = page.getByRole('dialog');
    await dialog.getByPlaceholder(/Zoek op naam/i).fill('Robbert');

    // Dropdown met patiënt verschijnt
    await expect(dialog.getByText('Robbert Robot')).toBeVisible({ timeout: 5_000 });
  });

  test('selecteren van patiënt vult het veld in', async ({ page }) => {
    await page.getByRole('button', { name: /Nieuwe Afspraak|Nieuw/i }).click();

    const dialog = page.getByRole('dialog');
    await dialog.getByPlaceholder(/Zoek op naam/i).fill('Robbert');
    await expect(dialog.getByText('Robbert Robot')).toBeVisible({ timeout: 5_000 });
    await dialog.getByText('Robbert Robot').click();

    // Patiënt bevestigingskaart zichtbaar
    await expect(dialog.locator('.bg-teal-50')).toBeVisible();
  });

  test('afspraak maken knop is uitgeschakeld zonder patiënt', async ({ page }) => {
    await page.getByRole('button', { name: /Nieuwe Afspraak|Nieuw/i }).click();

    const dialog = page.getByRole('dialog');
    const submitButton = dialog.getByRole('button', { name: 'Afspraak maken' });

    await expect(submitButton).toBeDisabled();
  });

  test('sluiten sluit de modal', async ({ page }) => {
    await page.getByRole('button', { name: /Nieuwe Afspraak|Nieuw/i }).click();

    const dialog = page.getByRole('dialog');
    await expect(dialog).toBeVisible();

    await dialog.getByRole('button', { name: 'Sluiten' }).click();
    await expect(dialog).not.toBeVisible();
  });
});
