import { mkdirSync, rmSync, existsSync } from 'fs';
import { resolve } from 'path';
import sharp from 'sharp';
import { renderImageVersion } from '../lib/image-editor.mjs';

const root = resolve('tmp-slice32-render');
rmSync(root, { recursive: true, force: true });
mkdirSync(resolve(root, 'source'), { recursive: true });
await sharp({ create: { width: 900, height: 1200, channels: 3, background: '#d8c1a7' } }).png().toFile(resolve(root, 'source', 'produkt.png'));
let recorded = null;
const result = await renderImageVersion({
  uploadRoot: root,
  sourcePath: 'source/produkt.png',
  context: 'batch',
  rootPath: 'source/produkt.png',
  outputDirectory: 'batch/test-job',
  outputFilename: 'produkt-webshop-01',
  operations: {
    resize: { width: 640, height: 640, fit: 'contain', background: '#ffffff' },
    format: 'webp', quality: 82, sharpness: 12,
    watermark: { enabled: true, text: 'Vendoo', position: 'bottom-right', opacity: 30 },
  },
  recordVersion: value => { recorded = value; return { id: 1, ...value }; },
});
if (!existsSync(resolve(root, result.filename))) throw new Error('Batch-Ausgabe fehlt.');
const meta = await sharp(resolve(root, result.filename)).metadata();
if (meta.width !== 640 || meta.height !== 640 || meta.format !== 'webp') throw new Error(`Falsche Ausgabe ${meta.width}x${meta.height} ${meta.format}`);
if (!result.filename.startsWith('batch/test-job/produkt-webshop-01')) throw new Error(`Falscher Ausgabeordner: ${result.filename}`);
if (recorded?.context !== 'batch' || recorded?.output_path !== result.filename) throw new Error('Nicht destruktiver Versionsdatensatz ist inkorrekt.');
console.log(JSON.stringify({ ok: true, output: result.filename, width: meta.width, height: meta.height, format: meta.format, version_context: recorded.context }, null, 2));
rmSync(root, { recursive: true, force: true });
