import { test, expect } from '../../../fixtures.js';
import * as apps from '../../../helpers/applications.js';
import * as bwl from '../../../process-actions/bwl-actions.js';
import { skipUnlessBwlOnConvex } from './_helpers.js';

test.describe('BMT (BWL) happy path', () => {
  test.beforeEach(({}, testInfo) => {
    skipUnlessBwlOnConvex(testInfo);
  });

  test('full stage 1 + stage 2 flow ends in accepted', async ({
    adminPage,
  }) => {
    const application = await bwl.seedApplication({ grade: '1,3' });
    try {
      await apps.gotoApplicationDetail(adminPage, 'bwl', application._id);

      await bwl.runStage1Accept(adminPage);

      await apps.expectPreviewDecision(
        adminPage,
        's1',
        'textevaluation2',
        'accept'
      );
      // Application status only flips to `accepted` once **all** steps
      // are done (engine rule, see ADR-0018 + `deriveStatus`). The
      // stage-2 export step has no decision but still gates the
      // aggregate, so we complete it before asserting the status.
      await apps.expectApplicationStatus(
        adminPage,
        application._id,
        'in_progress'
      );

      await bwl.markExported(adminPage);
      await apps.completeStep(adminPage, 's2', 'export');
      await apps.expectStepResult(adminPage, 's2', 'export', 'done');
      await apps.expectApplicationStatus(
        adminPage,
        application._id,
        'accepted'
      );
    } finally {
      await apps.deleteApplication(application._id);
    }
  });

  test('dashboard count for accepted reflects new application', async ({
    adminPage,
  }) => {
    const application = await bwl.seedApplication({ grade: '1,3' });
    try {
      await apps.gotoApplicationDetail(adminPage, 'bwl', application._id);
      await bwl.runStage1Accept(adminPage);
      // BMT only flips to `accepted` once `s2.export` is done too
      // (engine rule, see ADR-0018 + `deriveStatus`).
      await bwl.markExported(adminPage);
      await apps.completeStep(adminPage, 's2', 'export');
      await apps.expectApplicationStatus(
        adminPage,
        application._id,
        'accepted'
      );

      await apps.gotoApplicationDashboard(adminPage, 'bwl');
      // BMT renders the dashboard via `<masterev-stats-group>` from
      // `ui-dashboard` rather than the Convex `dashboard-counts`
      // component, so the per-status testid is not in the DOM. Locate
      // the accepted card by its translated label instead.
      const card = adminPage.locator('masterev-stats-item', {
        hasText: /Zugelassen|angenommen|accepted|succeeded|erfolgreich/i,
      }).first();
      await expect(card).toBeVisible({ timeout: 10_000 });
      const text = (await card.textContent()) ?? '';
      expect(text).toMatch(/\d+/);

      await apps.clickDashboardCard(adminPage, 'bwl', 'accepted');
      await expect(
        adminPage.locator(`[data-testid="application-row-${application._id}"]`)
      ).toBeVisible({ timeout: 10_000 });
    } finally {
      await apps.deleteApplication(application._id);
    }
  });
});
