<?php

it('requires id and at least one mutable field for PUT /collected-invoices in user route', function (): void {
    $routeFile = app_path('routes/userInvoicesRoute.php');
    $content = file_get_contents($routeFile);

    expect($content)->not->toBeFalse();
    expect($content)->toContain("\$this->put('/collected-invoices'");
    expect($content)->toContain("self::requireParameters(['id']);");
    expect($content)->toContain("\$is_superuser = self::hasPermission('superuser');");
    expect($content)->toContain("if (!self::isParametersSet(['po_number']) && !self::isParametersSet(['closed_at'])) {");
    expect($content)->toContain("\$response->error('Missing required parameters: po_number, closed_at', 400);");
    expect($content)->toContain("if (self::isParametersSet(['closed_at']) && !\$is_superuser) {");
    expect($content)->toContain("\$response->error('Forbidden: only superusers can update closed_at', 403);");
    expect($content)->toContain("if ((int)\$invoice->customer_number->value() !== (int)\$user->customer_number->value() && !\$is_superuser) {");
});

it('supports independent po_number and closed_at updates for PUT /collected-invoices in user route', function (): void {
    $routeFile = app_path('routes/userInvoicesRoute.php');
    $content = file_get_contents($routeFile);

    expect($content)->not->toBeFalse();
    expect($content)->toContain("if (self::isParametersSet(['po_number'])) {");
    expect($content)->toContain("\$invoice->po_number->set((string)self::getParameter('po_number'));");

    expect($content)->toContain("if (self::isParametersSet(['closed_at'])) {");
    expect($content)->toContain("if (\$closed_at !== null && \$closed_at !== '') {");
    expect($content)->toContain("self::requireDateFormat((string)\$closed_at, self::FORMAT_DATE());");
    expect($content)->toContain("\$invoice->closed_at->set(\$closed_at === null || \$closed_at === '' ? null : date('Y-m-d 23:59:59', strtotime((string)\$closed_at . ' 00:00:01')));");
});
