-- Jordan Bot Database Schema

CREATE DATABASE IF NOT EXISTS jordan_bot_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE jordan_bot_db;

-- Table to store ride requests for data mining
CREATE TABLE IF NOT EXISTS rides (
    id INT AUTO_INCREMENT PRIMARY KEY,
    fingerprint VARCHAR(255) NOT NULL,
    platform ENUM('Uber', 'Careem', 'Jeeny', 'Petra Ride', 'TaxiF', 'Unknown') DEFAULT 'Unknown',
    price DECIMAL(10, 2) DEFAULT 0.00,
    pickup_distance VARCHAR(50),
    dropoff_distance VARCHAR(50),
    time_to_pickup VARCHAR(50),
    pickup_address VARCHAR(255),
    dropoff_address VARCHAR(255),
    is_accepted TINYINT(1) DEFAULT 0,
    raw_text TEXT,
    latitude DOUBLE DEFAULT NULL,
    longitude DOUBLE DEFAULT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    INDEX (fingerprint),
    INDEX (platform)
) ENGINE=InnoDB;

-- Subscription System
CREATE TABLE IF NOT EXISTS subscriptions (
    id INT AUTO_INCREMENT PRIMARY KEY,
    fingerprint VARCHAR(255) NOT NULL,
    plan ENUM('free', 'basic', 'pro', 'annual') DEFAULT 'free',
    started_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    expires_at TIMESTAMP NULL,
    is_active TINYINT(1) DEFAULT 1,
    payment_ref VARCHAR(255),
    INDEX (fingerprint),
    INDEX (expires_at)
) ENGINE=InnoDB;

-- Daily Usage Quotas
CREATE TABLE IF NOT EXISTS daily_usage (
    id INT AUTO_INCREMENT PRIMARY KEY,
    fingerprint VARCHAR(255) NOT NULL,
    usage_date DATE NOT NULL,
    rides_accepted INT DEFAULT 0,
    UNIQUE KEY unique_daily (fingerprint, usage_date),
    INDEX (fingerprint)
) ENGINE=InnoDB;

-- Table to store driver locations for map updating
CREATE TABLE IF NOT EXISTS driver_locations (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    fingerprint VARCHAR(255) NOT NULL,
    latitude DOUBLE NOT NULL,
    longitude DOUBLE NOT NULL,
    speed FLOAT DEFAULT 0,
    recorded_at TIMESTAMP NULL,
    uploaded_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    INDEX (fingerprint),
    INDEX (recorded_at)
) ENGINE=InnoDB;
