<?php
require_once '../config.php';
require_once '../../includes/Auth.php';
require_once '../../includes/Delivery.php';

$auth = new Auth();
$user = $auth->requireRole('rider');
$delivery = new Delivery();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $input = json_decode(file_get_contents('php://input'), true);
    
    $delivery_id = $input['delivery_id'] ?? null;
    
    if (!$delivery_id) {
        jsonResponse(['success' => false, 'message' => 'Delivery ID required'], 400);
    }
    
    try {
        $success = $delivery->acceptDelivery($delivery_id, $user['id']);
        
        if ($success) {
            jsonResponse([
                'success' => true,
                'message' => 'Delivery accepted successfully'
            ]);
        } else {
            jsonResponse([
                'success' => false,
                'message' => 'Failed to accept delivery'
            ], 400);
        }
    } catch (Exception $e) {
        jsonResponse([
            'success' => false,
            'message' => $e->getMessage()
        ], 500);
    }
} else {
    jsonResponse([
        'success' => false,
        'message' => 'Method not allowed'
    ], 405);
}
?>