/**
 * Atlas Import Utility Tests
 * Verifies atlas translation merging and trait ID scoping helpers used by the
 * backend atlas import script.
 */
const fs = require('fs');
const os = require('os');
const path = require('path');

const {
    getAtlasTranslationFilePath,
    readAtlasJson,
    toImportedTraitId,
} = require('../../../utils/atlasImportUtils');

describe('importAtlas trait id scoping', () => {
    it('prefixes trait ids with atlas id', () => {
        expect(toImportedTraitId('ant-anthropological-atlas-of-facial-features', 'ant-head-shape')).toBe('ant-anthropological-atlas-of-facial-features::ant-head-shape');
    });

    it('does not double-prefix already scoped ids', () => {
        expect(toImportedTraitId('ant-anthropological-atlas-of-facial-features', 'ant-anthropological-atlas-of-facial-features::ant-head-shape')).toBe('ant-anthropological-atlas-of-facial-features::ant-head-shape');
    });
});

describe('readAtlasJson', () => {
    let tempDir;

    beforeEach(() => {
        tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'atlas-import-'));
    });

    afterEach(() => {
        fs.rmSync(tempDir, { recursive: true, force: true });
    });

    it('merges english atlas source with czech translation sidecar', () => {
        const atlasPath = path.join(tempDir, 'atlas.json');
        const translationPath = getAtlasTranslationFilePath(atlasPath);

        fs.writeFileSync(atlasPath, JSON.stringify({
            id: 'atlas_v1',
            label: 'Atlas',
            description: 'English description',
            sections: [
                {
                    name: 'Face',
                    traits: [
                        {
                            id: 'trait_1',
                            label: 'Trait 1',
                            description: 'Trait description',
                            options: [
                                {
                                    label: 'Option 1',
                                    images: ['face/option-1.jpg']
                                }
                            ]
                        }
                    ]
                }
            ]
        }), 'utf8');

        fs.writeFileSync(translationPath, JSON.stringify({
            id: 'atlas_v1',
            label: 'Atlas CS',
            description: 'Cesky popis',
            sections: [
                {
                    name: 'Oblicej',
                    traits: [
                        {
                            id: 'trait_1',
                            label: 'Znak 1',
                            description: 'Cesky popis znaku',
                            options: [
                                {
                                    label: 'Moznost 1'
                                }
                            ]
                        }
                    ]
                }
            ]
        }), 'utf8');

        expect(readAtlasJson(atlasPath)).toEqual({
            id: 'atlas_v1',
            label_cs: 'Atlas CS',
            label_en: 'Atlas',
            popis_cs: 'Cesky popis',
            popis_en: 'English description',
            sections: [
                {
                    nazev_cs: 'Oblicej',
                    nazev_en: 'Face',
                    znaky: [
                        {
                            id: 'trait_1',
                            label_cs: 'Znak 1',
                            label_en: 'Trait 1',
                            popis_cs: 'Cesky popis znaku',
                            popis_en: 'Trait description',
                            options: [
                                {
                                    label_cs: 'Moznost 1',
                                    label_en: 'Option 1',
                                    images: ['face/option-1.jpg']
                                }
                            ]
                        }
                    ]
                }
            ]
        });
    });

    it('throws when translation sidecar is missing for english-only atlas files', () => {
        const atlasPath = path.join(tempDir, 'atlas.json');

        fs.writeFileSync(atlasPath, JSON.stringify({
            id: 'atlas_v1',
            label: 'Atlas',
            description: '',
            sections: []
        }), 'utf8');

        expect(() => readAtlasJson(atlasPath)).toThrow('Atlas translation file not found');
    });

    it('throws when translation structure does not match the source atlas', () => {
        const atlasPath = path.join(tempDir, 'atlas.json');
        const translationPath = getAtlasTranslationFilePath(atlasPath);

        fs.writeFileSync(atlasPath, JSON.stringify({
            id: 'atlas_v1',
            label: 'Atlas',
            description: '',
            sections: [
                {
                    name: 'Face',
                    traits: [
                        {
                            id: 'trait_1',
                            label: 'Trait 1',
                            description: '',
                            options: [
                                { label: 'Option 1', images: [] }
                            ]
                        }
                    ]
                }
            ]
        }), 'utf8');

        fs.writeFileSync(translationPath, JSON.stringify({
            id: 'atlas_v1',
            label: 'Atlas CS',
            description: '',
            sections: [
                {
                    name: 'Oblicej',
                    traits: []
                }
            ]
        }), 'utf8');

        expect(() => readAtlasJson(atlasPath)).toThrow('translation trait count mismatch');
    });
});