<?php
session_start();

require_once '../includes/database.php';
require_once '../includes/multi-tenant-auth.php';

$database = new Database();
$pdo = $database->getConnection();
$auth = new MultiTenantAuth($pdo);

// Log the logout action if we have a valid session
if (isset($_SESSION['admin_session_id'], $_SESSION['admin_data'])) {
    $adminData = $_SESSION['admin_data'];
    $adminId = $adminData['id'] ?? 0;
    
    if ($adminId) {
        $auth->logActivity($adminId, 'logout', 'Admin logged out');
    }
    
    // Destroy the session in database
    $auth->logout($_SESSION['admin_session_id']);
}

// Clear all session data
session_destroy();

// Clear any cookies
if (isset($_COOKIE['admin_remember'])) {
    setcookie('admin_remember', '', time() - 3600, '/');
}

// Redirect to login page with success message
header('Location: login-multi.php?message=' . urlencode('Anda telah berhasil logout'));
exit;
?>