/**
 * Atlas JSON Image Tests
 * Validates that bundled atlas JSON files reference image assets in a shape that
 * the backend import and image resolution logic can consume.
 */
const fs = require('fs');
const path = require('path');

const { createAtlasImageResolver } = require('../../utils/atlasImageResolver');
const {
    ATLAS_TRANSLATION_SUFFIX,
    readAtlasJson,
} = require('../../utils/atlasImportUtils');

describe('atlas JSON image references', () => {
    const atlasesDir = path.join(__dirname, '..', '..', 'data', 'atlases');
    const imagesDir = path.join(__dirname, '..', '..', 'public', 'images');
    const resolveAtlasImage = createAtlasImageResolver(imagesDir);
    const atlasFiles = fs.readdirSync(atlasesDir)
        .filter(fileName => fileName.endsWith('.json'))
        .filter(fileName => !fileName.endsWith(ATLAS_TRANSLATION_SUFFIX));

    it('uses short image references only', () => {
        const violations = [];

        for (const fileName of atlasFiles) {
            const atlas = readAtlasJson(path.join(atlasesDir, fileName));

            for (const section of atlas.sections || []) {
                for (const trait of section.znaky || []) {
                    for (const option of trait.options || []) {
                        for (const imageReference of option.images || []) {
                            if (typeof imageReference !== 'string') {
                                violations.push(`${fileName}: non-string image reference in ${trait.id}`);
                                continue;
                            }

                            if (imageReference.includes('/public/images/') || /^https?:\/\//i.test(imageReference)) {
                                violations.push(`${fileName}: ${trait.id} -> ${imageReference}`);
                            }
                        }
                    }
                }
            }
        }

        expect(violations).toEqual([]);
    });

    it('resolves every atlas image reference to a real file', () => {
        const unresolvedReferences = [];

        for (const fileName of atlasFiles) {
            const atlas = readAtlasJson(path.join(atlasesDir, fileName));

            for (const section of atlas.sections || []) {
                for (const trait of section.znaky || []) {
                    for (const option of trait.options || []) {
                        for (const imageReference of option.images || []) {
                            try {
                                const resolvedPublicPath = resolveAtlasImage(imageReference);
                                const relativeImagePath = resolvedPublicPath.replace(/^\/public\/images\//, '');
                                const absoluteImagePath = path.join(imagesDir, ...relativeImagePath.split('/'));

                                if (!fs.existsSync(absoluteImagePath)) {
                                    unresolvedReferences.push(`${fileName}: ${trait.id} -> ${imageReference} => ${resolvedPublicPath}`);
                                }
                            } catch (error) {
                                unresolvedReferences.push(`${fileName}: ${trait.id} -> ${imageReference} (${error.message})`);
                            }
                        }
                    }
                }
            }
        }

        expect(unresolvedReferences).toEqual([]);
    });
});