<?php
// Load environment variables from .env file if it exists
function loadEnv($file) {
    if (!file_exists($file)) {
        return;
    }
    
    $lines = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    foreach ($lines as $line) {
        if (strpos(trim($line), '#') === 0) {
            continue;
        }
        
        list($name, $value) = explode('=', $line, 2);
        $name = trim($name);
        $value = trim($value);
        
        if (!array_key_exists($name, $_SERVER) && !array_key_exists($name, $_ENV)) {
            putenv(sprintf('%s=%s', $name, $value));
            $_ENV[$name] = $value;
            $_SERVER[$name] = $value;
        }
    }
}

// Load .env file
loadEnv(__DIR__ . '/../.env');

// Now include the required files
require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../helpers/Env.php';

try {
    $db = Database::getInstance();
    
    // Check if table already exists
    $stmt = $db->prepare("SHOW TABLES LIKE 'wishlists'");
    $stmt->execute();
    
    if ($stmt->rowCount() > 0) {
        echo "INFO: Wishlist table already exists. Skipping migration.\n";
        exit(0);
    }
    
    // Create the wishlists table
    $sql = "
    CREATE TABLE wishlists (
        id INT AUTO_INCREMENT PRIMARY KEY,
        user_id INT NOT NULL,
        item_type ENUM('item', 'coin') NOT NULL,
        item_id INT NOT NULL,
        notes TEXT DEFAULT NULL,
        target_price DECIMAL(10, 2) DEFAULT NULL,
        priority ENUM('low', 'medium', 'high') DEFAULT 'medium',
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
        UNIQUE KEY unique_user_item (user_id, item_type, item_id),
        FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
        INDEX idx_user_item (user_id, item_type, item_id),
        INDEX idx_user_created (user_id, created_at DESC)
    ) ENGINE=InnoDB;
    ";
    
    $db->exec($sql);
    
    echo "SUCCESS: Wishlist table created successfully!\n";
    echo "Table 'wishlists' has been created with the following structure:\n";
    echo "- id (Primary Key, Auto Increment)\n";
    echo "- user_id (Foreign Key to users table)\n";
    echo "- item_type (ENUM: 'item' for banknotes, 'coin' for coins)\n";
    echo "- item_id (Foreign reference to items or coins table)\n";
    echo "- notes (TEXT, optional user notes)\n";
    echo "- target_price (DECIMAL, optional target price in RSD)\n";
    echo "- priority (ENUM: 'low', 'medium', 'high')\n";
    echo "- created_at (Timestamp)\n";
    echo "- updated_at (Timestamp)\n";
    echo "- Unique constraint on (user_id, item_type, item_id)\n";
    echo "- Indexes for performance optimization\n\n";
    
    echo "Wishlist functionality is now ready to use!\n";
    
} catch (PDOException $e) {
    echo "ERROR: Failed to create wishlist table: " . $e->getMessage() . "\n";
    echo "Please check your database configuration in .env file\n";
    exit(1);
} catch (Exception $e) {
    echo "ERROR: " . $e->getMessage() . "\n";
    exit(1);
}
?>
