<?php
// prevent direct access
use classes\wordpress_bookings_remote;

if (!defined('WD')) {
    exit;
}

function warn($message): void
{
    echo "\n\033[33m$message\033[0m\n";
}

// Test if we can fetch a booking.
$booking_id = 155;

$wordpress_bookings_remote = new wordpress_bookings_remote();

// Get the last wash ID from the remote API.
$last_wash_id = $wordpress_bookings_remote->get_last_wash_id();

if (empty($last_wash_id)) {
    warn("Failed to fetch the last wash ID");
    return;
}

echo "\nLast wash ID fetched successfully from the remote API\n";
echo "\nLast wash ID: $last_wash_id\n";

// Get all wash IDs from the remote API.
$wash_ids = $wordpress_bookings_remote->get_all_wash_ids()['data']['all_wash_ids'];

if (empty($wash_ids)) {
    warn("Failed to fetch wash IDs");
    return;
}

echo "\nWash IDs fetched successfully from the remote API\n";
echo "\nWash IDs: (count: " . count($wash_ids) . ")\n";

// Parse all bookings from the remote API.
$bookings = $wordpress_bookings_remote->parse_bookings($wordpress_bookings_remote->get_all_bookings());

if (empty($bookings)) {
    warn("Failed to fetch bookings");
    return;
}

// Get all bookings from the remote API.
$bookings = $wordpress_bookings_remote->get_all_bookings();

if (empty($bookings)) {
    warn("Failed to fetch bookings");
    return;
}

echo "\nBookings fetched successfully from the remote API\n";
echo "\nBookings: (count: " . count($bookings['data']['all_wash_bookings']) . ")\n";

$booking = $wordpress_bookings_remote->parse_booking($booking_id);
$bookings_o = new \objects\bookings_o();
if (empty($booking)) {
    warn("Failed to fetch booking with ID $booking_id");
} else {
    // Test if the booking has the expected keys.
    $expected_keys = [
        'id',
        'customer_number',
        'wash_type',
        'contact_email',
        'reference_number',
        'regNrTraekker',
        'regNrTrailer',
        'washCertificateEmail',
        'date',
        'department',
        'pickup_bool',
        'notes',
        'washCertificateStatus',
        'washCertificateUrl',
        'status',
    ];

    foreach ( $expected_keys as $key ) {
        if (!array_key_exists($key, $booking)) {
            warn("Booking with ID $booking_id is missing key $key");
            return;
        }
    }

    echo "\nBooking with ID $booking_id fetched successfully\n";

    // Check if the booking wash certificate exists.
    $wash_certificate_store = new \classes\wash_certificate_store();

    if (!$wash_certificate_store->washCertificateExists($booking['id'])) {
        warn("Booking with ID $booking_id does not have a wash certificate");
    } else {
        echo "\nBooking with ID $booking_id has a wash certificate\n";
        $booking['washCertificateUrl'] = 'Protected URL';
        $booking['washCertificateStatus'] = 'completed';
        // Print the booking data.
        echo "\nBooking data:\n";
        print_r($booking);


        // Save the booking data to database.
        $bookings_o->addOrUpdate(
            $booking['id'],
            $booking['customer_number'],
            $booking['wash_type'],
            $booking['contact_email'],
            $booking['reference_number'],
            $booking['regNrTraekker'],
            $booking['regNrTrailer'],
            $booking['washCertificateEmail'],
            $booking['date'],
            $booking['department'],
            $booking['pickup_bool'] === 'true' ? 1 : 0,
            $booking['notes'],
            $booking['washCertificateStatus'],
            $booking['washCertificateUrl'],
            $booking['status']
        );

        echo "\nBooking data saved to database\n";
    }
}


echo "\nSyncing bookings to the remote API... This may take a while\n";

// Sync all bookings to the remote API.
$start = microtime(true);
$sync = $bookings_o->syncBookings();
$end = microtime(true);

echo "\nBookings synced to the remote API\n";
echo "\nTime taken: " . ($end - $start) . " seconds\n";
echo "\nTime per booking: " . (($end - $start) / count($bookings['data']['all_wash_bookings'])) . " seconds\n";
echo "\nSync: \n";
print_r($sync);