<?php
require_once __DIR__ . '/includes/env_loader.php';

$domain = $_ENV['APP_URL'] ?? getenv('APP_URL') ?? '';
if ($domain) {
    $parsed = parse_url($domain);
    $host = $parsed['host'] ?? '';
    if ($host) {
        session_set_cookie_params([
            'lifetime' => 0,
            'path' => '/',
            'domain' => $host,
            'secure' => true,
            'httponly' => true,
            'samesite' => 'Strict'
        ]);
    }
}

session_start();

if (isset($_SESSION['user_id'])) {
    require_once __DIR__ . '/includes/db.php';
    require_once __DIR__ . '/includes/activity_logger.php';
    
    logActivity($_SESSION['user_id'], 'logout', 'Usuario cerró sesión');
}

session_destroy();
header('Location: login.php');
exit;
