/*******************************************************************************
 * Copyright (c) 2026, Jan Koester jan.koester@gmx.net
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 * Neither the name of the <organization> nor the
 *      names of its contributors may be used to endorse or promote products
 *      derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *******************************************************************************/

#pragma once

#include <QObject>
#include <QString>

class DocumentManager;
class PublishClient;

/*
 * LocalApi — exposed to QML as context property "localApi".
 */
class LocalApi : public QObject {
    Q_OBJECT
public:
    explicit LocalApi(DocumentManager *doc, PublishClient *pub, QObject *parent = nullptr);

    /* Plugin types */
    Q_INVOKABLE QString getPluginTypes();

    /* Document tree */
    Q_INVOKABLE QString getDocTree();

    /* Element manipulation */
    Q_INVOKABLE QString addElement(const QString &parentUuid, const QString &pluginName, int position);
    Q_INVOKABLE QString removeElement(const QString &uuid);
    Q_INVOKABLE QString moveUp(const QString &uuid);
    Q_INVOKABLE QString moveDown(const QString &uuid);
    Q_INVOKABLE QString indent(const QString &uuid);
    Q_INVOKABLE QString outdent(const QString &uuid);
    Q_INVOKABLE QString copyElement(const QString &uuid);
    Q_INVOKABLE QString pasteElement(const QString &targetUuid);

    /* Properties */
    Q_INVOKABLE QString getElementProperties(const QString &uuid);
    Q_INVOKABLE QString setElementProperties(const QString &uuid, const QString &propsJson);

    /* Export/Import */
    Q_INVOKABLE QString exportXml();
    Q_INVOKABLE bool importXml(const QString &xml);
    Q_INVOKABLE QString renderPreview();
    Q_INVOKABLE void renderPreviewOnServer(bool mobile, const QString &target, const QString &paramsJson);

    /* Media browse (requires connection) */
    Q_INVOKABLE void fetchAlbums();
    Q_INVOKABLE void fetchMedia(const QString &albumId);

    /* Theme colors (requires connection) */
    Q_INVOKABLE void fetchThemeColors();

    /* Survey list (requires connection) */
    Q_INVOKABLE void fetchSurveyList();

    /* Publish (requires connection) */
    Q_INVOKABLE void fetchPublishTargets();
    Q_INVOKABLE void publishToTarget(const QString &target, const QString &paramsJson);
    Q_INVOKABLE void publishToTargetOverwrite(const QString &target, const QString &paramsJson, const QString &existingId);
    Q_INVOKABLE QString collectRepeaterParams(QObject *repeater);
    Q_INVOKABLE QString jsonSet(const QString &json, const QString &key, const QString &value);

    /* File operations (called from QML menus) */
    Q_INVOKABLE void newDocument();
    Q_INVOKABLE void openDocument();
    Q_INVOKABLE void importHtmlFile();
    Q_INVOKABLE void exportHtmlFile();
    Q_INVOKABLE QString generateAiPrompt(const QString &userRequest);
    Q_INVOKABLE void saveDocument();
    Q_INVOKABLE void saveAsDocument();
    Q_INVOKABLE void publishToServer();

    /* Connection management */
    Q_INVOKABLE void connectToServer(const QString &url, const QString &user, const QString &password);
    Q_INVOKABLE void disconnectServer();
    Q_INVOKABLE void abortConnection();
    Q_INVOKABLE void setIgnoreSslErrors(bool ignore);
    Q_INVOKABLE QString serverUrl() const;

    /* Saved connections CRUD */
    Q_INVOKABLE QString getSavedConnections();
    Q_INVOKABLE void    saveConnection(const QString &name, const QString &url, const QString &user, const QString &password, bool ignoreSsl);
    Q_INVOKABLE void    updateConnection(const QString &oldName, const QString &name, const QString &url, const QString &user, const QString &password, bool ignoreSsl);
    Q_INVOKABLE void    deleteConnection(const QString &name);

    /* Initial load */
    Q_INVOKABLE void refresh();

signals:
    void documentChanged();
    void statusMessage(const QString &msg);
    void currentFileChanged(const QString &path);
    void connectionStatusChanged(bool ok, const QString &msg);
    void serverPreviewReady(const QString &html);
    void albumsLoaded(const QString &json);
    void mediaLoaded(const QString &json);
    void publishTargetsLoaded(const QString &json);
    void publishCompleted(bool ok, const QString &msg);
    void themeColorsLoaded(const QString &json);
    void surveyListLoaded(const QString &json);
    void savedConnectionsChanged();

private:
    DocumentManager *_doc;
    PublishClient   *_pub;
    QString          _currentFile;
};
