package com.jordanbot.autoride.parser

import com.jordanbot.autoride.model.RideRequest

class UberParser : NotificationParser {
    override val packageName = "com.ubercab.driver"

    override fun parse(title: String, text: String): RideRequest? {
        // Example: "New trip request • 2.50 JOD • 5 min away"
        // This is a basic regex, needs adjustment based on actual Uber notification in Jordan
        
        var price: Double? = null
        var minutes: Int? = null
        var distance: Double? = null
        var pickupAddress: String? = null
        var dropoffAddress: String? = null

        val priceRegex = """(\d+\.?\d*)\s*(JOD|د\.أ)""".toRegex()
        val minutesRegex = """(\d+)\s*(min|دقيقة)""".toRegex()
        val distanceRegex = """(\d+\.?\d*)\s*(km|كم)""".toRegex()
        
        // Patterns for addresses (e.g. "Pick up: Amman", "Drop-off: Irbid")
        val pickupAddrRegex = """(?:Pick up|من|صعود):\s*([^•\d,]+)""".toRegex(RegexOption.IGNORE_CASE)
        val dropoffAddrRegex = """(?:Drop-off|إلى|نزول):\s*([^•\d,]+)""".toRegex(RegexOption.IGNORE_CASE)

        val fullText = "$title $text"

        priceRegex.find(fullText)?.let {
            price = it.groupValues[1].toDoubleOrNull()
        }

        minutesRegex.find(fullText)?.let {
            minutes = it.groupValues[1].toIntOrNull()
        }

        distanceRegex.find(fullText)?.let {
            distance = it.groupValues[1].toDoubleOrNull()
        }

        pickupAddrRegex.find(fullText)?.let {
            pickupAddress = it.groupValues[1].trim()
        }

        dropoffAddrRegex.find(fullText)?.let {
            dropoffAddress = it.groupValues[1].trim()
        }

        return RideRequest(
            appPackage = packageName,
            priceJod = price,
            minutesAway = minutes,
            distanceKm = distance,
            pickupAddress = pickupAddress,
            dropoffAddress = dropoffAddress,
            title = title,
            text = text
        )
    }
}
