<?php
require_once __DIR__ . '/../Core.php';
require_once __DIR__ . '/../models/Contact.php';
require_once __DIR__ . '/../models/Template.php';
require_once __DIR__ . '/../models/Message.php';
require_once __DIR__ . '/../models/Campaign.php';
require_once __DIR__ . '/../models/User.php';
require_once __DIR__ . '/../models/AuditLog.php';
require_once __DIR__ . '/../models/QueueJob.php';
require_once __DIR__ . '/../models/Segment.php';
require_once __DIR__ . '/../models/Suppression.php';
require_once __DIR__ . '/../models/Workflow.php';
require_once __DIR__ . '/../models/Reminder.php';
require_once __DIR__ . '/../models/Account.php';
require_once __DIR__ . '/../models/Setting.php';
require_once __DIR__ . '/../models/LoginHistory.php';

class DashboardController extends Controller
{
    public function index(): void
    {
        require_permission('dashboard.view');
        $messageModel = new Message();
        $contactModel = new Contact();
        $campaignModel = new Campaign();
        $segmentModel = new Segment();
        $segments = [];
        try {
            $segments = $segmentModel->all();
            foreach ($segments as &$segment) {
                $segment['member_count'] = $segmentModel->countMembers((int)$segment['id']);
            }
        } catch (Throwable $e) {
            $segments = [];
        }
        $queue = new QueueJob();
        $settingModel = new Setting();
        $providerTestResults = json_decode((string)$settingModel->get('provider_last_test_results_json', '[]'), true) ?: [];
        $this->view('dashboard/index', [
            'contactCounts' => $contactModel->counts(),
            'templateCount' => count((new Template())->all()),
            'messageStats' => $messageModel->statsLast30Days(),
            'delivery' => $messageModel->deliverySummary(),
            'messages' => $messageModel->recent(12),
            'campaigns' => array_slice($campaignModel->all(), 0, 8),
            'campaignRollup' => $campaignModel->rollup(),
            'campaignExecution' => $campaignModel->executionSummary(),
            'users' => array_slice((new User())->all(), 0, 8),
            'analytics' => $messageModel->analyticsDaily(14),
            'auditLogs' => (new AuditLog())->recent(12),
            'queueStats' => $queue->stats(),
            'queueJobs' => $queue->recent(10),
            'deadLetters' => $queue->deadLetters(8),
            'segments' => array_slice($segments, 0, 8),
            'suppressions' => array_slice((new Suppression())->all(), 0, 8),
            'heartbeat' => worker_heartbeat(),
            'schemaVersion' => schema_version(),
            'appVersion' => app_version(),
            'schemaMatches' => schema_matches_app_version(),
            'appVersionSource' => app_version_source(),
            'workflowStats' => (new Workflow())->stats(),
            'stageBreakdown' => $contactModel->stageBreakdown(),
            'pipelineBreakdown' => $contactModel->pipelineBreakdown(),
            'pipelineStageReport' => $contactModel->pipelineStageReport(),
            'pipelineStageMatrix' => $contactModel->pipelineStageMatrix(),
            'pipelineOwnerReport' => $contactModel->pipelineOwnerReport(10),
            'accountBreakdown' => $contactModel->accountBreakdown(),
            'reminders' => (new Reminder())->upcoming(8),
            'providerStatus' => provider_status_summary(),
            'providerTestResults' => $providerTestResults,
            'quietHours' => quiet_hours_settings(),
            'loginHistory' => (new LoginHistory())->recent(8),
        ]);
    }
}
