<?php

declare(strict_types=1);

usesApiSuite();

it('sets preserves and clears product fixed prices through the user discounts endpoint', function (): void {
    api_test_covers('POST /superuser/user/discounts', 'pricing');
    api_test_covers('GET /superuser/user/discounts', 'pricing');

    $customer = api_fixtures()->createUser(['display_name' => 'Endpoint Fixed Price Customer']);
    $product = api_fixtures()->createProduct([
        'name' => 'Endpoint Fixed Price Product',
        'price' => 900,
    ]);
    $session = api_fixtures()->createUserSession([
        'set_custom_price',
        'get_custom_prices_other',
    ]);

    $findProductRow = function () use ($customer, $product, $session): array {
        $response = api_client()->get(
            '/superuser/user/discounts?user_id=' . $customer['id'],
            $session['headers']
        );
        $response
            ->assertStatus(200)
            ->assertEnvelope()
            ->assertSuccess();

        foreach ($response->data() as $row) {
            if ((int)($row['product_or_category_id'] ?? 0) === (int)$product['id'] && !($row['is_category'] ?? false)) {
                return $row;
            }
        }

        throw new RuntimeException('Expected product override row was not returned.');
    };

    api_client()
        ->post('/superuser/user/discounts', [
            'user_id' => $customer['id'],
            'object_id' => $product['id'],
            'is_category' => false,
            'discount' => 20,
            'fixed_price' => 350,
        ], $session['headers'])
        ->assertStatus(200)
        ->assertEnvelope()
        ->assertSuccess();

    $row = $findProductRow();
    expect((int)$row['percentage'])->toBe(20);
    expect((int)$row['fixed_price'])->toBe(350);

    api_client()
        ->post('/superuser/user/discounts', [
            'user_id' => $customer['id'],
            'object_id' => $product['id'],
            'is_category' => false,
            'discount' => 10,
        ], $session['headers'])
        ->assertStatus(200)
        ->assertEnvelope()
        ->assertSuccess();

    $row = $findProductRow();
    expect((int)$row['percentage'])->toBe(10);
    expect((int)$row['fixed_price'])->toBe(350);

    api_client()
        ->post('/superuser/user/discounts', [
            'user_id' => $customer['id'],
            'object_id' => $product['id'],
            'is_category' => false,
            'discount' => 10,
            'fixed_price' => null,
        ], $session['headers'])
        ->assertStatus(200)
        ->assertEnvelope()
        ->assertSuccess();

    $row = $findProductRow();
    expect((int)$row['percentage'])->toBe(10);
    expect($row['fixed_price'])->toBeNull();
});

it('rejects invalid fixed price payloads for user discounts', function (): void {
    api_test_covers('POST /superuser/user/discounts', 'validation');

    $customer = api_fixtures()->createUser(['display_name' => 'Invalid Fixed Price Customer']);
    $product = api_fixtures()->createProduct(['name' => 'Invalid Fixed Price Product']);
    $category = api_fixtures()->createCategory(['name' => 'Invalid Fixed Price Category']);
    $session = api_fixtures()->createUserSession(['set_custom_price']);

    api_client()
        ->post('/superuser/user/discounts', [
            'user_id' => $customer['id'],
            'object_id' => $product['id'],
            'is_category' => false,
            'discount' => 10,
            'fixed_price' => '12.5',
        ], $session['headers'])
        ->assertStatus(400)
        ->assertEnvelope()
        ->assertSuccess(false);

    api_client()
        ->post('/superuser/user/discounts', [
            'user_id' => $customer['id'],
            'object_id' => (string)$category['id'],
            'is_category' => true,
            'discount' => 10,
            'fixed_price' => 350,
        ], $session['headers'])
        ->assertStatus(400)
        ->assertEnvelope()
        ->assertSuccess(false);
});
