<?php
// api/categorias.php
header('Content-Type: application/json; charset=utf-8');
header('Access-Control-Allow-Origin: *');

require_once __DIR__ . '/../Connections/conexionabm.php';

// Obtenemos categorías distintas desde productos activos (no borrados si manejas ese flag)
$sql = "
    SELECT DISTINCT 
        cod_cat   AS id,
        categoria AS nombre
    FROM productos
    WHERE (borrado = 0 OR borrado IS NULL)
      AND categoria IS NOT NULL
      AND categoria <> ''
    ORDER BY nombre
";

$res = $mysqli->query($sql);
$cats = [];

if ($res) {
    while ($row = $res->fetch_assoc()) {
        // forzamos tipos limpios
        $cats[] = [
            'id'     => (int)$row['id'],
            'nombre' => trim($row['nombre']),
        ];
    }
    $res->free();
}

echo json_encode($cats, JSON_UNESCAPED_UNICODE);
