<?php

declare(strict_types=1);

final class MyronSiteSnapshotImportAuditRepository
{
    public function __construct(private readonly PDO $pdo)
    {
    }

    /**
     * @param array<string, int> $entityCounts
     */
    public function append(string $action, string $outcome, string $message, array $entityCounts = []): void
    {
        $statement = $this->pdo->prepare(
            'INSERT INTO site_snapshot_import_audit (
                id, action, outcome, message, entity_counts_json, created_at
            ) VALUES (
                :id, :action, :outcome, :message, :entity_counts_json, :created_at
            )'
        );
        $statement->execute([
            ':id' => myronGenerateId('ssi'),
            ':action' => substr(trim($action), 0, 80),
            ':outcome' => substr(trim($outcome), 0, 40),
            ':message' => $message,
            ':entity_counts_json' => json_encode($entityCounts, JSON_UNESCAPED_SLASHES),
            ':created_at' => myronNow(),
        ]);
    }
}

