/**
 * File System Infrastructure Tests
 * Verifies temporary directory creation and cleanup behavior used by backend
 * workflows that depend on local filesystem access.
 */
const fs = require('fs');
const path = require('path');

describe('File System Infrastructure Tests', () => {
    const testDir = path.join(__dirname, 'test_temp');

    beforeAll(() => {
        // Create test directory
        if (!fs.existsSync(testDir)) {
            fs.mkdirSync(testDir, { recursive: true });
        }
    });

    afterAll(() => {
        // Clean up test directory
        if (fs.existsSync(testDir)) {
            fs.rmSync(testDir, { recursive: true, force: true });
        }
    });

    describe('Directory Operations', () => {
        it('should create and remove directories', () => {
            const subDir = path.join(testDir, 'subdir');

            // Create directory
            fs.mkdirSync(subDir, { recursive: true });
            expect(fs.existsSync(subDir)).toBe(true);

            // Remove directory
            fs.rmSync(subDir, { recursive: true });
            expect(fs.existsSync(subDir)).toBe(false);
        });
    });

    describe('File Operations', () => {
        it('should create, read, and delete files', () => {
            const testFile = path.join(testDir, 'test.txt');
            const testContent = 'Hello, World!';

            // Create file
            fs.writeFileSync(testFile, testContent);
            expect(fs.existsSync(testFile)).toBe(true);

            // Read file
            const content = fs.readFileSync(testFile, 'utf8');
            expect(content).toBe(testContent);

            // Delete file
            fs.unlinkSync(testFile);
            expect(fs.existsSync(testFile)).toBe(false);
        });

        it('should handle file paths correctly', () => {
            const testFile = path.join(testDir, 'path_test.txt');
            const content = 'Path test content';

            fs.writeFileSync(testFile, content);

            // Verify path operations
            expect(path.extname(testFile)).toBe('.txt');
            expect(path.basename(testFile)).toBe('path_test.txt');
            expect(path.dirname(testFile)).toBe(testDir);

            fs.unlinkSync(testFile);
        });
    });

    describe('Upload Directory Structure', () => {
        const uploadsDir = path.join(__dirname, '..', '..', 'uploads');
        const userImagesDir = path.join(__dirname, '..', '..', 'public', 'user_added_images');

        it('should verify upload directories exist', () => {
            // These directories should be created by the server
            // In tests, we may need to create them manually
            [uploadsDir, userImagesDir].forEach(dir => {
                if (!fs.existsSync(dir)) {
                    fs.mkdirSync(dir, { recursive: true });
                }
                expect(fs.existsSync(dir)).toBe(true);
            });
        });

        it('should allow file creation in upload directories', () => {
            const testFile = path.join(uploadsDir, 'infrastructure_test.txt');
            const content = 'Infrastructure test file';

            fs.writeFileSync(testFile, content);
            expect(fs.existsSync(testFile)).toBe(true);

            const readContent = fs.readFileSync(testFile, 'utf8');
            expect(readContent).toBe(content);

            fs.unlinkSync(testFile);
        });
    });
});