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

const ADMIN_EMAIL = 'admin@e2e.test';
const ADMIN_PASSWORD = 'e2e-admin-pw-9f3a2b1c';
const ORIGIN = 'http://localhost:4331';

async function signIn(page: import('@playwright/test').Page) {
  await page.goto('/admin/login');
  await page.getByLabel('Email').fill(ADMIN_EMAIL);
  await page.getByLabel('Password').fill(ADMIN_PASSWORD);
  await page.getByRole('button', { name: 'Sign in' }).click();
  await page.waitForURL('**/admin');
}

test('catch-all routes a multi-segment DELETE to OUR handler (not Astro default 404)', async ({ page }) => {
  await signIn(page);
  // A multi-segment, absent key. If [...key].ts is correctly named + placed, Astro routes this to our
  // DELETE handler → 404 with our JSON envelope. If the file is misnamed, Astro serves its own 404 HTML.
  // The DISCRIMINATOR is the body shape, not the status (both are 404): our handler proves it ran.
  const res = await page.request.delete('/api/admin/media/media/2026/06/does-not-exist.png', {
    headers: { origin: ORIGIN },
  });
  expect(res.status()).toBe(404);
  const ct = res.headers()['content-type'] ?? '';
  expect(ct, `expected JSON from our handler, got ${ct} — catch-all likely misrouted`).toContain('application/json');
  const body = (await res.json()) as { error?: { code?: string } };
  expect(body.error?.code).toBe('not_found'); // OUR envelope → the catch-all routed correctly
});
