

-- Tabela zamówień Uber Eats
CREATE TABLE IF NOT EXISTS ubereats_orders (
    id INT AUTO_INCREMENT PRIMARY KEY,
    order_number VARCHAR(20) NOT NULL UNIQUE,
    user_id INT,
    customer_name VARCHAR(100),
    customer_phone VARCHAR(20),
    customer_email VARCHAR(100),
    
    -- Szczegóły dostawy
    delivery_address VARCHAR(255) NOT NULL,
    delivery_city VARCHAR(100) DEFAULT 'Los Santos',
    delivery_notes TEXT,
    
    -- Restauracja
    restaurant_name VARCHAR(100) DEFAULT 'Burgershot',
    
    -- Status
    status ENUM('pending', 'confirmed', 'preparing', 'ready_for_pickup', 'picked_up', 'delivering', 'delivered', 'cancelled') DEFAULT 'pending',
    
    -- Przypisany kierowca
    driver_id INT,
    driver_assigned_at TIMESTAMP NULL,
    picked_up_at TIMESTAMP NULL,
    delivered_at TIMESTAMP NULL,
    
    -- Płatności
    payment_method ENUM('card', 'cash', 'paypal') DEFAULT 'card',
    payment_status ENUM('pending', 'paid', 'refunded') DEFAULT 'pending',
    
    -- Kwoty
    subtotal DECIMAL(10,2) NOT NULL,
    delivery_fee DECIMAL(10,2) DEFAULT 2.99,
    service_fee DECIMAL(10,2) DEFAULT 0.99,
    tip DECIMAL(10,2) DEFAULT 0.00,
    total DECIMAL(10,2) NOT NULL,
    
    -- Driver earnings
    driver_earnings DECIMAL(10,2) DEFAULT 0.00,
    
    -- Ocena
    customer_rating INT,
    customer_review TEXT,
    
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

-- Tabela pozycji zamówienia
CREATE TABLE IF NOT EXISTS ubereats_order_items (
    id INT AUTO_INCREMENT PRIMARY KEY,
    order_id INT NOT NULL,
    product_id INT,
    product_name VARCHAR(100) NOT NULL,
    quantity INT NOT NULL DEFAULT 1,
    unit_price DECIMAL(8,2) NOT NULL,
    total_price DECIMAL(10,2) NOT NULL,
    notes TEXT,
    FOREIGN KEY (order_id) REFERENCES ubereats_orders(id) ON DELETE CASCADE
);

-- Tabela historii statusów zamówienia
CREATE TABLE IF NOT EXISTS ubereats_order_history (
    id INT AUTO_INCREMENT PRIMARY KEY,
    order_id INT NOT NULL,
    status VARCHAR(50) NOT NULL,
    changed_by VARCHAR(100),
    notes TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (order_id) REFERENCES ubereats_orders(id) ON DELETE CASCADE
);

-- Tabela sesji kierowców (lokalizacja, dostępność)
CREATE TABLE IF NOT EXISTS ubereats_driver_sessions (
    id INT AUTO_INCREMENT PRIMARY KEY,
    driver_id INT NOT NULL,
    status ENUM('online', 'offline') DEFAULT 'offline',
    current_lat DECIMAL(10,8),
    current_lng DECIMAL(11,8),
    last_location_update TIMESTAMP,
    session_start TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    session_end TIMESTAMP NULL,
    FOREIGN KEY (driver_id) REFERENCES ubereats_employees(id) ON DELETE CASCADE
);

-- ========================================
-- INDEKSY
-- ========================================

CREATE INDEX idx_ubereats_orders_status ON ubereats_orders(status);
CREATE INDEX idx_ubereats_orders_driver ON ubereats_orders(driver_id);
CREATE INDEX idx_ubereats_orders_user ON ubereats_orders(user_id);
CREATE INDEX idx_ubereats_orders_created ON ubereats_orders(created_at);

SELECT 'Uber Eats database setup completed!' as message;
