#!/usr/bin/php
<?php

/*
 * The MIT License
 *
 * Copyright 2024 Austrian Centre for Digital Humanities.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

$composerDir = realpath(__DIR__);
while ($composerDir !== false && !file_exists("$composerDir/vendor")) {
    $composerDir = realpath("$composerDir/..");
}
require_once "$composerDir/vendor/autoload.php";

use acdhOeaw\arche\lib\schema\Ontology;
use acdhOeaw\arche\lib\Repo;
use zozlak\logging\Log;
use zozlak\logging\LogMultiplexer;
use Psr\Log\LogLevel;
use quickRdf\Dataset;
use quickRdf\DataFactory;
use zozlak\argparse\ArgumentParser;
use quickRdfIo\Util as RdfIoUtil;
use acdhOeaw\UriNormalizerRetryConfig;
use acdhOeaw\arche\metadataCrawler\MetadataChecker;
use acdhOeaw\arche\metadataCrawler\MetadataCrawlerException;

$parser = new ArgumentParser();
$parser->addArgument('--repositoryUrl', default: 'https://arche.acdh.oeaw.ac.at/api/');
$parser->addArgument('--retryAttempts', default: 3);
$parser->addArgument('--retryDelay', default: 2);
$parser->addArgument('--logFile', default: 'php://stdout');
$parser->addArgument('--errorLog', help: 'Path to a CSV log storing metadata check errors');
$parser->addArgument('--verbose', action: ArgumentParser::ACTION_STORE_TRUE);
$parser->addArgument('--tmpDir', default: '/tmp');
$parser->addArgument('metadataFile');
$args   = $parser->parseArgs();

$log = new LogMultiplexer();
$log->addLog(new Log($args->logFile, $args->verbose ? LogLevel::DEBUG : LogLevel::INFO));
if (!empty($args->errorLog)) {
    if (file_exists($args->errorLog)) {
        unlink($args->errorLog);
    }
    $errLog = new Log($args->errorLog, LogLevel::ERROR, "{MESSAGE}");
    $errLog->error('resource;error message');
    $log->addLog($errLog);
}

if (!file_exists($args->metadataFile)) {
    $log->error("metadata file doesn't exist");
    exit(1);
}

$log->info("----------------------------------------");
$log->info("Reading ontology");
$log->info("----------------------------------------");
$repo     = Repo::factoryFromUrl($args->repositoryUrl);
$ontology = Ontology::factoryRest($args->repositoryUrl, $args->tmpDir . '/ontology.cache', 36000);

try {
    $log->info("----------------------------------------");
    $log->info("Reading metadata");
    $log->info("----------------------------------------");
    $meta = new Dataset();
    $meta->add(RdfIoUtil::parse($args->metadataFile, new DataFactory()));

    $log->info("----------------------------------------");
    $log->info("Checking metadata");
    $log->info("----------------------------------------");
    $retryCfg = new UriNormalizerRetryConfig($args->retryAttempts, $args->retryDelay, UriNormalizerRetryConfig::SCALE_POWER);
    $checker  = new MetadataChecker($ontology, $repo->getSchema(), $log, $retryCfg);
    $noErrors = $checker->check($meta, false);

    exit($noErrors ? 0 : 2);
} catch (MetadataCrawlerException $e) {
    $log->error($e->getMessage());
    exit($e->getCode());
}
