<?php
require_once __DIR__ . '/../../app/includes/auth.php';
require_once __DIR__ . '/../../app/includes/product_helpers.php';
require_auth(); require_role_access('*');

$rows = $_SESSION['product_import_errors'] ?? [];
csv_download_headers('products-import-errors-' . date('Ymd-His') . '.csv');
$out = fopen('php://output', 'w');
fputcsv($out, ['row_number', ...PRODUCT_CSV_HEADER, 'error_message']);
foreach ($rows as $row) {
    $d = $row['data'] ?? [];
    fputcsv($out, [
        $row['line'] ?? '',
        $d['product_category'] ?? '',
        $d['product_name'] ?? '',
        $d['description'] ?? '',
        $d['brand'] ?? '',
        $d['model'] ?? '',
        $d['unit'] ?? '',
        $d['unit_price'] ?? '',
        $d['status'] ?? '',
        implode(' ', $row['errors'] ?? []),
    ]);
}
exit;
