<?php
declare(strict_types=1);

namespace App\Modules\Auth\Models;

use App\Core\App;

class ActiveSession
{
    public static function createSession(int $employeeId, string $sessionId, string $ip, string $userAgent): void
    {
        $db = App::getInstance()->db();
        $db->insert('active_sessions', [
            'employee_id'      => $employeeId,
            'session_id'       => $sessionId,
            'ip_address'       => $ip,
            'user_agent'       => mb_substr($userAgent, 0, 500),
            'logged_in_at'     => date('Y-m-d H:i:s'),
            'last_activity_at' => date('Y-m-d H:i:s'),
            'is_active'        => 1,
        ]);
    }

    public static function deactivateSession(string $sessionId): void
    {
        $db = App::getInstance()->db();
        $db->update('active_sessions', ['is_active' => 0], '`session_id` = ?', [$sessionId]);
    }

    public static function getActiveForEmployee(int $employeeId): array
    {
        $db = App::getInstance()->db();
        return $db->select(
            "SELECT * FROM active_sessions WHERE employee_id = ? AND is_active = 1 ORDER BY last_activity_at DESC",
            [$employeeId]
        );
    }
}