const { formatGroupListRow, formatGroupDetail } = require('../../src/utils/groupFormatter');

describe('groupFormatter', () => {
    describe('formatGroupListRow', () => {
        it('maps snake_case columns to camelCase dto', () => {
            const row = {
                group_id: 'foo',
                year: 2024,
                title: 'Title',
                description: 'Desc',
                name: 'Alice',
                upload_date: '2024-01-01',
                approved: 1,
                image_count: '5',
                preview_image: 'path/to/thumb.jpg'
            };

            expect(formatGroupListRow(row)).toEqual({
                groupId: 'foo',
                year: 2024,
                title: 'Title',
                description: 'Desc',
                name: 'Alice',
                uploadDate: '2024-01-01',
                approved: true,
                imageCount: 5,
                previewImage: 'path/to/thumb.jpg'
            });
        });

        it('provides sane defaults when optional values missing', () => {
            const row = {
                group_id: 'bar',
                year: 2023,
                title: 'Other',
                description: null,
                name: null,
                upload_date: '2023-12-24',
                approved: 0,
                image_count: null,
                preview_image: undefined
            };

            expect(formatGroupListRow(row)).toEqual({
                groupId: 'bar',
                year: 2023,
                title: 'Other',
                description: null,
                name: null,
                uploadDate: '2023-12-24',
                approved: false,
                imageCount: 0,
                previewImage: null
            });
        });
    });

    describe('formatGroupDetail', () => {
        it('maps nested image rows and flags', () => {
            const group = {
                group_id: 'foo',
                year: 2024,
                title: 'Title',
                description: 'Desc',
                name: 'Alice',
                upload_date: '2024-01-01',
                approved: 0,
                display_in_workshop: 1,
                consent_timestamp: null
            };
            const images = [
                {
                    id: 1,
                    file_name: 'one.png',
                    original_name: 'one.png',
                    file_path: 'images/one.png',
                    preview_path: null,
                    upload_order: 1,
                    file_size: null,
                    mime_type: 'image/png',
                    image_description: 'desc'
                }
            ];

            expect(formatGroupDetail(group, images)).toEqual({
                groupId: 'foo',
                year: 2024,
                title: 'Title',
                description: 'Desc',
                name: 'Alice',
                uploadDate: '2024-01-01',
                approved: false,
                display_in_workshop: true,
                consent_timestamp: null,
                images: [
                    {
                        id: 1,
                        fileName: 'one.png',
                        originalName: 'one.png',
                        filePath: 'images/one.png',
                        previewPath: null,
                        uploadOrder: 1,
                        fileSize: null,
                        mimeType: 'image/png',
                        imageDescription: 'desc'
                    }
                ],
                imageCount: 1
            });
        });
    });
});
