<?php

namespace App\Commands;

use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\CLI;

class SafetyCheck extends BaseCommand
{
    protected $group       = 'Safety';
    protected $name        = 'safety:check';
    protected $description = 'Checks for expired safety timers and triggers alerts.';

    public function run(array $params)
    {
        $db = \Config\Database::connect();
        $safety = new \App\Libraries\SafetyService();
    
        $expired = $db->table('active_sessions')
            ->where('expires_at <=', date('Y-m-d H:i:s'))
            ->where('status', 'ACTIVE')
            ->get()->getResult();
    
        foreach ($expired as $session) {
            // Automatically triggers the same SMS/DB logic as the app button!
            $safety->triggerEmergency($session->user_id, $session->last_location, $session->token);
        }
    }

    private function sendAlert($userId)
    {
        // Your logic to send SMS or Push Notification goes here
    }
}