/*
 * Copyright (C) 2016-2025 Matthias Klumpp <matthias@tenstral.net>
 *
 * Licensed under the GNU Lesser General Public License Version 3
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the license, or
 * (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this software.  If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once

#include <memory>
#include <appstream-compose.h>
#include <glib.h>

#include "config.h"
#include "logging.h"
#include "datastore.h"
#include "iconhandler.h"
#include "result.h"
#include "backends/interfaces.h"
#include "cptmodifiers.h"
#include "dataunits.h"

namespace ASGenerator
{

/**
 * Class for extracting AppStream metadata from packages.
 */
class DataExtractor
{
public:
    DataExtractor(
        std::shared_ptr<DataStore> db,
        std::shared_ptr<IconHandler> iconHandler,
        AsgLocaleUnit *localeUnit,
        AscImageFormat imageFormat,
        std::shared_ptr<InjectedModifications> modInjInfo = nullptr,
        const std::string &prefix = {});
    ~DataExtractor();

    /**
     * Process a package and extract AppStream metadata from it.
     *
     * @param pkg The package to process
     * @return GeneratorResult containing the extraction results
     */
    GeneratorResult processPackage(std::shared_ptr<Package> pkg);

    // Delete copy constructor and assignment operator
    DataExtractor(const DataExtractor &) = delete;
    DataExtractor &operator=(const DataExtractor &) = delete;

private:
    Config *m_conf;
    quill::Logger *m_log;
    AscCompose *m_compose;
    AscMedia *m_media;
    DataType m_dtype;

    // the package we are processing, so the duplicate-component check - which only gets
    // handed this instance - can weigh us against the package owning a component
    std::string m_currentPkid;

    std::shared_ptr<DataStore> m_dstore;
    std::shared_ptr<IconHandler> m_iconh;
    std::shared_ptr<InjectedModifications> m_modInj;
    AsgLocaleUnit *m_l10nUnit;

    /**
     * Helper function for early asgen-specific metadata manipulation.
     * This is a C callback function used by AppStream Compose.
     *
     * It drops components that another package already provides identical data for, so we
     * do not spend time rendering media that we would throw away when committing the result.
     * The same is done for components that another architecture of this very package has
     * produced already, only that we keep referencing those.
     * Components of another version of ourselves are deliberately kept and processed again,
     * so their media is refreshed rather than reused from an earlier run.
     */
    static void checkMetadataIntermediate(AscResult *cres, AscUnit *cunit, void *userData);

    /**
     * Helper function for translating desktop file text.
     * This is a C callback function for desktop entry translation.
     */
    static GPtrArray *translateDesktopTextCallback(GKeyFile *dePtr, const char *text, void *userData);
};

} // namespace ASGenerator
