<?php
header('Content-Type: application/json');
require_once dirname(__FILE__) . '/../../config.php';
$db = getDB();
$rows = $db->query("
    SELECT
        MIN(productos_idprod) as min_id,
        MAX(productos_idprod) as max_id,
        COUNT(DISTINCT productos_idprod) as distinct_ids,
        SUM(CASE WHEN productos_idprod > 1000 THEN 1 ELSE 0 END) as count_gt_1000,
        SUM(CASE WHEN productos_idprod <= 1000 THEN 1 ELSE 0 END) as count_lte_1000
    FROM nsalida
")->fetch(PDO::FETCH_ASSOC);

$sample_gt1000 = $db->query("
    SELECT DISTINCT productos_idprod FROM nsalida WHERE productos_idprod > 1000 LIMIT 10
")->fetchAll(PDO::FETCH_COLUMN);

$promod_ids = $db->query("
    SELECT id_promo, promodesc, puntos FROM promod LIMIT 5
")->fetchAll(PDO::FETCH_ASSOC);

echo json_encode([
    'range' => $rows,
    'sample_gt1000' => $sample_gt1000,
    'promod_sample' => $promod_ids
], JSON_PRETTY_PRINT);
