<?php
require_once __DIR__ . '/../vendor/autoload.php';

if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) {
    return;
}

use Blazemedia\Blackhole\Tools\DotEnv;
use Blazemedia\Blackhole\Tools\SimpleDB;
use Blazemedia\Blackhole\OfferListProvider;

class Offer_Scan_Command {

    public function __invoke( $args, $assoc_args ) {

        // Carica il file .env
        (new DotEnv( __DIR__ . '/../../../../../.env' ))->load();
   
        $prefix   = getenv('DB_PREFIX') ?? "_wp";
        // Inizializza il database
        $db = SimpleDB::getInstance($prefix);
     
        WP_CLI::log( "Scanning website offer posts..." );
        ( new OfferListProvider( $db, 2, 10 ) )->scan();

        WP_CLI::success( "Scan completed!" );
    }
}

// Registra il comando WP-CLI
WP_CLI::add_command( 'offers-scan', 'Offer_Scan_Command' );