<?php
require '../db.php';
require '../functions.php';

header('Content-Type: application/json');
$villager = requireVillagerToken($pdo);

try {
    $stmt = $pdo->prepare("SELECT v.fullname, v.phone, v.photo, v.address, v.houseno, v.dob, v.age,
        p.name AS panchayatname, v.status, v.paymentstatus, v.passactive, v.passstartdate, v.passenddate
        FROM villager v LEFT JOIN panchayat p ON p.id = v.panchayatid
        WHERE v.id = ? AND v.isdeleted = 0");
    $stmt->execute([$villager['id']]);
    $v = $stmt->fetch();

    if (!$v) {
        jsonOut(false, 'Profile not found');
    }

    $vehiclestmt = $pdo->prepare("SELECT vehiclenumber FROM villagervehicle WHERE villagerid = ? AND isdeleted = 0");
    $vehiclestmt->execute([$villager['id']]);
    $vehicles = array_column($vehiclestmt->fetchAll(), 'vehiclenumber');

    $v['vehicles'] = $vehicles;
    jsonOut(true, '', $v);
} catch (Exception $e) {
    logDebug('[api_getprofile] ' . $e->getMessage());
    jsonOut(false, 'Something went wrong. Please try again.');
}
