<?php

it('adds a cached order-bookings counts endpoint and invalidates it on booking changes', function (): void {
    $routeFile = app_path('routes/orderBookingRoute.php');
    $objectFile = app_path('objects/order_bookings_o.php');

    expect(is_file($routeFile))->toBeTrue();
    expect(is_file($objectFile))->toBeTrue();

    $routeCode = preg_replace('/\s+/', ' ', (string)file_get_contents($routeFile));
    $objectCode = preg_replace('/\s+/', ' ', (string)file_get_contents($objectFile));

    expect($routeCode)->toContain('use classes\order_bookings_counts_cache;');
    expect($routeCode)->toContain("\$this->get('/order-bookings/counts', function () {");
    expect($routeCode)->toContain('$cacheTtl = order_bookings_counts_cache::getTtl();');
    expect($routeCode)->toContain('$cacheKey = $cacheTtl > 0 ? $this->getOrderBookingsCountsCacheKey(');
    expect($routeCode)->toContain('$cachedCounts = order_bookings_counts_cache::getCounts($cacheKey);');
    expect($routeCode)->toContain('order_bookings_counts_cache::storeCounts($cacheKey, $counts, $cacheTtl);');
    expect($routeCode)->toContain('getPendingBookingCounts(');

    expect($objectCode)->toContain('use classes\order_bookings_counts_cache;');
    expect($objectCode)->toContain('order_bookings_counts_cache::clearAll();');
});
