#! /usr/bin/env php
<?php

namespace Gazelle;

use sad_spirit\pg_wrapper\converters\containers\ArrayConverter;
use sad_spirit\pg_wrapper\converters\IntegerConverter;
use sad_spirit\pg_wrapper\converters\StringConverter;

error_reporting(E_ALL);

require __DIR__ . '/../lib/bootstrap.php';
require __DIR__ . '/../tests/helper.php';

$remove = [];
foreach (['pcntl_fork', 'pcntl_signal', 'pcntl_signal_dispatch', 'pcntl_waitpid'] as $function) {
    if (!function_exists($function)) {
        $remove[] = $function;
    }
}
if ($remove) {
    echo "The following functions must be removed from the `disable_functions` directive in cli/php.ini:\n";
    echo "=>  ", implode(', ', $remove), "\n";
    exit;
}

define('BORIS', 1);
$_SERVER['HTTP_USER_AGENT'] = 'boris';
$_SERVER['REMOTE_ADDR']     = '127.0.0.1';
Base::setRequestContext(new RequestContext(
    'boris', $_SERVER['REMOTE_ADDR'], $_SERVER['HTTP_USER_AGENT']
));

$torMan  = new Manager\Torrent();
$userMan = new Manager\User();
$db      = DB::DB();

$sysop = $userMan->findById(
    $db->scalar('
        SELECT um.id
        FROM users_main um
        INNER JOIN permissions p on (p.ID = um.PermissionID)
        ORDER BY p.level DESC
        LIMIT 1
    ')
);
if (!$sysop instanceof User) {
    echo "**** NOTE! no viewer context is set\n";
} else {
    $sysop->requestContext()->setViewer($sysop);
    \Text::setViewer($sysop);
    Util\Twig::setViewer($sysop);
    echo "** viewer context is @{$sysop->username()}\n";
}

$preload = [
    'cache'       => $Cache,
    'db'          => $db,
    'debug'       => $Debug,
    'Twig'        => $Twig,
    'artMan'      => new Manager\Artist(),
    'bonus'       => new Manager\Bonus(),
    'collMan'     => new Manager\Collage(),
    'forMan'      => new Manager\Forum(),
    'ipMan'       => new Manager\IPv4(),
    'notifMan'    => new Manager\Notification(),
    'pg'          => new DB\Pg(PG_RO_DSN),
    'postMan'     => new Manager\ForumPost(),
    'privMan'     => new Manager\Privilege(),
    'repMan'      => new Manager\Report($userMan),
    'reqMan'      => new Manager\Request(),
    'tagMan'      => new Manager\Tag(),
    'tgMan'       => new Manager\TGroup(),
    'threadMan'   => new Manager\ForumThread(),
    'torMan'      => $torMan,
    'trepMan'     => new Manager\Torrent\Report($torMan),
    'trepTypeMan' => new Manager\Torrent\ReportType(),
    'userMan'     => $userMan,
];

printf("** preloaded objects:\n** %s\n", implode(', ', array_keys($preload)));

$b = new \Boris\Boris(SITE_NAME . '> ');
$b->setInspector(new \Boris\ExportInspector());
$b->setLocal($preload);
$b->start();

/*
 * Sample usage:
 *
    ./boris
    [1] ops> $db->query('select count(*) from torrents');
    // object(mysqli_result)(
    //   'current_field' => NULL,
    //   'field_count' => NULL,
    //   'lengths' => NULL,
    //   'num_rows' => NULL,
    //   'type' => NULL
    // )
    [2] ops> $db->to_array(false, MYSQLI_ASSOC);
    // array(
    //   0 => array(
    //     'count(*)' => '14'
    //   )
    // )
*/
