import blocks from '@/Utils/blocks';
import path from 'path';
import fs from 'fs';
import { expect } from 'vitest';

describe('blocks.js structure', () => {
    it('should contain uuid, componentName, renderComponentName and description for every block', () => {
        blocks.groups.forEach(group => {
            group.blocks.forEach(block => {
                expect(block).toHaveProperty('uuid');
                expect(typeof block.uuid).toBe('string');

                expect(block).toHaveProperty('componentName');
                expect(typeof block.componentName).toBe('string');

                expect(block).toHaveProperty('renderComponentName');
                expect(typeof block.renderComponentName).toBe('string');

                expect(block).toHaveProperty('description');
                expect(typeof block.description).toBe('string');
            });
        });
    });

    it('should contain optionsComponentName if block is not static', () => {
        blocks.groups.forEach(group => {
            group.blocks.forEach(block => {
                if (!block.static) {
                    expect(block).toHaveProperty('optionsComponentName');
                    expect(typeof block.optionsComponentName).toBe('string');
                }
            });
        });
    });

    it('should not require optionsComponentName if block is static', () => {
        blocks.groups.forEach(group => {
            group.blocks.forEach(block => {
                if (block.static) {
                    if (block.hasOwnProperty('optionsComponentName')) {
                        expect(typeof block.optionsComponentName).toBe('string');
                    }
                }
            });
        });
    });

    it('should contain a valid thumbUrl that points to an existing file, for each block', () => {
        const publicDir = path.resolve(__dirname, '../../../../public');

        blocks.groups.forEach(group => {
            group.blocks.forEach(block => {
              expect(block).toHaveProperty('thumbUrl');
              expect(typeof block.thumbUrl).toBe('string');
            
              const isValid = /^\/img\/blocks\/[^\/]+\.(jpg|png)$/.test(block.thumbUrl);
              expect(isValid).toBe(true);

              const fullPath = path.join(publicDir, block.thumbUrl);
              const exists = fs.existsSync(fullPath);
              expect(exists).toBe(true);
            });
        });
    });
});
