<?php
require_once __DIR__ . '/../models/Company.php';
require_once __DIR__ . '/../models/User.php';
require_once __DIR__ . '/../models/Vehicle.php';
require_once __DIR__ . '/../models/Customer.php';
require_once __DIR__ . '/../models/Service.php';
require_once __DIR__ . '/../models/Reminder.php';

class ApiController {
    public function getStats($f3) {
        header('Content-Type: application/json');
        
        if (!isset($_SESSION['user'])) {
            http_response_code(401);
            echo json_encode(['success' => false, 'message' => 'Unauthorized']);
            return;
        }
        
        $db = $f3->get('DB');
        $role = $_SESSION['user']['role'];
        $companyId = $_SESSION['user']['company_id'];
        
        $stats = [];
        
        if ($role === 'super_admin') {
            $companyModel = new \Models\Company($db);
            $userModel = new \Models\User($db);
            $domainModel = new \Models\Vehicle($db);
            
            $allCompanies = $companyModel->findAll();
            $activeCompanies = array_filter($allCompanies, fn($c) => $c['is_active']);
            
            $stats = [
                'total_companies' => count($allCompanies),
                'active_companies' => count($activeCompanies),
                'total_users' => $userModel->countAll(),
                'total_vehicles' => $domainModel->countAll()
            ];
        } else if ($role === 'company_admin' || $role === 'staff') {
            $customerModel = new \Models\Customer($db);
            $domainModel = new \Models\Vehicle($db);
            $reminderModel = new \Models\Reminder($db);
            
            $stats = [
                'total_customers' => $customerModel->countByCompany($companyId),
                'total_vehicles' => $domainModel->countByCompany($companyId),
                'pending_reminders' => $reminderModel->countPendingByCompany($companyId)
            ];
        }
        
        echo json_encode(['success' => true, 'stats' => $stats]);
    }
    
    public function getDashboardData($f3) {
        header('Content-Type: application/json');
        
        if (!isset($_SESSION['user'])) {
            http_response_code(401);
            echo json_encode(['success' => false, 'message' => 'Unauthorized']);
            return;
        }
        
        $db = $f3->get('DB');
        $role = $_SESSION['user']['role'];
        $companyId = $_SESSION['user']['company_id'];
        
        $data = [];
        
        if ($role === 'company_admin' || $role === 'staff') {
            $serviceModel = new \Models\Service($db);
            
            $data['today_services'] = $serviceModel->countTodayByCompany($companyId);
            $data['overdue_services'] = $serviceModel->countOverdueByCompany($companyId);
            $data['upcoming_services'] = array_slice($serviceModel->findUpcomingByCompany($companyId, 30), 0, 10);
        }
        
        echo json_encode(['success' => true, 'data' => $data]);
    }
}
