<?php

/**
 * Unit-level tests for the decision_ids filter logic used in AutoMailDispatcher.
 * These tests execute the filter predicate in isolation without database interaction.
 */

/**
 * Simulates the filter closure from AutoMailDispatcher::maybeQueue().
 *
 * @param  array<string,mixed>  $preferences
 */
function emailPassesDecisionFilter(array $preferences, int $decisionId): bool
{
    $decisionIds = $preferences['decision_ids'] ?? [];

    if (empty($decisionIds)) {
        return true;
    }

    return in_array($decisionId, array_map('intval', $decisionIds), true);
}

it('email with no decision_ids restriction passes the filter for any decision', function () {
    expect(emailPassesDecisionFilter([], 5))->toBeTrue();
});

it('email with a matching decision_id in preferences passes the filter', function () {
    expect(emailPassesDecisionFilter(['decision_ids' => [3, 7]], 7))->toBeTrue();
});

it('email with a non-matching decision_id in preferences is filtered out', function () {
    expect(emailPassesDecisionFilter(['decision_ids' => [3, 7]], 99))->toBeFalse();
});

it('email with empty preferences is treated as no restriction', function () {
    expect(emailPassesDecisionFilter([], 42))->toBeTrue();
});

it('string decision ids in preferences are cast to int for comparison', function () {
    expect(emailPassesDecisionFilter(['decision_ids' => ['3', '7']], 7))->toBeTrue();
    expect(emailPassesDecisionFilter(['decision_ids' => ['3', '7']], 99))->toBeFalse();
});
