<?php

namespace Blazemedia\Blackhole\Tools;

class RunLogger {

    const OPTION_KEY = 'blackhole_run_logs';
    const MAX_RUNS = 10;

    public static function add( string $command, int $found = 0, int $blackholed = 0, string $note = '' ) : void {
        $runs = get_option( self::OPTION_KEY, array() );
        if ( !is_array( $runs ) ) {
            $runs = array();
        }

        $entry = array(
            'datetime' => current_time( 'mysql' ),
            'command' => sanitize_text_field( $command ),
            'found' => max( 0, (int) $found ),
            'blackholed' => max( 0, (int) $blackholed ),
            'note' => sanitize_text_field( $note ),
        );

        array_unshift( $runs, $entry );
        $runs = array_slice( $runs, 0, self::MAX_RUNS );

        update_option( self::OPTION_KEY, $runs, false );
    }

    public static function all() : array {
        $runs = get_option( self::OPTION_KEY, array() );
        return is_array( $runs ) ? $runs : array();
    }
}
