import { test, expect } from '@playwright/test';
import * as fs from 'fs';
import * as path from 'path';
import Ajv from 'ajv/dist/2020.js';
import addFormats from 'ajv-formats';
import {
    MINIMAL_TRANSMITTAL,
    FULL_TRANSMITTAL,
    SUBMITTAL,
    INVALID_TRANSMITTALS,
} from './fixtures/transmittal-data.js';

const SCHEMA_PATH = path.resolve('transmittal/transmittal.schema.json');
const schema = JSON.parse(fs.readFileSync(SCHEMA_PATH, 'utf8'));

const ajv = new Ajv({ strict: false, allErrors: true });
addFormats(ajv);
const validate = ajv.compile(schema);

test.describe('transmittal.schema.json', () => {
    test('schema is syntactically valid JSON Schema', () => {
        expect(() => ajv.compile(schema)).not.toThrow();
    });

    for (const [name, data] of [
        ['MINIMAL_TRANSMITTAL', MINIMAL_TRANSMITTAL],
        ['FULL_TRANSMITTAL', FULL_TRANSMITTAL],
        ['SUBMITTAL', SUBMITTAL],
    ]) {
        test(`accepts canonical fixture: ${name}`, () => {
            const ok = validate(data);
            if (!ok) {
                console.error(`Validation errors for ${name}:`, JSON.stringify(validate.errors, null, 2));
            }
            expect(ok).toBe(true);
        });
    }

    for (const { description, data } of INVALID_TRANSMITTALS) {
        test(`rejects: ${description}`, () => {
            expect(validate(data)).toBe(false);
        });
    }
});
