<?php

/**
 * FileSystemService
 * Handles file system operations and path management
 */
class FileSystemService {
    
    /**
     * Determine the correct path to the area folder based on environment
     */
    public static function getAreaPath() {
        // Check if we're in development structure (backend/src/Api)
        $devPath = __DIR__ . '/../../../area';
        if (is_dir($devPath)) {
            return '/../../../area';
        }
        
        // Check if we're in deployment structure (src/Api)
        $deployPath = __DIR__ . '/../../area';
        if (is_dir($deployPath)) {
            return '/../../area';
        }
        
        // Fallback to development path
        return '/../../../area';
    }
    
    /**
     * Central list of files to ignore
     */
    public static function getIgnoredFiles() {
        return [
            '.DS_Store',        // macOS System file
            'Thumbs.db',        // Windows Thumbnail cache
            'desktop.ini',      // Windows Desktop configuration
            '.gitignore',       // Git configuration
            '.gitkeep',         // Git placeholder
            'config.yaml',      // Configuration files
            'config.yml',
            'setup.yaml',
            'setup.yml',
            '.htaccess',        // Apache configuration
            'index.php',        // PHP index (except in HTML folders)
            'web.config',       // IIS configuration
            '.env',             // Environment variables
            '.env.local',
            'README.md',        // Documentation
            'readme.txt',
            'license.txt',
            'LICENSE'
        ];
    }
    
    /**
     * Check if a file should be ignored
     */
    public static function shouldIgnoreFile($filename) {
        $ignoredFiles = self::getIgnoredFiles();
        return in_array(strtolower($filename), array_map('strtolower', $ignoredFiles));
    }
    
    /**
     * Get file type based on extension
     */
    public static function getFileType($filename) {
        $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
        
        if (in_array($ext, ['jpg', 'jpeg', 'png', 'gif', 'webp', 'svg'])) {
            return 'image';
        } elseif (in_array($ext, ['mp4', 'mov', 'avi', 'webm'])) {
            return 'video';
        } elseif (in_array($ext, ['html', 'htm'])) {
            return 'html';
        }
        
        return 'other';
    }
    
    /**
     * Create safe URL for file access
     */
    public static function createFileUrl($clientDir, $projectName, $adsFolders, $filename) {
        $urlParts = array_map('rawurlencode', $adsFolders);
        return "/area/" . rawurlencode($clientDir) . "/" . rawurlencode($projectName) . "/ads/" 
             . implode('/', $urlParts) . (count($urlParts) ? '/' : '') . rawurlencode($filename);
    }
    
    /**
     * Validate path is within allowed client directory
     */
    public static function validatePath($realPath, $clientDir) {
        $allowedBasePath = realpath(__DIR__ . self::getAreaPath() . '/' . $clientDir);
        return $realPath && strpos($realPath, $allowedBasePath) === 0;
    }
}
