<?php

declare(strict_types=1);

namespace App\Framework\SlidingWindow\Aggregator;

use App\Framework\SlidingWindow\WindowEntry;

/**
 * Aggregator for boolean values (success/failure) - useful for CircuitBreaker
 *
 * @implements SlidingWindowAggregator<BooleanResult>
 */
final readonly class BooleanAggregator implements SlidingWindowAggregator
{
    public function aggregate(WindowEntry ...$entries): BooleanResult
    {
        $successes = 0;
        $failures = 0;

        foreach ($entries as $entry) {
            if ($entry->value === true) {
                $successes++;
            } elseif ($entry->value === false) {
                $failures++;
            }
        }

        $total = $successes + $failures;
        $failureRate = $total > 0 ? $failures / $total : 0.0;
        $successRate = $total > 0 ? $successes / $total : 0.0;

        return new BooleanResult(
            total: $total,
            successes: $successes,
            failures: $failures,
            successRate: $successRate,
            failureRate: $failureRate
        );
    }

    public function getIdentifier(): string
    {
        return 'boolean';
    }
}
