<?php
header('Content-Type: application/json');
require_once '../config/database.php';

$login = trim($_GET["u"] ?? "");
if (strlen($login) < 2) { echo json_encode(["found"=>false]); exit; }

try {
    $st = $pdo->prepare("
        SELECT u.name, u.profile_photo, c.customer_code
        FROM users u
        LEFT JOIN customers c ON c.user_id = u.id
        WHERE (u.email=? OR u.username=?) AND u.status='active'
        LIMIT 1
    ");
    $st->execute([$login, $login]);
    $user = $st->fetch(PDO::FETCH_ASSOC);

    if ($user) {
        echo json_encode([
            "found"         => true,
            "name"          => $user["name"],
            "photo"         => $user["profile_photo"],
            "customer_code" => $user["customer_code"],
            "initial"       => strtoupper(mb_substr($user["name"], 0, 1)),
        ]);
    } else {
        echo json_encode(["found" => false]);
    }
} catch (Throwable $e) {
    echo json_encode(["found" => false]);
}