<?php

/**
 * Home Controller for PWA
 * Displays today's birthdays and reminders
 */
class HomeController {
    
    private $contactModel;
    private $noteModel;
    private $templateModel;
    
    public function __construct() {
        $this->contactModel = new Contact();
        $this->noteModel = new Note();
        $this->templateModel = new Template();
        
        // Require authentication
        $this->requireAuth();
    }
    
    /**
     * Display PWA home page with today's birthdays and reminders
     */
    public function index() {
        // Get current user ID
        $userId = getCurrentUserId();
        
        // Get today's birthdays based on user type
        if (isAdmin()) {
            $todayBirthdays = $this->contactModel->getAllTodayBirthdays();
        } else {
            $todayBirthdays = $this->contactModel->getTodayBirthdays($userId);
        }
        
        // Get next 3 days birthdays
        if (isAdmin()) {
            // For admin, we need to get all upcoming birthdays
            $nextThreeDaysBirthdays = [
                'day1' => [],
                'day2' => [],
                'day3' => []
            ];
            $allUpcoming = $this->contactModel->getUpcomingBirthdays(3);
            // Group them by day (simplified for admin)
            foreach ($allUpcoming as $contact) {
                $nextThreeDaysBirthdays['day1'][] = $contact;
            }
        } else {
            $nextThreeDaysBirthdays = $this->contactModel->getNextThreeDaysBirthdays($userId);
        }
        
        // Get today's reminders based on user type
        if (isAdmin()) {
            $todayReminders = $this->noteModel->getAllTodayNotes();
        } else {
            $todayReminders = $this->noteModel->getTodayReminders($userId);
        }
        
        // Get user's default template
        $defaultTemplate = $this->templateModel->getUserDefaultTemplate($userId);
        
        // Get all active templates for selection
        $activeTemplates = $this->templateModel->getActive();
        
        // Pass data to view
        require_once APP_PATH . '/views/pwa/home.php';
    }
    
    /**
     * Require authentication for PWA
     */
    private function requireAuth() {
        if (!isAuthenticated()) {
            redirect(BASE_URL . '/login');
            exit;
        }
    }
}
