<?php
/**
 * Migration script to add SR NO (Serial Number) to all existing products
 * This script runs once to assign unique SR NO to every product based on their ID
 */

session_start();

// Check if user is admin
if (empty($_SESSION['is_admin']) && (empty($_SESSION['user_type']) || $_SESSION['user_type'] !== 'admin')) {
    die('Access denied. Admin login required.');
}

$dataFile = __DIR__ . '/data/products.json';
$migrationFlagFile = __DIR__ . '/data/.sr_no_migration_done';

// Check if migration already done
if (file_exists($migrationFlagFile)) {
    die('<div style="padding:20px; text-align:center; font-family:Arial;">
        <h2>✓ Migration Already Complete</h2>
        <p>SR NO has already been assigned to all products.</p>
        <p><a href="admin_products.php">← Back to Products</a></p>
    </div>');
}

if (!file_exists($dataFile)) {
    die('Products file not found.');
}

$products = json_decode(file_get_contents($dataFile), true);
if (!is_array($products)) {
    die('Failed to load products data.');
}

// Assign SR NO to products
$maxSrNo = 0;
foreach ($products as &$product) {
    // If product doesn't have sr_no, assign one based on ID
    if (empty($product['sr_no'])) {
        $product['sr_no'] = $product['id'];
        if ($product['id'] > $maxSrNo) {
            $maxSrNo = $product['id'];
        }
    } else {
        if ($product['sr_no'] > $maxSrNo) {
            $maxSrNo = $product['sr_no'];
        }
    }
}
unset($product);

// Save updated products
$saveResult = file_put_contents($dataFile, json_encode($products, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));

if ($saveResult !== false) {
    // Create migration flag file
    file_put_contents($migrationFlagFile, date('Y-m-d H:i:s') . ' - SR NO migration completed');
    
    echo '<div style="padding:40px; text-align:center; font-family:Arial; max-width:600px; margin:50px auto; background:#f0fff4; border:2px solid #48bb78; border-radius:8px;">';
    echo '<h1 style="color:#22543d; margin-bottom:20px;">✓ Migration Successful!</h1>';
    echo '<p style="color:#276749; font-size:16px; margin-bottom:20px;">SR NO has been assigned to all ' . count($products) . ' products.</p>';
    echo '<p style="color:#22543d; margin-bottom:30px;"><strong>Max SR NO: ' . $maxSrNo . '</strong></p>';
    echo '<p style="color:#276749; margin-bottom:30px;">All products now have unique serial numbers for QR code generation.</p>';
    echo '<a href="admin_products.php" style="display:inline-block; padding:12px 24px; background:#48bb78; color:#fff; text-decoration:none; border-radius:6px; font-weight:bold;">← Back to Products</a>';
    echo '</div>';
} else {
    echo '<div style="padding:40px; text-align:center; font-family:Arial; max-width:600px; margin:50px auto; background:#fff5f5; border:2px solid #f56565; border-radius:8px;">';
    echo '<h1 style="color:#742a2a;">✗ Migration Failed</h1>';
    echo '<p style="color:#742a2a;">Could not save products file. Check file permissions.</p>';
    echo '<a href="admin_products.php" style="display:inline-block; padding:12px 24px; background:#f56565; color:#fff; text-decoration:none; border-radius:6px; font-weight:bold; margin-top:20px;">← Back to Products</a>';
    echo '</div>';
}
?>
