<?PHP
/**
 * Contains a class for keeping the Manticore times fulltext index in sync.
 *
 * @author Joshua Ramon Enslin <joshua@museum-digital.de>
 */
declare(strict_types = 1);

/**
 * Class for keeping the times fulltext index in sync.
 */
final class NodaTimeFulltextSyncManticore {

    const FULL_SYNC_COMMIT_AFTER = 30000;

    /**
     * Returns all names and descriptions in the different languages of a time.
     *
     * @param MDMysqli $mysqli_noda  DB connection.
     * @param string   $databasename Name of the DB connection.
     * @param integer  $id           ID of the entry to load.
     *
     * @return array<array{language: string, name: string, timestamp: string}>
     */
    public static function getAllNamesById(MDMysqli $mysqli_noda, string $databasename, int $id):array {

        $output = [];

        $result = $mysqli_noda->query_by_stmt("SELECT `zeit_name`, `zeit_erfasst_am`
            FROM `$databasename`.`zeiten`
            WHERE `zeit_id` = ?", "i", $id);

        if (!($data = $result->fetch_row())) {
            $result->close();
            return [];
        }

        $result->close();

        $output[] = [
            'language' => '',
            'name' => $data[0],
            'timestamp' => $data[1],
        ];

        $result = $mysqli_noda->query_by_stmt("SELECT `trans_language`, `trans_name`, `trans_last`
            FROM `$databasename`.`zeit_translation`
            WHERE `zeit_id` = ?", "i", $id);

        while ($cur = $result->fetch_row()) {

            $output[] = [
                'language' => $cur[0],
                'name' => $cur[1],
                'timestamp' => $cur[2],
            ];

        }

        $result->close();

        return $output;

    }

    /**
     * Removes all names of an entry from the Manticore index.
     *
     * @param MDMysqli $mysqli_manticore Connection to Manticore DB.
     * @param string   $databasename     Name of the main noda database.
     * @param integer  $id               ID of the entry to load.
     *
     * @return void
     */
    public static function removeById(MDMysqli $mysqli_manticore, string $databasename, int $id):void {

        $mysqli_manticore->do_update_query_large("DELETE FROM `" . $databasename . "_zeiten`
            WHERE `entry_id` = " . $id);

    }

    /**
     * Inserts the names of an entry.
     *
     * @param MDMysqli $mysqli_noda      DB connection.
     * @param MDMysqli $mysqli_manticore Connection to Manticore DB.
     * @param string   $databasename     Name of the DB connection.
     * @param integer  $id               ID of the entry to load.
     *
     * @return void
     */
    public static function insertById(MDMysqli $mysqli_noda, MDMysqli $mysqli_manticore, string $databasename, int $id):void {

        $names = self::getAllNamesById($mysqli_noda, $databasename, $id);

        $mysqli_manticore->autocommit(false);

        foreach ($names as $cur) {

            $mysqli_manticore->query("INSERT INTO `" . $databasename . "_zeiten`
                (`entry_id`, `language`, `name`, `timestamp`)
                VALUES
                (" . $id . ",
                    '" . $mysqli_manticore->escape_string($cur['language']) . "',
                    '" . $mysqli_manticore->escape_string($cur['name']) . "',
                    '" . strtotime($mysqli_manticore->escape_string($cur['timestamp'])) . "')");

            if ($mysqli_manticore->error) {
                throw new Exception($mysqli_manticore->error);
            }

        }

        $mysqli_manticore->commit();
        $mysqli_manticore->autocommit(true);

    }

    /**
     * Updates the names of an entry.
     *
     * @param MDMysqli $mysqli_noda      DB connection.
     * @param MDMysqli $mysqli_manticore Connection to Manticore DB.
     * @param string   $databasename     Name of the DB connection.
     * @param integer  $id               ID of the entry to load.
     *
     * @return void
     */
    public static function updateById(MDMysqli $mysqli_noda, MDMysqli $mysqli_manticore, string $databasename, int $id):void {

        self::removeById($mysqli_manticore, $databasename, $id);
        self::insertById($mysqli_noda, $mysqli_manticore, $databasename, $id);

    }

    /**
     * Runs a full sync.
     *
     * @param MDMysqli $mysqli_noda      Connection to MySQL DB.
     * @param MDMysqli $mysqli_manticore Connection to Manticore DB.
     * @param string   $databasename     Name of the main noda database.
     *
     * @return void
     */
    public static function runFullSync(MDMysqli $mysqli_noda, MDMysqli $mysqli_manticore, string $databasename):void {

        $mysqli_manticore->do_update_query_large("TRUNCATE RTINDEX `" . $databasename . "_zeiten`");

        // Sync tags

        $i = 0;

        $result = $mysqli_noda->do_read_query("SELECT `zeit_id`, `zeit_name`,
                `zeit_erfasst_am`
            FROM `" . $databasename . "`.`zeiten`");

        $mysqli_manticore->autocommit(false);

        while ($cur = $result->fetch_assoc()) {

            $mysqli_manticore->query("INSERT INTO `" . $databasename . "_zeiten`
                (`entry_id`, `language`, `name`, `timestamp`)
                VALUES
                (" . $cur['zeit_id'] . ",
                    '',
                    '" . $mysqli_manticore->escape_string($cur['zeit_name']) . "',
                    '" . strtotime($mysqli_manticore->escape_string($cur['zeit_erfasst_am'])) . "')");

            if ($mysqli_manticore->error) {
                throw new Exception($mysqli_manticore->error);
            }

            MDConsole::write("Wrote {$cur['zeit_id']}");
            ++$i;
            if ($i === self::FULL_SYNC_COMMIT_AFTER) {
                $mysqli_manticore->commit();
                $i = 0;
            }

        }

        $result->close();

        $mysqli_manticore->commit();

        // Sync translations

        $result = $mysqli_noda->do_read_query("SELECT `zeit_id`, `trans_language`,
            `trans_name`, `trans_last`
                FROM `" . $databasename . "`.`zeit_translation`");

        $i = 0;

        while ($cur = $result->fetch_assoc()) {

            $mysqli_manticore->query("INSERT INTO `" . $databasename . "_zeiten`
                (`entry_id`, `language`, `name`, `timestamp`)
                VALUES
                (" . $cur['zeit_id'] . ",
                    '" . $mysqli_manticore->escape_string($cur['trans_language']) . "',
                    '" . $mysqli_manticore->escape_string($cur['trans_name']) . "',
                    '" . strtotime($mysqli_manticore->escape_string($cur['trans_last'])) . "')");

            if ($mysqli_manticore->error) {
                throw new Exception($mysqli_manticore->error);
            }

            MDConsole::write("Wrote {$cur['zeit_id']}");
            ++$i;
            if ($i === self::FULL_SYNC_COMMIT_AFTER) {
                $mysqli_manticore->commit();
                $i = 0;
            }

        }

        $mysqli_manticore->commit();
        $mysqli_manticore->autocommit(true);

    }
}
