<?php
require 'includes/db.php';
try {
    // Drop category
    $stmt = $pdo->query("DESCRIBE products");
    $columns = $stmt->fetchAll(PDO::FETCH_COLUMN);
    
    if (in_array('category', $columns)) {
        $pdo->exec("ALTER TABLE products DROP COLUMN category");
    }
    
    if (!in_array('units', $columns)) {
        $pdo->exec("ALTER TABLE products ADD COLUMN units INT DEFAULT 0 AFTER base_price");
    }
    
    $pdo->exec("ALTER TABLE products MODIFY COLUMN sku VARCHAR(50) NOT NULL");
    
    // Check if unique index exists
    $stmt = $pdo->query("SHOW INDEX FROM products WHERE Column_name = 'sku' AND Non_unique = 0");
    if (!$stmt->fetch()) {
        $pdo->exec("CREATE UNIQUE INDEX idx_sku_unique ON products(sku)");
    }
    
    echo "Products table updated successfully.";
} catch (Exception $e) {
    echo "Error: " . $e->getMessage();
}
