<?php
require_once 'includes/auth_check.php';
require_once 'includes/db_connect.php';

$user_id = $_SESSION['user_id'];

// Statistiken für das Dashboard laden
$stats = [
    'zones' => 0,
    'seeds' => 0,
    'plants' => 0
];

// Zonen zählen
$result_zones = $mysqli->query("SELECT COUNT(*) as count FROM zones WHERE user_id = $user_id");
if($result_zones) {
    $stats['zones'] = $result_zones->fetch_assoc()['count'];
}

// Samen zählen
$result_seeds = $mysqli->query("SELECT SUM(stock_count) as count FROM seeds WHERE user_id = $user_id");
if($result_seeds) {
    $seed_count_result = $result_seeds->fetch_assoc()['count'];
    $stats['seeds'] = $seed_count_result ?? 0;
}

// Aktive Pflanzen zählen
$result_plants = $mysqli->query("SELECT COUNT(*) as count FROM plants WHERE user_id = $user_id AND status = 'Eingepflanzt'");
if($result_plants) {
    $stats['plants'] = $result_plants->fetch_assoc()['count'];
}


require_once 'includes/header.php'; 
?>

<div class="card header-card mb-4">
    <div class="card-body text-center">
        <h1 class="display-4">Willkommen, <?php echo htmlspecialchars($_SESSION["username"]); ?>!</h1>
        <p class="lead text-white-50">Was möchtest du als Nächstes tun?</p>
    </div>
</div>

<div class="row">
    <div class="col-lg-4 mb-4 d-flex">
        <table class="table cazubu-table-frameless">
            <thead class="table-dark"><tr><th class="text-center">Schritt 1: Inventar</th></tr></thead>
            <tbody>
                <tr>
                    <td class="d-flex flex-column align-items-center text-center p-4">
                        <p class="text-muted">Lege deine Anbau-Zonen und Pflanzgefäße an.</p>
                        <?php if ($stats['zones'] > 0): ?>
                            <div class="my-auto">
                                <div class="display-4 fw-bold text-dark"><?php echo $stats['zones']; ?></div>
                                <div class="h5 text-dark">Zone(n) angelegt</div>
                            </div>
                            <a href="inventory.php" class="btn btn-outline-dark mt-3">Zum Inventar</a>
                        <?php else: ?>
                            <div class="my-auto p-4">
                                <a href="inventory.php" class="btn btn-cazubu btn-lg mt-2">Inventar anlegen</a>
                            </div>
                        <?php endif; ?>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>

    <div class="col-lg-4 mb-4 d-flex">
        <table class="table cazubu-table-frameless">
            <thead class="table-dark"><tr><th class="text-center">Schritt 2: Samenbank</th></tr></thead>
            <tbody>
                <tr>
                    <td class="d-flex flex-column align-items-center text-center p-4">
                        <p class="text-muted">Erfasse alle Samen, die du auf Lager hast.</p>
                         <?php if ($stats['seeds'] > 0): ?>
                            <div class="my-auto">
                                <div class="display-4 fw-bold text-dark"><?php echo $stats['seeds']; ?></div>
                                <div class="h5 text-dark">Samen auf Lager</div>
                            </div>
                            <a href="seeds.php" class="btn btn-outline-dark mt-3">Zur Samenbank</a>
                        <?php else: ?>
                            <div class="my-auto p-4">
                                <a href="seeds.php" class="btn btn-cazubu btn-lg mt-2">Samen erfassen</a>
                            </div>
                        <?php endif; ?>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>

    <div class="col-lg-4 mb-4 d-flex">
        <table class="table cazubu-table-frameless">
            <thead class="table-dark"><tr><th class="text-center">Schritt 3: Pflanzen</th></tr></thead>
            <tbody>
                <tr>
                    <td class="d-flex flex-column align-items-center text-center p-4">
                        <p class="text-muted">Lege deine Pflanzen an und verfolge ihren Fortschritt.</p>
                         <?php if ($stats['plants'] > 0): ?>
                            <div class="my-auto">
                                <div class="display-4 fw-bold text-dark"><?php echo $stats['plants']; ?></div>
                                <div class="h5 text-dark">Pflanze(n) wachsen</div>
                            </div>
                            <a href="plants.php" class="btn btn-outline-dark mt-3">Zu den Pflanzen</a>
                        <?php else: ?>
                             <div class="my-auto p-4">
                                <a href="plants.php" class="btn btn-cazubu btn-lg mt-2">+ Erste Pflanze anlegen</a>
                            </div>
                        <?php endif; ?>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</div>

<?php 
require_once 'includes/footer.php'; 
?>
