<?php
// ═══════════════════════════════════════════════════════════════
// passenger/ride/add_ride.php
// PURPOSE : إنشاء رحلة جديدة — ride DB أولاً، primary DB ثانياً
// ═══════════════════════════════════════════════════════════════

include "../../connect.php";

try {
    $con_ride = Database::get('ride');
} catch (Exception $e) {
    error_log("[add_ride] Failed to connect to Ride Database: " . $e->getMessage());
    printFailure("Database connection failed");
    exit;
}
// =================================================================================
// 🛠️ دالة مساعدة: إرسال الرحلة لسوق السائقين (Marketplace Broadcast)
// =================================================================================
function broadcastRideToMarket($rideId, $lat, $lng, $payloadData) {
    $url = getenv('LOCATION_SOCKET_URL'); 
    $keyPath = getenv('INTERNAL_SOCKET_KEY_PATH');
    $INTERNAL_KEY = $keyPath && file_exists($keyPath) ? trim(file_get_contents($keyPath)) : '';

    $marketPayload = [
        'id'            => (string)$rideId,
        'start_lat'     => $lat,
        'start_lng'     => $lng,
        'price'         => $payloadData[2],  
        'carType'       => $payloadData[31], 
        'startName'     => $payloadData[29], 
        'endName'       => $payloadData[30], 
        'distance'      => $payloadData[11], 
        'duration'      => $payloadData[15], 
        'passengerRate' => $payloadData[33], 
    ];

    $postData = [
        'action'  => 'market_new_ride', 
        'payload' => $marketPayload
    ];

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT_MS, 200); 
    if ($INTERNAL_KEY) {
        curl_setopt($ch, CURLOPT_HTTPHEADER, ["x-internal-key: $INTERNAL_KEY"]);
    }
    curl_exec($ch);
    curl_close($ch);
}
error_log("[add_ride] Request started. passenger_id=" . ($_POST['passenger_id'] ?? '?'));

// ── 1. Input ───────────────────────────────────────────────────
$start_location = filterRequest("start_location");
$end_location = filterRequest("end_location");
$price = filterRequest("price");
$passenger_id = filterRequest("passenger_id");
$driver_id = filterRequest("driver_id") ?: 0;
$status = filterRequest("status");
$price_for_driver = filterRequest("price_for_driver");
$price_for_passenger = filterRequest("price_for_passenger");
$distance = filterRequest("distance");
$carType = filterRequest("carType");
$passenger_name = filterRequest("passenger_name");
$passenger_phone = filterRequest("passenger_phone");
$passenger_token = filterRequest("passenger_token");
$passenger_email = filterRequest("passenger_email");
$passenger_wallet = filterRequest("passenger_wallet");
$passenger_rating = filterRequest("passenger_rating");
$start_name_loc = filterRequest("start_name");
$end_name_loc = filterRequest("end_name");
$duration_text = filterRequest("duration_text");
$distance_text = filterRequest("distance_text");
$is_wallet = filterRequest("is_wallet");
$has_steps = filterRequest("has_steps");
$step0 = filterRequest("step0");
$step1 = filterRequest("step1");
$step2 = filterRequest("step2");
$step3 = filterRequest("step3");
$step4 = filterRequest("step4");

// Validation
if (empty($passenger_id) || empty($start_location) || empty($end_location) || empty($price)) {
    error_log("[add_ride] Validation failed — missing required fields.");
    printFailure("Missing required fields");
    exit;
}

// ── 2. تنسيق التواريخ ─────────────────────────────────────────
$date_formatted = date("Y-m-d");
$time_formatted = date("H:i:s");
$endtime_formatted = filterRequest("endtime")
    ? date("H:i:s", strtotime(filterRequest("endtime")))
    : "00:00:00";

// ── 3. إحداثيات البداية والنهاية ──────────────────────────────
$startLat = $startLng = $endLat = $endLng = "";
if (!empty($start_location)) {
    [$startLat, $startLng] = array_map('trim', explode(',', $start_location, 2));
}
if (!empty($end_location)) {
    [$endLat, $endLng] = array_map('trim', explode(',', $end_location, 2));
}

// ── 4. مصفوفة بيانات الإدخال ──────────────────────────────────
$insertData = [
    ':start_location' => $start_location,
    ':end_location' => $end_location,
    ':date' => $date_formatted,
    ':time' => $time_formatted,
    ':endtime' => $endtime_formatted,
    ':price' => $price,
    ':passenger_id' => $passenger_id,
    ':driver_id' => $driver_id,
    ':status' => $status,
    ':carType' => $carType,
    ':price_for_driver' => $price_for_driver,
    ':price_for_passenger' => $price_for_passenger,
    ':distance' => $distance,
];

$sqlInsert = "INSERT INTO `ride`
    (`start_location`,`end_location`,`date`,`time`,`endtime`,
     `price`,`passenger_id`,`driver_id`,`status`,`carType`,
     `price_for_driver`,`price_for_passenger`,`distance`)
    VALUES
    (:start_location,:end_location,:date,:time,:endtime,
     :price,:passenger_id,:driver_id,:status,:carType,
     :price_for_driver,:price_for_passenger,:distance)";

try {
    // ═══════════════════════════════════════════════════════════
    // STEP A — ride DB أولاً (هو المرجع الأساسي)
    // ═══════════════════════════════════════════════════════════
    $stmtRide = $con_ride->prepare($sqlInsert);
    $stmtRide->execute($insertData);
    $insertedId = $con_ride->lastInsertId();

    if (!$insertedId) {
        error_log("[add_ride] ride DB insert returned no ID.");
        printFailure("Failed to create ride");
        exit;
    }

    error_log("[add_ride] ride DB insert success. RideID=$insertedId");

    // ═══════════════════════════════════════════════════════════
    // STEP B — primary DB ثانياً (نسخة أرشيفية بنفس الـ ID)
    // ═══════════════════════════════════════════════════════════
    $sqlInsertWithId = "INSERT INTO `ride`
        (`id`,`start_location`,`end_location`,`date`,`time`,`endtime`,
         `price`,`passenger_id`,`driver_id`,`status`,`carType`,
         `price_for_driver`,`price_for_passenger`,`distance`)
        VALUES
        (:id,:start_location,:end_location,:date,:time,:endtime,
         :price,:passenger_id,:driver_id,:status,:carType,
         :price_for_driver,:price_for_passenger,:distance)";

    try {
        $primaryData = $insertData;
        $primaryData[':id'] = $insertedId;
        $stmtPrimary = $con->prepare($sqlInsertWithId);
        $stmtPrimary->execute($primaryData);
        error_log("[add_ride] primary DB sync success. RideID=$insertedId");
    } catch (PDOException $ePrimary) {
        // لا نوقف العملية — ride DB هو المرجع
        error_log("[add_ride] primary DB sync WARNING: " . $ePrimary->getMessage());
    }

    // ═══════════════════════════════════════════════════════════
    // STEP C — بناء الـ payload وإرسال الرحلة للسائقين
    // ═══════════════════════════════════════════════════════════
    $kazan = (float) $price - (float) $price_for_driver;
    $payload = [
        (string) $startLat,
        (string) $startLng,
        number_format((float) $price, 2, '.', ''),
        (string) $endLat,
        (string) $endLng,
        (string) $distance_text,
        "",
        (string) $passenger_id,
        (string) $passenger_name,
        (string) $passenger_token,
        (string) $passenger_phone,
        (string) $distance,
        "1",
        (string) $is_wallet,
        (string) $distance,
        (string) $duration_text,
        (string) $insertedId,
        "",
        "",
        (string) $duration_text,
        $has_steps ?: 'false',
        (string) $step0,
        (string) $step1,
        (string) $step2,
        (string) $step3,
        (string) $step4,
        number_format((float) $price_for_driver, 2, '.', ''),
        (string) $passenger_wallet,
        (string) $passenger_email,
        (string) $start_name_loc,
        (string) $end_name_loc,
        (string) $carType,
        number_format($kazan, 2, '.', ''),
        (string) $passenger_rating,
    ];

    // Direct dispatch للسائقين القريبين
    $driversData = findBestDrivers($con, $startLat, $startLng, $carType);
    if (!empty($driversData)) {
        dispatchRideToDrivers($driversData, $insertedId, $payload, $start_name_loc, $encryptionHelper);
        error_log("[add_ride] Dispatched RideID=$insertedId to " . count($driversData) . " drivers.");
    } else {
        error_log("[add_ride] No direct drivers found for RideID=$insertedId — market only.");
    }

    // Broadcast للـ marketplace دائماً
    broadcastRideToMarket($insertedId, $startLat, $startLng, $payload);

    // رد النجاح للتطبيق
    printSuccess($insertedId);

} catch (PDOException $e) {
    error_log("[add_ride] CRITICAL ride DB error: " . $e->getMessage());
    printFailure("Database error");
}