<?php
if (!isset($_SESSION)) session_start();
require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/functions.php';

// Redirect if not logged in (except for public pages)
$public_pages = ['index.php', 'register.php', 'forgot-password.php', 'reset-password.php'];
$current_file = basename($_SERVER['PHP_SELF']);
if (!isset($_SESSION['user_id']) && !in_array($current_file, $public_pages)) {
    header('Location: index.php');
    exit;
}

// Get company data for logged-in users
$company = null;
if (isset($_SESSION['user_id'])) {
    $stmt = $pdo->prepare("SELECT * FROM companies WHERE user_id = ? LIMIT 1");
    $stmt->execute([$_SESSION['user_id']]);
    $company = $stmt->fetch();
}

$current_page = basename($_SERVER['PHP_SELF']);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Invoicyble - <?php echo ucfirst(str_replace('.php', '', $current_page)); ?></title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <script>
        tailwind.config = {
            darkMode: 'class',
            theme: {
                extend: {
                    fontFamily: { 'sans': ['Inter', 'sans-serif'] }
                }
            }
        }
    </script>
    <style>
        /* Additional dark mode fixes */
        .dark {
            color-scheme: dark;
        }
        .dark table, .dark td, .dark th {
            border-color: #374151;
        }
        .dark input, .dark select, .dark textarea {
            background-color: #1f2937;
            color: #f3f4f6;
            border-color: #4b5563;
        }
        .dark input::placeholder, .dark textarea::placeholder {
            color: #9ca3af;
        }
    </style>
</head>
<body class="bg-gray-50 dark:bg-gray-900">
    <div class="flex h-screen">
        <?php if (isset($_SESSION['user_id'])): ?>
        <!-- Sidebar -->
        <aside class="w-64 bg-white dark:bg-gray-800 shadow-xl flex flex-col">
            <div class="p-6 border-b border-gray-200 dark:border-gray-700">
                <h1 class="text-2xl font-bold bg-gradient-to-r from-blue-600 to-indigo-600 bg-clip-text text-transparent">Invoicyble</h1>
                <?php if ($company): ?>
                    <p class="text-xs text-gray-500 dark:text-gray-400 mt-1 truncate"><?php echo htmlspecialchars($company['name']); ?></p>
                <?php endif; ?>
            </div>
            <nav class="flex-1 p-4 space-y-1">
                <a href="dashboard.php" class="flex items-center space-x-3 px-4 py-3 rounded-lg <?php echo $current_page == 'dashboard.php' ? 'bg-blue-50 dark:bg-blue-900/50 text-blue-600 dark:text-blue-400' : 'text-gray-700 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-700'; ?>">
                    <i class="fas fa-tachometer-alt w-5"></i><span>Dashboard</span>
                </a>
                <a href="clients.php" class="flex items-center space-x-3 px-4 py-3 rounded-lg <?php echo $current_page == 'clients.php' ? 'bg-blue-50 dark:bg-blue-900/50 text-blue-600 dark:text-blue-400' : 'text-gray-700 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-700'; ?>">
                    <i class="fas fa-users w-5"></i><span>Clients</span>
                </a>
                <a href="products.php" class="flex items-center space-x-3 px-4 py-3 rounded-lg <?php echo $current_page == 'products.php' ? 'bg-blue-50 dark:bg-blue-900/50 text-blue-600 dark:text-blue-400' : 'text-gray-700 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-700'; ?>">
                    <i class="fas fa-boxes w-5"></i><span>Products</span>
                </a>
                <a href="documents.php" class="flex items-center space-x-3 px-4 py-3 rounded-lg <?php echo $current_page == 'documents.php' ? 'bg-blue-50 dark:bg-blue-900/50 text-blue-600 dark:text-blue-400' : 'text-gray-700 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-700'; ?>">
                    <i class="fas fa-file-invoice w-5"></i><span>Documents</span>
                </a>
                <a href="settings.php" class="flex items-center space-x-3 px-4 py-3 rounded-lg <?php echo $current_page == 'settings.php' ? 'bg-blue-50 dark:bg-blue-900/50 text-blue-600 dark:text-blue-400' : 'text-gray-700 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-700'; ?>">
                    <i class="fas fa-cog w-5"></i><span>Settings</span>
                </a>
            </nav>
            <div class="p-4 border-t border-gray-200 dark:border-gray-700">
                <div class="flex items-center justify-between">
                    <span class="text-sm text-gray-600 dark:text-gray-300"><?php echo htmlspecialchars($_SESSION['user_name']); ?></span>
                    <a href="logout.php" class="text-red-600 dark:text-red-400 hover:text-red-700"><i class="fas fa-sign-out-alt"></i></a>
                </div>
            </div>
        </aside>
        <?php endif; ?>

        <main class="flex-1 overflow-y-auto">
            <div class="p-6">
                <div class="flex justify-between items-center mb-6">
                    <h2 class="text-2xl font-bold text-gray-800 dark:text-white"><?php echo ucfirst(str_replace('.php', '', $current_page)); ?></h2>
                    <button onclick="toggleDarkMode()" class="p-2 rounded-lg bg-gray-200 dark:bg-gray-700 text-gray-700 dark:text-gray-200">
                        <i class="fas fa-moon dark:hidden"></i><i class="fas fa-sun hidden dark:inline"></i>
                    </button>
                </div>