<?php
// api/auth/check.php

session_start();

header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Credentials: true');

$response = [
    'authenticated' => false,
    'user' => null,
    'session_id' => session_id(),
    'timestamp' => time()
];

if (isset($_SESSION['member_id']) && $_SESSION['authenticated'] === true) {
    $response['authenticated'] = true;
    $response['user'] = [
        'member_id' => $_SESSION['member_id'],
        'username' => $_SESSION['username'] ?? '',
        'email' => $_SESSION['email'] ?? ''
    ];
    
    // Update last activity
    $_SESSION['last_activity'] = time();
}

echo json_encode($response);