<?php

/**
 * AJAX Controller
 * Handles AJAX requests for search and pagination
 */
class AjaxController {
    
    private $templateModel;
    private $userModel;
    private $contactModel;
    private $noteModel;
    
    public function __construct() {
        $this->templateModel = new Template();
        $this->userModel = new User();
        $this->contactModel = new Contact();
        $this->noteModel = new Note();
    }
    
    /**
     * Search templates via AJAX
     */
    public function searchTemplates() {
        header('Content-Type: application/json');
        
        $search = sanitize($_GET['search'] ?? '');
        $page = isset($_GET['page']) ? max(1, intval($_GET['page'])) : 1;
        $perPage = 10;
        $offset = ($page - 1) * $perPage;
        
        if (!empty($search)) {
            $templates = $this->templateModel->searchPaginated($search, $perPage, $offset);
            $total = $this->templateModel->countSearch($search);
        } else {
            $templates = $this->templateModel->getPaginated($perPage, $offset);
            $total = $this->templateModel->countTotal();
        }
        
        $totalPages = ceil($total / $perPage);
        
        echo json_encode([
            'success' => true,
            'data' => $templates,
            'pagination' => [
                'current_page' => $page,
                'total_pages' => $totalPages,
                'total_items' => $total,
                'per_page' => $perPage
            ]
        ]);
    }
    
    /**
     * Search users via AJAX
     */
    public function searchUsers() {
        header('Content-Type: application/json');
        
        $search = sanitize($_GET['search'] ?? '');
        $page = isset($_GET['page']) ? max(1, intval($_GET['page'])) : 1;
        $perPage = 10;
        $offset = ($page - 1) * $perPage;
        
        if (!empty($search)) {
            $users = $this->userModel->searchPaginated($search, $perPage, $offset);
            $total = $this->userModel->countSearch($search);
        } else {
            $users = $this->userModel->getPaginated($perPage, $offset);
            $total = $this->userModel->countTotal();
        }
        
        $totalPages = ceil($total / $perPage);
        
        echo json_encode([
            'success' => true,
            'data' => $users,
            'pagination' => [
                'current_page' => $page,
                'total_pages' => $totalPages,
                'total_items' => $total,
                'per_page' => $perPage
            ]
        ]);
    }
    
    /**
     * Search contacts via AJAX
     */
    public function searchContacts() {
        header('Content-Type: application/json');
        
        $search = sanitize($_GET['search'] ?? '');
        $page = isset($_GET['page']) ? max(1, intval($_GET['page'])) : 1;
        $perPage = 10;
        $offset = ($page - 1) * $perPage;
        
        // Get user ID for filtering
        $userId = isAdmin() ? null : getCurrentUserId();
        
        if (!empty($search)) {
            $contacts = $this->contactModel->searchPaginated($search, $perPage, $offset, $userId);
            $total = $this->contactModel->countSearch($search);
        } else {
            $contacts = $this->contactModel->getPaginated($perPage, $offset, $userId);
            $total = $this->contactModel->countTotal();
        }
        
        $totalPages = ceil($total / $perPage);
        
        echo json_encode([
            'success' => true,
            'data' => $contacts,
            'pagination' => [
                'current_page' => $page,
                'total_pages' => $totalPages,
                'total_items' => $total,
                'per_page' => $perPage
            ]
        ]);
    }
    
    /**
     * Search notes via AJAX
     */
    public function searchNotes() {
        header('Content-Type: application/json');
        
        $search = sanitize($_GET['search'] ?? '');
        $page = isset($_GET['page']) ? max(1, intval($_GET['page'])) : 1;
        $perPage = 10;
        $offset = ($page - 1) * $perPage;
        
        // Get user ID for filtering
        $userId = isAdmin() ? null : getCurrentUserId();
        
        if (!empty($search)) {
            $notes = $this->noteModel->searchPaginated($search, $perPage, $offset, $userId);
            $total = $this->noteModel->countSearch($search);
        } else {
            $notes = $this->noteModel->getPaginated($perPage, $offset, $userId);
            $total = $this->noteModel->countTotal();
        }
        
        $totalPages = ceil($total / $perPage);
        
        echo json_encode([
            'success' => true,
            'data' => $notes,
            'pagination' => [
                'current_page' => $page,
                'total_pages' => $totalPages,
                'total_items' => $total,
                'per_page' => $perPage
            ]
        ]);
    }
}
