<?php
require_once __DIR__ . '/../inc/helpers.php';
require_once __DIR__ . '/../inc/docs.php';
require_login();
ensure_part2_tables();
if(!is_admin()){ http_response_code(403); exit('Solo admin'); }

header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename="clientes.csv"');

$out = fopen('php://output', 'w');
fputcsv($out, ['id','customer_number','nombre','apellidos','direccion','nif_cif','telefono','created_at']);
$st = db()->query("SELECT id, customer_number, nombre, apellidos, direccion, nif_cif, telefono, created_at FROM clients ORDER BY id ASC");
while($row = $st->fetch()){
  fputcsv($out, $row);
}
fclose($out);
exit;
