<?php

declare(strict_types=1);

namespace BotGuard\Models;

use BotGuard\Core\Model;

/**
 * Normalized per-module detection output, one row per module per result.
 */
final class DetectionLog extends Model
{
    protected string $table = 'detection_logs';

    /**
     * Aggregate which checks fail most often (analytics).
     *
     * @return array<int, array<string, mixed>>
     */
    public function moduleFailureRates(int $sinceDays = 30): array
    {
        $since = (new \DateTimeImmutable("-{$sinceDays} days"))->format('Y-m-d H:i:s');

        return $this->db->select(
            "SELECT `module`,
                    COUNT(*) AS runs,
                    ROUND(AVG(`raw_score`) * 100, 1) AS avg_human_pct
             FROM `{$this->table}`
             WHERE `created_at` >= ?
             GROUP BY `module` ORDER BY avg_human_pct ASC",
            [$since]
        );
    }
}
