<?php
// app/Controllers/DashboardController.php - FULL REWRITE

class DashboardController {

    public function index() {
        if (!isset($_SESSION['user_id'])) {
            header('Location: /login'); exit;
        }

        $role = $_SESSION['role'];
        $db   = getDB();

        if ($role === 'superadmin') {
            $this->superAdminDashboard($db);
        } elseif ($role === 'admin') {
            $this->adminDashboard($db);
        } elseif ($role === 'cashier') {
            header('Location: /pos'); exit;
        } elseif ($role === 'waiter') {
            header('Location: /waiter'); exit;
        } elseif ($role === 'kitchen') {
            header('Location: /kitchen'); exit;
        } elseif ($role === 'inventory') {
            header('Location: /inventory'); exit;
        } elseif ($role === 'hr_manager') {
            header('Location: /hr'); exit;
        } else {
            header('Location: /login'); exit;
        }
    }

    private function superAdminDashboard($db) {
        $pageTitle = 'Super Admin Dashboard';

        $stats = [
            'total_restaurants' => $db->query('SELECT COUNT(*) FROM restaurants')->fetchColumn(),
            'active_subs'       => $db->query('SELECT COUNT(*) FROM subscriptions WHERE status="active"')->fetchColumn(),
            'total_revenue'     => $db->query('SELECT COALESCE(SUM(amount),0) FROM subscriptions WHERE payment_status="paid"')->fetchColumn(),
            'total_users'       => $db->query('SELECT COUNT(*) FROM users')->fetchColumn(),
        ];

        $recent_restaurants = $db->query('
            SELECT r.*, p.name as package_name
            FROM restaurants r
            LEFT JOIN subscriptions s ON s.restaurant_id = r.id AND s.status = "active"
            LEFT JOIN packages p ON p.id = s.package_id
            ORDER BY r.created_at DESC LIMIT 8
        ')->fetchAll();

        // Render with layout
        ob_start();
        require 'app/Views/superadmin/dashboard/index.php';
        $content = ob_get_clean();
        require 'app/Views/layouts/superadmin.php';
    }

    private function adminDashboard($db) {
        $pageTitle  = 'Restaurant Dashboard';
        $rest_id    = $_SESSION['restaurant_id'];
        $today      = date('Y-m-d');
        $currency   = FeatureGate::currency($rest_id);

        $stats = [
            'today_revenue'   => $db->prepare('SELECT COALESCE(SUM(total),0) FROM orders WHERE restaurant_id=? AND payment_status="paid" AND DATE(created_at)=?'),
            'today_orders'    => $db->prepare('SELECT COUNT(*) FROM orders WHERE restaurant_id=? AND DATE(created_at)=?'),
            'occupied_tables' => $db->prepare('SELECT COUNT(*) FROM tables WHERE restaurant_id=? AND status="occupied"'),
            'active_sessions' => $db->prepare('SELECT COUNT(*) FROM sessions WHERE restaurant_id=? AND status="open"'),
        ];

        $stats['today_revenue']->execute([$rest_id, $today]);
        $stats['today_revenue'] = $stats['today_revenue']->fetchColumn();
        $stats['today_orders']->execute([$rest_id, $today]);
        $stats['today_orders'] = $stats['today_orders']->fetchColumn();
        $stats['occupied_tables']->execute([$rest_id]);
        $stats['occupied_tables'] = $stats['occupied_tables']->fetchColumn();
        $stats['active_sessions']->execute([$rest_id]);
        $stats['active_sessions'] = $stats['active_sessions']->fetchColumn();

        $ordStmt = $db->prepare('
            SELECT o.*, t.name as table_name
            FROM orders o
            LEFT JOIN tables t ON t.id = o.table_id
            WHERE o.restaurant_id = ? AND DATE(o.created_at) = ?
            ORDER BY o.created_at DESC LIMIT 10
        ');
        $ordStmt->execute([$rest_id, $today]);
        $recent_orders = $ordStmt->fetchAll();

        ob_start();
        require 'app/Views/admin/dashboard/index.php';
        $content = ob_get_clean();
        require 'app/Views/layouts/admin.php';
    }
}
