<?php

declare(strict_types=1);

usesApiSuite();

it('detaches an order booking and clears the matching order link', function (): void {
    $customer = api_fixtures()->createUser(['display_name' => 'Detached Booking Customer']);
    $department = api_fixtures()->createDepartment();
    $cashier = api_fixtures()->createUser(['display_name' => 'Detached Booking Cashier']);
    $order = api_fixtures()->createOrder([
        'customer_id' => $customer['customer_number'],
        'cashier_id' => $cashier['id'],
        'department_id' => $department['id'],
    ]);
    $booking = api_fixtures()->createOrderBooking([
        'customer_number' => $customer['customer_number'],
        'department' => $department['id'],
        'order_id' => $order['id'],
    ]);

    api_test_runtime()->db()->query(
        'UPDATE orders SET booking_id = ' . (int)$booking['id'] . ' WHERE id = ' . (int)$order['id']
    );

    $session = api_fixtures()->createUserSession([
        'edit_bookings',
        'department_access_' . $department['id'],
    ]);

    $response = api_client()->put('/order-bookings', [
        'id' => $booking['id'],
        'order_id' => null,
    ], $session['headers']);

    $response
        ->assertStatus(200)
        ->assertEnvelope()
        ->assertSuccess();

    expect($response->data())
        ->toHaveKey('order_id')
        ->and($response->data()['order_id'])
        ->toBeNull();

    $result = api_test_runtime()->db()->query(
        'SELECT ob.order_id AS booking_order_id, o.booking_id AS order_booking_id ' .
        'FROM order_bookings ob JOIN orders o ON o.id = ' . (int)$order['id'] . ' ' .
        'WHERE ob.id = ' . (int)$booking['id'] . ' LIMIT 1'
    );

    expect($result)->not->toBeFalse();
    $row = $result->fetch_assoc();
    expect($row)->toBeArray();
    expect($row['booking_order_id'] ?? null)->toBeNull();
    expect($row['order_booking_id'] ?? null)->toBeNull();
});

it('keeps the linked order when order_id is omitted from an order booking update', function (): void {
    $customer = api_fixtures()->createUser(['display_name' => 'Still Linked Booking Customer']);
    $department = api_fixtures()->createDepartment();
    $cashier = api_fixtures()->createUser(['display_name' => 'Still Linked Booking Cashier']);
    $order = api_fixtures()->createOrder([
        'customer_id' => $customer['customer_number'],
        'cashier_id' => $cashier['id'],
        'department_id' => $department['id'],
    ]);
    $booking = api_fixtures()->createOrderBooking([
        'customer_number' => $customer['customer_number'],
        'department' => $department['id'],
        'order_id' => $order['id'],
        'reference' => 'ORIGINAL-BOOKING-REF',
    ]);

    api_test_runtime()->db()->query(
        'UPDATE orders SET booking_id = ' . (int)$booking['id'] . ' WHERE id = ' . (int)$order['id']
    );

    $session = api_fixtures()->createUserSession([
        'edit_bookings',
        'department_access_' . $department['id'],
    ]);

    $response = api_client()->put('/order-bookings', [
        'id' => $booking['id'],
        'reference' => 'UPDATED-BOOKING-REF',
    ], $session['headers']);

    $response
        ->assertStatus(200)
        ->assertEnvelope()
        ->assertSuccess();

    expect((int)($response->data()['order_id'] ?? 0))->toBe($order['id']);
    expect($response->data()['reference'] ?? null)->toBe('UPDATED-BOOKING-REF');

    $result = api_test_runtime()->db()->query(
        'SELECT ob.order_id AS booking_order_id, o.booking_id AS order_booking_id ' .
        'FROM order_bookings ob JOIN orders o ON o.id = ' . (int)$order['id'] . ' ' .
        'WHERE ob.id = ' . (int)$booking['id'] . ' LIMIT 1'
    );

    expect($result)->not->toBeFalse();
    $row = $result->fetch_assoc();
    expect($row)->toBeArray();
    expect((int)($row['booking_order_id'] ?? 0))->toBe($order['id']);
    expect((int)($row['order_booking_id'] ?? 0))->toBe($booking['id']);
});
