const schema = require('./data/eds-c-accordion.schema.json');
const exampleJson = require('./data/eds-c-accordion.example.json');
const fs = require('fs');
const path = require('path');
const componentTemplate = '{{> eds-c-accordion }}';
const Ajv = require("ajv");
const ajv = new Ajv();
const handlebars = require('handlebars');

const {findTemplates} = require('../../development/tests/templateLoader.js');
findTemplates(['eds-c-accordion']).load(handlebars);

const componentHbs = handlebars.compile(componentTemplate);
const renderedComponent = componentHbs(exampleJson);

describe('EDS accordion message component', () => {
    test('docs file exists', () => {
        const exists = fs.existsSync(path.resolve(__dirname, 'docs.md'));
        expect(exists).toBe(true);
    });
});

const validateData = (data) => {
    const requiredFields = ['id', 'title', 'content'];
    requiredFields.forEach(field => {
    if (!data[field]) {
        throw new Error(`${field} is required`);
    }
    });
};

describe(' EDS accordion schema validation and accessibility test', () => {

    const { configureAxe, toHaveNoViolations } = require('jest-axe');
    expect.extend(toHaveNoViolations);
    
    const axe = configureAxe({
        rules: {
            region: { enabled: false }
        }
    });
    
    test('Example data matches schema', () => {
        const validate = ajv.compile(schema);
        const valid = validate(exampleJson);
    
        if (!valid) {
            // Extract and format the Ajv validation errors
            const errors = validate.errors.map((error) => ajv.errorsText([error])).join('\n');
            throw new Error(errors);
        }
    
        expect(valid).toBeTruthy();
    });
    
    test('Template can be compiled and rendered', () => {
        document.body.innerHTML = renderedComponent;
        const container = document.querySelector('*');
        expect(container).toBeDefined();
    });
    
    test('Template passes basic HTML accessibility', async () => {
        expect(await axe(renderedComponent)).toHaveNoViolations();
    });

    describe('Accordion Template', () => {
    
        test('renders correctly when all fields are present', () => {
        validateData(exampleJson);
        const result = componentHbs(exampleJson);
        
        expect(result).toContain('id="test1"');
        expect(result).toContain('For authors');
        });
    
        test('throws error if id is missing', () => {
        const { id, ...newData } = exampleJson;
        expect(() => validateData(newData)).toThrow('id is required');
        });
    
        test('throws error if title is missing', () => {
        const { title, ...newData } = exampleJson;
        expect(() => validateData(newData)).toThrow('title is required');
        });
    
        test('throws error if content is missing', () => {
        const { content, ...newData } = exampleJson;
        expect(() => validateData(newData)).toThrow('content is required');
        });
    });
});