<?php
include 'config.php';
$action = $_POST['action'] ?? $_GET['action'] ?? '';

if($action === 'login') {
    $stmt = $pdo->prepare("SELECT * FROM users WHERE email = ?");
    $stmt->execute([$_POST['email']]);
    $user = $stmt->fetch();
    if($user && verifyPass($_POST['pass'], $user['password'])) {
        $_SESSION['user_id'] = $user['id'];
        $_SESSION['mc_username'] = $user['mc_username'];
        echo json_encode(['success' => true]);
    } else {
        echo json_encode(['success' => false, 'msg' => 'Invalid creds']);
    }
} elseif($action === 'register') {
    // Hash pass, insert user, echo success
    $hash = hashPass($_POST['pass']);
    try {
        $stmt = $pdo->prepare("INSERT INTO users (email, mc_username, password) VALUES (?, ?, ?)");
        $stmt->execute([$_POST['email'], $_POST['mc_username'], $hash]);
        echo json_encode(['success' => true]);
    } catch(Exception $e) {
        echo json_encode(['success' => false, 'msg' => 'Email exists']);
    }
}
?>