<?php
namespace App\Controllers;

use App\Services\PurchaseService;
use App\Repositories\CarRepository;
use Exception;

class PurchaseController {
    protected $purchaseService;
    protected $carRepo;

    public function __construct() {
        $this->purchaseService = new PurchaseService();
        $this->carRepo = new CarRepository();
    }

    public function reserve($carId, $userId) {
        try {
            $this->purchaseService->reserveCar($carId, $userId);
            return ['success' => true, 'message' => 'Car reserved successfully for 15 minutes.'];
        } catch (Exception $e) {
            return ['success' => false, 'message' => $e->getMessage()];
        }
    }

    public function checkout($carId, $userId, $buyerData) {
        try {
            $transactionId = $this->purchaseService->initiatePurchase($carId, $userId, $buyerData);
            return ['success' => true, 'transaction_id' => $transactionId];
        } catch (Exception $e) {
            return ['success' => false, 'message' => $e->getMessage()];
        }
    }
}