<?php
require_once __DIR__ . '/db/config.php';

try {
    $pdo = db();
    $stmt = $pdo->query('SELECT * FROM photos ORDER BY created_at DESC');
    $images = $stmt->fetchAll();
} catch (PDOException $e) {
    // Handle DB error
    $images = [];
    // You might want to log an error here
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo htmlspecialchars($_SERVER['PROJECT_NAME'] ?? 'Photo App'); ?></title>
    <meta name="description" content="<?php echo htmlspecialchars($_SERVER['PROJECT_DESCRIPTION'] ?? 'A Windows photos app like Google Photos'); ?>">
    
    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- Bootstrap Icons -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
    
    <!-- Custom CSS -->
    <link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
</head>
<body class="light-theme">

    <div class="main-container">
        <aside class="sidebar">
            <h4 class="mb-4">My Photos</h4>

            <div class="px-3 mb-3">
                <button id="import-folder-btn" class="btn btn-primary w-100"><i class="bi bi-folder-plus me-2"></i> Import Folder</button>
                <input type="file" id="folder-input" webkitdirectory directory multiple style="display: none;" />
            </div>

            <ul class="nav flex-column">
                <li class="nav-item">
                    <a class="nav-link active" href="#"><i class="bi bi-image me-2"></i> Photos</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#"><i class="bi bi-star me-2"></i> Favorites</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#"><i class="bi bi-images me-2"></i> Albums</a>
                </li>
            </ul>

            <hr>

            <h6>Filters</h6>
            <ul class="nav flex-column">
                <li class="nav-item">
                    <a class="nav-link" href="#"><i class="bi bi-people me-2"></i> Faces</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#"><i class="bi bi-geo-alt me-2"></i> Places</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#"><i class="bi bi-tag me-2"></i> Things</a>
                </li>
            </ul>
            
            <div class="position-absolute bottom-0 mb-3">
                <a class="nav-link" href="#"><i class="bi bi-gear me-2"></i> Settings</a>
            </div>
        </aside>

        <main class="main-content">
            <header class="header">
                <div class="search-bar">
                    <input type="search" class="form-control" placeholder="Search photos, people, places...">
                </div>
                <div class="d-flex align-items-center">
                    <div id="theme-switcher" class="theme-switcher fs-4">
                        <i class="bi bi-moon-fill"></i>
                    </div>
                </div>
            </header>

            <div class="photo-gallery">
                <?php if (!empty($images)):
                    foreach ($images as $image):
                 ?>
                        <div class="photo-item">
                            <img src="<?php echo htmlspecialchars($image['filepath']); ?>" alt="<?php echo htmlspecialchars($image['filename']); ?>" loading="lazy">
                        </div>
                    <?php 
                        endforeach;
                    else: 
                ?>
                    <div class="col">
                        <p>No images found. Please import photos to see them here.</p>
                    </div>
                <?php endif; ?>
            </div>
        </main>
    </div>

    <!-- Custom JS -->
    <script src="assets/js/main.js?v=<?php echo time(); ?>"></script>
</body>
</html>
