<?php
session_start();
require_once 'php.php';

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    header("Location: SS_Main_Page_.php");
    exit();
}

$email = trim($_POST['email'] ?? '');
$pass  = $_POST['password'] ?? '';

if ($email === '' || $pass === '') {
    $_SESSION['login_error'] = "Please enter email and password.";
    header("Location: SS_Main_Page_.php");
    exit();
}

$stmt = $conn->prepare("SELECT id, name, email, password FROM users WHERE email = ?");
if (!$stmt) {
    die("Prepare failed: " . $conn->error);
}

$stmt->bind_param("s", $email);
$stmt->execute();
$result = $stmt->get_result();
$user = $result->fetch_assoc();
$stmt->close();

if ($user && password_verify($pass, $user['password'])) {
    $_SESSION['user_id'] = (int)$user['id'];
    $_SESSION['name']    = $user['name'];
    $_SESSION['email']   = $user['email'];

    header("Location: SS_dashboard.php");
    exit();
}

$_SESSION['login_error'] = "Incorrect email or password.";
header("Location: SS_Main_Page_.php");
exit();
