<?php
// api/auth.php

header('Content-Type: application/json');
require_once __DIR__ . '/../config/config.php';
require_once __DIR__ . '/../src/Helpers/Auth.php';

$action = $_POST['action'] ?? $_GET['action'] ?? '';

if ($action === 'register') {
    $name = trim($_POST['name'] ?? '');
    $email = trim($_POST['email'] ?? '');
    $phone = trim($_POST['phone'] ?? '');
    $password = $_POST['password'] ?? '';
    $bio = trim($_POST['bio'] ?? '');

    if (empty($name) || empty($email) || empty($phone) || empty($password)) {
        echo json_encode(['success' => false, 'message' => 'All required fields must be filled.']);
        exit;
    }

    $result = Auth::register($name, $email, $phone, $password, $bio);
    echo json_encode($result);
    exit;
}

if ($action === 'login') {
    $email = trim($_POST['email'] ?? '');
    $password = $_POST['password'] ?? '';

    if (empty($email) || empty($password)) {
        echo json_encode(['success' => false, 'message' => 'Please provide email and password.']);
        exit;
    }

    $result = Auth::login($email, $password);
    echo json_encode($result);
    exit;
}

if ($action === 'logout') {
    Auth::logout();
    echo json_encode(['success' => true, 'message' => 'Logged out successfully.']);
    exit;
}

echo json_encode(['success' => false, 'message' => 'Invalid action.']);
