<?php

namespace App\Controllers\Admin;

use App\Controllers\BaseController;
use App\Models\ProfileModel;
use App\Models\QRCodeModel;
use App\Models\ActivityLogModel;

/**
 * DashboardController — admin home, stats, and utilities.
 */
class DashboardController extends BaseController
{
    private ProfileModel      $profileModel;
    private QRCodeModel       $qrModel;
    private ActivityLogModel  $logModel;

    public function __construct()
    {
        $this->profileModel = new ProfileModel();
        $this->qrModel      = new QRCodeModel();
        $this->logModel     = new ActivityLogModel();
    }

    // ----------------------------------------------------------------
    // GET /admin/dashboard
    // ----------------------------------------------------------------
    public function index(): string
    {
        $data = [
            'title'          => lang('Admin.dashboard'),
            'totalProfiles'  => $this->profileModel->getTotalCount(),
            'activeProfiles' => $this->profileModel->getActiveCount(),
            'totalQRCodes'   => $this->qrModel->getTotalCount(),
            'recentProfiles' => $this->profileModel->getRecentProfiles(5),
            'recentLogs'     => $this->logModel->getRecent(10),
        ];

        return $this->render('admin/dashboard/index', $data);
    }

    // ----------------------------------------------------------------
    // GET /admin/activity-logs
    // ----------------------------------------------------------------
    public function activityLogs(): string
    {
        $data = [
            'title' => lang('Admin.activity_logs'),
            'logs'  => $this->logModel->getRecent(100),
        ];
        return $this->render('admin/dashboard/activity_logs', $data);
    }

    // ----------------------------------------------------------------
    // GET /admin/lang/:locale
    // ----------------------------------------------------------------
    public function switchLang(string $locale)
    {
        $supported = ['en', 'hi', 'mr'];
        if (in_array($locale, $supported, true)) {
            session()->set('locale', $locale);
        }
        return redirect()->back();
    }
}
