<?php
/**
 * Create table for store-specific Paystack credentials
 * Run this once to set up the database structure
 */

require_once 'config.php';

echo "Creating store_paystack_credentials table...\n";

$sql = "CREATE TABLE IF NOT EXISTS store_paystack_credentials (
    id INT AUTO_INCREMENT PRIMARY KEY,
    store_id INT NOT NULL UNIQUE,
    public_key VARCHAR(255) NOT NULL,
    secret_key VARCHAR(255) NOT NULL,
    is_active TINYINT(1) DEFAULT 1,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (store_id) REFERENCES stores(id) ON DELETE CASCADE,
    INDEX idx_store_id (store_id),
    INDEX idx_is_active (is_active)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";

if ($conn->query($sql)) {
    echo "✓ Table 'store_paystack_credentials' created successfully!\n";
} else {
    echo "✗ Error creating table: " . $conn->error . "\n";
    exit(1);
}

// Add payment_method column to orders table if it doesn't exist
echo "\nChecking orders table for payment_method column...\n";
$checkCol = $conn->query("SHOW COLUMNS FROM orders LIKE 'payment_method'");
if ($checkCol->num_rows == 0) {
    $alterSql = "ALTER TABLE orders ADD COLUMN payment_method VARCHAR(50) DEFAULT 'mobile_money' AFTER status";
    if ($conn->query($alterSql)) {
        echo "✓ Added 'payment_method' column to orders table\n";
    } else {
        echo "⚠ Warning: Could not add payment_method column: " . $conn->error . "\n";
    }
} else {
    echo "✓ 'payment_method' column already exists\n";
}

// Add paystack_reference column to orders table if it doesn't exist
echo "\nChecking orders table for paystack_reference column...\n";
$checkCol2 = $conn->query("SHOW COLUMNS FROM orders LIKE 'paystack_reference'");
if ($checkCol2->num_rows == 0) {
    $alterSql2 = "ALTER TABLE orders ADD COLUMN paystack_reference VARCHAR(100) NULL AFTER payment_method";
    if ($conn->query($alterSql2)) {
        echo "✓ Added 'paystack_reference' column to orders table\n";
    } else {
        echo "⚠ Warning: Could not add paystack_reference column: " . $conn->error . "\n";
    }
} else {
    echo "✓ 'paystack_reference' column already exists\n";
}

echo "\n✅ Database setup complete!\n";
?>



























