<?php
require_once __DIR__ . '/../../app/includes/auth.php';
require_once __DIR__ . '/../../app/includes/product_helpers.php';
require_auth();
if (!can_export_products()) {
    http_response_code(403);
    exit('Forbidden');
}
$products = fetch_filtered_products($_GET);
csv_download_headers('products-export-' . date('Ymd-His') . '.csv');
$out = fopen('php://output', 'w');
fputcsv($out, PRODUCT_CSV_HEADER);
foreach ($products as $p) {
    fputcsv($out, [$p['product_category'], $p['product_name'], $p['description'], $p['brand'], $p['model'], $p['unit'], $p['unit_price'], $p['status']]);
}
log_activity('product_exported', 'Exported products CSV with ' . count($products) . ' products.');
exit;
