<?php
// addresses/list.php — Lista las direcciones de envío del distribuidor autenticado
declare(strict_types=1);
require_once dirname(__FILE__) . '/../config.php';

$jwt   = auth();
$idinv = (int)$jwt['idinv'];
$db    = getDB();

$stmt = $db->prepare(
    'SELECT id, id_inver, calle, noInt, noExt, entre1, entre2,
            referencia, colonia, ciudad, estado, cp, pais, status
     FROM direccionenvio
     WHERE id_inver = ?
     ORDER BY status DESC, id ASC'
);
$stmt->execute([$idinv]);
$rows = $stmt->fetchAll();

// Normalizar tipos
foreach ($rows as &$r) {
    $r['id']       = (int)$r['id'];
    $r['id_inver'] = (int)$r['id_inver'];
    $r['status']   = (int)$r['status'];
}
unset($r);

jsonOk(['direcciones' => $rows]);
