-- Create database for AliExpress orders
CREATE DATABASE IF NOT EXISTS aliexpress 
CHARACTER SET utf8mb4 
COLLATE utf8mb4_unicode_ci;

USE aliexpress;

-- Create items table
CREATE TABLE IF NOT EXISTS items (
    id INT AUTO_INCREMENT PRIMARY KEY,
    orderDate DATE COMMENT 'Order date in US format (YYYY-MM-DD)',
    orderNumber VARCHAR(20) COMMENT 'Order number (16 digits)',
    orderURL VARCHAR(500) COMMENT 'URL to order detail page',
    itemDesc TEXT COMMENT 'Item description',
    itemPrice DECIMAL(10, 2) COMMENT 'Item price in EUR',
    itemQuantity INT COMMENT 'Quantity ordered',
    itemImageURL VARCHAR(500) COMMENT 'URL to item image',
    orderTotal DECIMAL(10, 2) COMMENT 'Total order price in EUR',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT 'Record creation timestamp',
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'Record update timestamp',
    
    INDEX idx_orderNumber (orderNumber),
    INDEX idx_orderDate (orderDate),
    INDEX idx_created_at (created_at)
) ENGINE=InnoDB 
DEFAULT CHARSET=utf8mb4 
COLLATE=utf8mb4_unicode_ci
COMMENT='AliExpress order items extracted from HTML';

-- Display table structure
DESCRIBE items;

-- Example queries to use after data import:

-- Select all orders
-- SELECT * FROM items ORDER BY orderDate DESC;

-- Select orders by date range
-- SELECT * FROM items WHERE orderDate BETWEEN '2025-12-01' AND '2026-01-31';

-- Select orders by order number
-- SELECT * FROM items WHERE orderNumber = '3066436169351201';

-- Calculate total spending
-- SELECT SUM(orderTotal) as total_spent FROM items;

-- Count orders by month
-- SELECT DATE_FORMAT(orderDate, '%Y-%m') as month, COUNT(DISTINCT orderNumber) as order_count
-- FROM items
-- GROUP BY month
-- ORDER BY month DESC;
