<?php
require_once 'Database.php';

class Delivery {
    private $db;

    public function __construct() {
        $this->db = new Database();
    }

    public function createDelivery($user_id, $data) {
        // This method is called by api/deliveries/create.php
        // It's included here for completeness.
        $tracking_id = 'MBX' . strtoupper(substr(uniqid(), -8));
        $stmt = $this->db->query(
            "INSERT INTO deliveries (user_id, tracking_id, package_description, package_weight, package_value, pickup_location, delivery_location, delivery_instructions) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
            [$user_id, $tracking_id, $data['package_description'], $data['package_weight'], $data['package_value'], $data['pickup_location'], $data['delivery_location'], $data['delivery_instructions']]
        );
        return $this->db->lastInsertId();
    }

    public function getUserDeliveries($user_id, $limit = null) {
        $sql = "SELECT * FROM deliveries WHERE user_id = ? ORDER BY created_at DESC";
        $params = [$user_id];
        if ($limit) {
            $sql .= " LIMIT ?";
            $params[] = $limit;
        }
        $stmt = $this->db->query($sql, $params);
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }
}
?>