<?php
/**
 * Ön yüz (müşteri) kontrolcüsü.
 */

function render_front(string $view, array $vars = []): void
{
    extract($vars);
    $viewFile = APP_ROOT . '/app/views/' . $view . '.php';
    ob_start();
    require $viewFile;
    $content = ob_get_clean();
    require APP_ROOT . '/app/views/layout.php';
}

function front_home(): void
{
    $categories = Database::all(
        "SELECT c.*, (SELECT COUNT(*) FROM products p WHERE p.category_id=c.id AND p.is_active=1) AS product_count
         FROM categories c WHERE c.is_active=1 ORDER BY c.sort_order, c.name"
    );
    $featured = Database::all(
        "SELECT p.*, c.name AS category_name, c.slug AS category_slug
         FROM products p LEFT JOIN categories c ON c.id=p.category_id
         WHERE p.is_active=1 AND p.is_featured=1 ORDER BY p.sort_order LIMIT 3"
    );
    $events = [];
    if (setting('show_events', '1') === '1') {
        $events = Database::all("SELECT * FROM events WHERE is_active=1 ORDER BY sort_order LIMIT 6");
    }
    render_front('home', [
        'title'      => setting('business_name', 'QR Menü'),
        'categories' => $categories,
        'featured'   => $featured,
        'events'     => $events,
    ]);
}

function front_menu(): void
{
    $categories = Database::all(
        "SELECT * FROM categories WHERE is_active=1 ORDER BY sort_order, name"
    );
    $byCat = [];
    foreach ($categories as $c) {
        $byCat[$c['id']] = [];
    }
    if ($categories) {
        $ids = array_column($categories, 'id');
        $placeholders = implode(',', array_fill(0, count($ids), '?'));
        $products = Database::all(
            "SELECT * FROM products WHERE is_active=1 AND category_id IN ($placeholders)
             ORDER BY category_id, sort_order, name",
            $ids
        );
        foreach ($products as $p) {
            $byCat[$p['category_id']][] = $p;
        }
    }
    render_front('menu', [
        'title'      => 'Menü — ' . setting('business_name', 'QR Menü'),
        'categories' => $categories,
        'byCat'      => $byCat,
    ]);
}

function front_category(array $p): void
{
    $cat = Database::one("SELECT * FROM categories WHERE slug=? AND is_active=1", [$p['slug']]);
    if (!$cat) {
        http_response_code(404);
        require APP_ROOT . '/app/views/404.php';
        return;
    }
    $products = Database::all(
        "SELECT * FROM products WHERE category_id=? AND is_active=1 ORDER BY sort_order, name",
        [$cat['id']]
    );
    render_front('category', [
        'title'    => $cat['name'] . ' — ' . setting('business_name', 'QR Menü'),
        'cat'      => $cat,
        'products' => $products,
    ]);
}

function front_product(array $p): void
{
    $product = Database::one(
        "SELECT p.*, c.name AS category_name, c.slug AS category_slug
         FROM products p LEFT JOIN categories c ON c.id=p.category_id
         WHERE p.slug=? AND p.is_active=1",
        [$p['slug']]
    );
    if (!$product) {
        http_response_code(404);
        require APP_ROOT . '/app/views/404.php';
        return;
    }
    $related = [];
    if ($product['category_id']) {
        $related = Database::all(
            "SELECT * FROM products WHERE category_id=? AND id<>? AND is_active=1 ORDER BY sort_order, id DESC LIMIT 4",
            [$product['category_id'], $product['id']]
        );
    }
    render_front('product', [
        'title'   => $product['name'] . ' — ' . setting('business_name', 'QR Menü'),
        'product' => $product,
        'related' => $related,
    ]);
}

function front_events(): void
{
    if (setting('show_events', '1') !== '1') {
        redirect('/');
    }
    $events = Database::all("SELECT * FROM events WHERE is_active=1 ORDER BY sort_order");
    render_front('events', [
        'title'  => 'Etkinlikler — ' . setting('business_name', 'QR Menü'),
        'events' => $events,
    ]);
}
