<?php

/**
 * Dashboard Controller
 * Handles admin dashboard display
 */
class DashboardController {
    
    private $authController;
    
    public function __construct() {
        $this->authController = new AuthController();
        
        // Require authentication for all dashboard actions
        $this->authController->requireAuth();
    }
    
    /**
     * Show dashboard home page
     */
    public function index() {
        $pageTitle = 'Dashboard';
        
        // Get statistics
        $db = Database::getInstance();
        $userId = getCurrentUserId();
        
        // Count total users (only admins can see this)
        if (isAdmin()) {
            $totalUsers = $db->fetchOne("SELECT COUNT(*) as count FROM users WHERE is_active = 1")['count'] ?? 0;
        } else {
            $totalUsers = 1; // Regular users only see themselves
        }
        
        // Count total templates (same for all users)
        $totalTemplates = $db->fetchOne("SELECT COUNT(*) as count FROM templates WHERE status = 'active'")['count'] ?? 0;
        
        // Count total contacts based on user type
        if (isAdmin()) {
            $totalContacts = $db->fetchOne("SELECT COUNT(*) as count FROM contacts WHERE status = 'active'")['count'] ?? 0;
        } else {
            $totalContacts = $db->fetchOne("SELECT COUNT(*) as count FROM contacts WHERE status = 'active' AND user_id = ?", [$userId])['count'] ?? 0;
        }
        
        // Count total notes based on user type
        if (isAdmin()) {
            $totalNotes = $db->fetchOne("SELECT COUNT(*) as count FROM notes")['count'] ?? 0;
        } else {
            $totalNotes = $db->fetchOne("SELECT COUNT(*) as count FROM notes WHERE user_id = ?", [$userId])['count'] ?? 0;
        }
        
        // Get today's birthdays count based on user type
        if (isAdmin()) {
            $todayBirthdays = $db->fetchOne("
                SELECT COUNT(*) as count FROM contacts 
                WHERE status = 'active' 
                AND MONTH(date_of_birth) = MONTH(CURDATE()) 
                AND DAY(date_of_birth) = DAY(CURDATE())
            ")['count'] ?? 0;
        } else {
            $todayBirthdays = $db->fetchOne("
                SELECT COUNT(*) as count FROM contacts 
                WHERE status = 'active' 
                AND user_id = ?
                AND MONTH(date_of_birth) = MONTH(CURDATE()) 
                AND DAY(date_of_birth) = DAY(CURDATE())
            ", [$userId])['count'] ?? 0;
        }
        
        // Get today's reminders count based on user type
        if (isAdmin()) {
            $todayReminders = $db->fetchOne("
                SELECT COUNT(*) as count FROM notes 
                WHERE (
                    (note_type = 'single' AND reminder_date = CURDATE())
                    OR (note_type = 'regular' AND MONTH(reminder_date) = MONTH(CURDATE()) AND DAY(reminder_date) = DAY(CURDATE()))
                )
            ")['count'] ?? 0;
        } else {
            $todayReminders = $db->fetchOne("
                SELECT COUNT(*) as count FROM notes 
                WHERE user_id = ?
                AND (
                    (note_type = 'single' AND reminder_date = CURDATE())
                    OR (note_type = 'regular' AND MONTH(reminder_date) = MONTH(CURDATE()) AND DAY(reminder_date) = DAY(CURDATE()))
                )
            ", [$userId])['count'] ?? 0;
        }
        
        // Get recent contacts based on user type
        if (isAdmin()) {
            $recentContacts = $db->fetchAll("
                SELECT c.*, u.username 
                FROM contacts c 
                LEFT JOIN users u ON c.user_id = u.id 
                ORDER BY c.created_at DESC 
                LIMIT 5
            ");
        } else {
            $recentContacts = $db->fetchAll("
                SELECT c.*, u.username 
                FROM contacts c 
                LEFT JOIN users u ON c.user_id = u.id 
                WHERE c.user_id = ?
                ORDER BY c.created_at DESC 
                LIMIT 5
            ", [$userId]);
        }
        
        // Get upcoming birthdays (next 7 days) based on user type
        if (isAdmin()) {
            $upcomingBirthdays = $db->fetchAll("
                SELECT c.*, u.username,
                    CONCAT(
                        YEAR(CURDATE()), '-',
                        LPAD(MONTH(c.date_of_birth), 2, '0'), '-',
                        LPAD(DAY(c.date_of_birth), 2, '0')
                    ) as next_birthday
                FROM contacts c
                LEFT JOIN users u ON c.user_id = u.id
                WHERE c.status = 'active'
                HAVING next_birthday BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 7 DAY)
                ORDER BY next_birthday ASC
                LIMIT 10
            ");
        } else {
            $upcomingBirthdays = $db->fetchAll("
                SELECT c.*, u.username,
                    CONCAT(
                        YEAR(CURDATE()), '-',
                        LPAD(MONTH(c.date_of_birth), 2, '0'), '-',
                        LPAD(DAY(c.date_of_birth), 2, '0')
                    ) as next_birthday
                FROM contacts c
                LEFT JOIN users u ON c.user_id = u.id
                WHERE c.status = 'active'
                AND c.user_id = ?
                HAVING next_birthday BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 7 DAY)
                ORDER BY next_birthday ASC
                LIMIT 10
            ", [$userId]);
        }
        
        require_once APP_PATH . '/views/admin/dashboard.php';
    }
}
