import { test, expect } from '@playwright/test';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

test('admin setup wizard uploads selected assets with bearer token', async ({ page }) => {
  const uploads = [];
  await page.route('**/api/v1/admin/weddings/*/assets', async (route) => {
    uploads.push({
      method: route.request().method(),
      authorization: route.request().headers().authorization
    });
    await route.fulfill({
      status: 200,
      contentType: 'application/json',
      body: JSON.stringify({ asset: { id: `asset-${uploads.length}`, url: '/media/test/asset.webp' } })
    });
  });

  await page.goto('http://127.0.0.1:8082/admin');
  await expect(page.locator('#setupForm')).toBeHidden();
  await page.locator('#adminToken').fill('local-dev-token');
  await page.locator('#saveAdminToken').click();
  await expect(page.locator('#validationOutput')).toContainText('Admin token saved');

  const here = path.dirname(fileURLToPath(import.meta.url));
  const welcomeVideo = path.resolve(here, '../../server/storage/media/demo-wedding/66f1a5d7-0170-4fd4-98e5-b78a637f91f3/video.webm');
  await page.locator('input[name="coupleMessageVideo"]').setInputFiles(welcomeVideo);
  await page.locator('#setupForm button[type="submit"]').click();

  await expect(page.locator('#validationOutput')).toContainText('Uploaded 1 setup asset');
  expect(uploads).toHaveLength(1);
  expect(uploads.every((upload) => upload.method === 'POST')).toBe(true);
  expect(uploads.every((upload) => upload.authorization === 'Bearer local-dev-token')).toBe(true);
});
