<?php
namespace App\Controllers;

use App\Core\Controller;
use App\Models\Contact;
use App\Models\Conversation;
use App\Models\Message;
use App\Models\Deal;
use App\Models\PipelineStage;

class DashboardController extends Controller {
    public function index(): void {
        $totalContacts = Contact::count();
        $openConversations = Conversation::count("status = 'open'");
        $unreadCount = Conversation::count("unread_count > 0");

        $messageMetrics = Message::getMetrics();
        $pipelineSummary = Deal::getPipelineSummary();
        $stagesWithDeals = PipelineStage::getStagesWithDeals();
        $recentConversations = Conversation::getInboxList(null, 'all');
        $recentConversations = array_slice($recentConversations, 0, 5);

        $this->render('dashboard/index', [
            'totalContacts'       => $totalContacts,
            'openConversations'   => $openConversations,
            'unreadCount'         => $unreadCount,
            'messageMetrics'      => $messageMetrics,
            'pipelineSummary'     => $pipelineSummary,
            'stagesWithDeals'     => $stagesWithDeals,
            'recentConversations' => $recentConversations
        ]);
    }
}
