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

/**
 * @author Nicolas CARPi <nico-git@deltablot.email>
 * @copyright 2012 Nicolas CARPi
 * @see https://www.elabftw.net Official website
 * @license AGPL-3.0
 * @package elabftw
 */

declare(strict_types=1);

namespace Elabftw\Commands;

use Defuse\Crypto\Exception\WrongKeyOrModifiedCiphertextException;
use Elabftw\Elabftw\FsTools;
use Elabftw\Models\Config;
use Elabftw\Services\Email;
use Elabftw\Storage\Exports;
use Monolog\Handler\ErrorLogHandler;
use Monolog\Logger;
use Symfony\Component\Console\Application;
use Symfony\Component\Mailer\Exception\InvalidArgumentException;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mime\Exception\RfcComplianceException;
use Symfony\Component\Console\Logger\ConsoleLogger;
use Symfony\Component\Console\Output\ConsoleOutput;

use function dirname;

require dirname(__DIR__) . '/vendor/autoload.php';

$Application = new Application();
$Config = Config::getConfig();
$errorLogLogger = new Logger('elabftw');
$errorLogLogger->pushHandler(new ErrorLogHandler());
$logger = new ConsoleLogger(new ConsoleOutput());
try {
    $Email = new Email(
        new Mailer(Transport::fromDsn($Config->getDsn())),
        $errorLogLogger,
        $Config->configArr['mail_from'],
    );
} catch (WrongKeyOrModifiedCiphertextException $e) {
    $logger->critical("It seems the SMTP password does not correspond to the current SECRET_KEY, and that is something that needs to be fixed. Try setting the 'smtp_password' to an empty string in the 'config' table: update config set conf_value = '' where conf_name = 'smtp_password';\nError message:\n" . $e->getMessage() . "\n");
    exit;
} catch (RfcComplianceException $e) {
    $logger->critical("It seems the configured sending email address is wrong. Try setting the 'mail_from' value to a valid email address: update config set conf_value = 'example@example.com' where conf_name = 'mail_from';\nError message:\n" . $e->getMessage() . "\n");
    exit;
} catch (InvalidArgumentException $e) {
    $logger->critical("It seems your SMTP configuration is wrong. Try setting the 'smtp_address' value to a valid hostname: update config set conf_value = 'example.com' where conf_name = 'smtp_address';\nError message:\n" . $e->getMessage() . "\n");
    exit;
}

$exportsFs = new Exports();
$sqlFs = FsTools::getFs(dirname(__DIR__) . '/src/sql');

$Application->add(new InstallRedir());
$Application->add(new GenI18n4Js());
$Application->add(new CacheClear());
$Application->add(new CheckDatabase((int) $Config->configArr['schema']));
$Application->add(new CheckTsBalance((int) $Config->configArr['ts_balance'], $Email));
$Application->add(new CleanDatabase());
$Application->add(new ExportCommand($exportsFs));
$Application->add(new ExportEln($exportsFs));
$Application->add(new ImportEln($exportsFs));
$Application->add(new ImportCompoundsCsv($exportsFs));
$Application->add(new UpdateDatabase());
$Application->add(new RevertSchema($sqlFs));
$Application->add(new UpdateTo3());
$Application->add(new UpdateTo34());
$Application->add(new SnapFingers());
$Application->add(new ForceSchema());
$Application->add(new GenCache());
$Application->add(new GenSchema($sqlFs));
$Application->add(new PruneRevisions());
$Application->add(new MfaCode());
$Application->add(new RefreshIdps($Config->configArr['proxy']));
$Application->add(new SendNotifications($Email));
$Application->add(new SendExpirationNotifications($Email));
$Application->add(new TagsTeamsSync());
$Application->add(new PruneUploads());
$Application->add(new PruneExperiments());
$Application->add(new PruneItems());
$Application->add(new CheckUploads());
$Application->add(new MigrateUploads());
$Application->add(new ExperimentsTimestamp());

$Application->run();
