<?php
header('Content-Type: application/json');
header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');

$dir = __DIR__ . '/uploads/glasses/';
$glasses = [];

if (is_dir($dir)) {
    $files = [];
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            if ($file != '.' && $file != '..' && strtolower(pathinfo($file, PATHINFO_EXTENSION)) == 'png') {
                $files[] = $file;
            }
        }
        closedir($dh);
        
        // Sort files to show newest first
        rsort($files);
        
        foreach ($files as $file) {
            // Return path relative to the frontend script
            $glasses[] = 'uploads/glasses/' . $file;
        }
    }
}

echo json_encode(['success' => true, 'glasses' => $glasses]);
?>
