/***************************************************************************
 * SPDX-FileCopyrightText: 2026 S. MANKOWSKI stephane@mankowski.fr
 * SPDX-License-Identifier: GPL-3.0-or-later
 ***************************************************************************/
#ifndef SKGMCONTROLER_H
#define SKGMCONTROLER_H

#include "skgdocumentbank.h"
#include "skgmaccountlistmodel.h"
#include "skgmreferenceproxymodel.h"
#include "skgmtransactionlistmodel.h"

#include <qabstractitemmodel.h>
#include <qobject.h>
#include <qstringlist.h>
#include <qvariant.h>

class SKGObjectModelBase;
class SKGSortFilterProxyModel;

class SKGMControler : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString filePath READ filePath WRITE setFilePath NOTIFY filePathChanged)
    Q_PROPERTY(QString password READ password WRITE setPassword NOTIFY passwordChanged)
    Q_PROPERTY(bool showClosedAccounts READ showClosedAccounts WRITE setShowClosedAccounts NOTIFY showClosedAccountsChanged)
    Q_PROPERTY(bool showClosedTrackers READ showClosedTrackers WRITE setShowClosedTrackers NOTIFY showClosedTrackersChanged)
    Q_PROPERTY(QString errorMessage READ errorMessage NOTIFY errorMessageChanged)
    Q_PROPERTY(QString globalFilterText READ globalFilterText WRITE setGlobalFilterText NOTIFY globalFilterTextChanged)
    Q_PROPERTY(SKGMAccountListModel *accountsModel READ accountsModel CONSTANT)
    Q_PROPERTY(QString globalAccountsTotalText READ globalAccountsTotalText NOTIFY globalAccountsTotalTextChanged)
    Q_PROPERTY(QStringList openAccountNames READ openAccountNames NOTIFY openAccountsChanged)
    Q_PROPERTY(int selectedOpenAccountIndex READ selectedOpenAccountIndex WRITE setSelectedOpenAccountIndex NOTIFY selectedOpenAccountIndexChanged)
    Q_PROPERTY(QString selectedAccountBalanceText READ selectedAccountBalanceText NOTIFY selectedAccountBalanceTextChanged)
    Q_PROPERTY(SKGMTransactionListModel *transactionsModel READ transactionsModel CONSTANT)
    Q_PROPERTY(SKGMTransactionListModel *subTransactionsModel READ subTransactionsModel CONSTANT)
    Q_PROPERTY(QString selectedTrackerName READ selectedTrackerName NOTIFY selectedTrackerNameChanged)
    Q_PROPERTY(QString transactionFilterText READ transactionFilterText WRITE setTransactionFilterText NOTIFY transactionFilterTextChanged)
    Q_PROPERTY(int transactionStatusFilter READ transactionStatusFilter WRITE setTransactionStatusFilter NOTIFY transactionStatusFilterChanged)
    Q_PROPERTY(bool passwordRequired READ passwordRequired NOTIFY passwordRequiredChanged)
    Q_PROPERTY(QVariantList dashboardBankTotals READ dashboardBankTotals NOTIFY dashboardDataChanged)
    Q_PROPERTY(QString dashboardBankGrandTotalText READ dashboardBankGrandTotalText NOTIFY dashboardDataChanged)
    Q_PROPERTY(QString dashboardCurrentPeriodLabel READ dashboardCurrentPeriodLabel NOTIFY dashboardDataChanged)
    Q_PROPERTY(QString dashboardPreviousPeriodLabel READ dashboardPreviousPeriodLabel NOTIFY dashboardDataChanged)
    Q_PROPERTY(QString dashboardCurrentIncomeText READ dashboardCurrentIncomeText NOTIFY dashboardDataChanged)
    Q_PROPERTY(QString dashboardCurrentExpenseText READ dashboardCurrentExpenseText NOTIFY dashboardDataChanged)
    Q_PROPERTY(QString dashboardPreviousIncomeText READ dashboardPreviousIncomeText NOTIFY dashboardDataChanged)
    Q_PROPERTY(QString dashboardPreviousExpenseText READ dashboardPreviousExpenseText NOTIFY dashboardDataChanged)
    Q_PROPERTY(QString dashboardCurrentSavingsText READ dashboardCurrentSavingsText NOTIFY dashboardDataChanged)
    Q_PROPERTY(QString dashboardPreviousSavingsText READ dashboardPreviousSavingsText NOTIFY dashboardDataChanged)
    Q_PROPERTY(double dashboardCurrentSavings READ dashboardCurrentSavings NOTIFY dashboardDataChanged)
    Q_PROPERTY(double dashboardPreviousSavings READ dashboardPreviousSavings NOTIFY dashboardDataChanged)
    Q_PROPERTY(QString dashboardPersonalFinanceScoreText READ dashboardPersonalFinanceScoreText NOTIFY dashboardDataChanged)
    Q_PROPERTY(QString dashboardPersonalFinanceScoreMessage READ dashboardPersonalFinanceScoreMessage NOTIFY dashboardDataChanged)
    Q_PROPERTY(QString dashboardPersonalFinanceScoreColor READ dashboardPersonalFinanceScoreColor NOTIFY dashboardDataChanged)
    Q_PROPERTY(QString dashboardNetWorthText READ dashboardNetWorthText NOTIFY dashboardDataChanged)
    Q_PROPERTY(QString dashboardAnnualSpendingText READ dashboardAnnualSpendingText NOTIFY dashboardDataChanged)
    Q_PROPERTY(QAbstractItemModel *trackersModel READ trackersModel CONSTANT)
    Q_PROPERTY(QAbstractItemModel *payeesModel READ payeesModel CONSTANT)
    Q_PROPERTY(QAbstractItemModel *categoriesModel READ categoriesModel CONSTANT)
    Q_PROPERTY(QAbstractItemModel *unitsModel READ unitsModel CONSTANT)

public:
    explicit SKGMControler(QObject *iParent = nullptr);
    ~SKGMControler() override;

    QString filePath() const;
    void setFilePath(const QString &iFilePath);

    QString password() const;
    void setPassword(const QString &iPassword);

    bool showClosedAccounts() const;
    void setShowClosedAccounts(bool iShowClosedAccounts);
    bool showClosedTrackers() const;
    void setShowClosedTrackers(bool iShowClosedTrackers);
    QString globalFilterText() const;
    void setGlobalFilterText(const QString &iFilterText);

    QString errorMessage() const;
    SKGMAccountListModel *accountsModel();
    QString globalAccountsTotalText() const;
    QStringList openAccountNames() const;
    int selectedOpenAccountIndex() const;
    void setSelectedOpenAccountIndex(int iIndex);
    QString selectedAccountBalanceText() const;
    SKGMTransactionListModel *transactionsModel();
    SKGMTransactionListModel *subTransactionsModel();
    QString selectedTrackerName() const;
    QString transactionFilterText() const;
    void setTransactionFilterText(const QString &iFilterText);
    int transactionStatusFilter() const;
    void setTransactionStatusFilter(int iStatusFilter);
    bool passwordRequired() const;
    QVariantList dashboardBankTotals() const;
    QString dashboardBankGrandTotalText() const;
    QString dashboardCurrentPeriodLabel() const;
    QString dashboardPreviousPeriodLabel() const;
    QString dashboardCurrentIncomeText() const;
    QString dashboardCurrentExpenseText() const;
    QString dashboardPreviousIncomeText() const;
    QString dashboardPreviousExpenseText() const;
    QString dashboardCurrentSavingsText() const;
    QString dashboardPreviousSavingsText() const;
    double dashboardCurrentSavings() const;
    double dashboardPreviousSavings() const;
    QString dashboardPersonalFinanceScoreText() const;
    QString dashboardPersonalFinanceScoreMessage() const;
    QString dashboardPersonalFinanceScoreColor() const;
    QString dashboardNetWorthText() const;
    QString dashboardAnnualSpendingText() const;
    QAbstractItemModel *trackersModel();
    QAbstractItemModel *payeesModel();
    QAbstractItemModel *categoriesModel();
    QAbstractItemModel *unitsModel();

    Q_INVOKABLE bool openFile();
    Q_INVOKABLE bool openFile(const QString &iFilePath, const QString &iPassword = QString());
    Q_INVOKABLE bool selectOpenAccount(const QString &iBank, const QString &iAccountName);
    Q_INVOKABLE bool selectTracker(const QString &iTrackerName);
    Q_INVOKABLE bool selectCategory(const QString &iCategoryFullName);
    Q_INVOKABLE bool selectTransaction(int iOperationId);
    Q_INVOKABLE void ensureDataForPage(int iPageIndex);

Q_SIGNALS:
    void filePathChanged();
    void passwordChanged();
    void showClosedAccountsChanged();
    void showClosedTrackersChanged();
    void errorMessageChanged();
    void globalFilterTextChanged();
    void globalAccountsTotalTextChanged();
    void openAccountsChanged();
    void selectedOpenAccountIndexChanged();
    void selectedAccountBalanceTextChanged();
    void selectedTrackerNameChanged();
    void transactionFilterTextChanged();
    void transactionStatusFilterChanged();
    void passwordRequiredChanged();
    void dashboardDataChanged();
    void referenceDataChanged();

private:
    bool reloadAccountsFromCurrentDocument();
    bool reloadOpenAccountsFromCurrentDocument();
    void reloadDashboardFromCurrentDocument();
    void clearDashboardData();
    void reloadTrackersFromCurrentDocument();
    void reloadPayeesFromCurrentDocument();
    void reloadCategoriesFromCurrentDocument();
    void reloadUnitsFromCurrentDocument();
    void clearReferenceData();
    void reloadTransactionsForSelectedAccount();
    void rebuildTransactionsFromProxy();
    void rebuildSubTransactionsFromProxy();
    void clearLoadedDocumentState();
    void refreshReferenceProxyFilters();
    void applyFilterAndDateSort(SKGSortFilterProxyModel *iProxyModel, SKGObjectModelBase *iObjectModel);
    bool loadSubTransactionsForSelection(const QString &iSelectionLabel, const QString &iWhereClause);
    void updateSelectedAccountBalanceText();
    void setPasswordRequired(bool iRequired);
    void loadPersistedState();
    void persistLastFilePath(const QString &iFilePath);
    void clearPersistedLastFilePath();
    void setErrorMessage(const QString &iMessage);

    SKGDocumentBank m_document;
    SKGMAccountListModel m_accountsModel;
    SKGMTransactionListModel m_transactionsModel;
    SKGMTransactionListModel m_subTransactionsModel;
    SKGObjectModelBase *m_accountsObjectModel{nullptr};
    SKGObjectModelBase *m_transactionsObjectModel{nullptr};
    SKGObjectModelBase *m_subTransactionsObjectModel{nullptr};
    SKGObjectModelBase *m_trackersObjectModel{nullptr};
    SKGObjectModelBase *m_payeesObjectModel{nullptr};
    SKGObjectModelBase *m_categoriesObjectModel{nullptr};
    SKGObjectModelBase *m_unitsObjectModel{nullptr};
    SKGSortFilterProxyModel *m_transactionsProxyModel{nullptr};
    SKGSortFilterProxyModel *m_subTransactionsProxyModel{nullptr};
    SKGSortFilterProxyModel *m_trackersProxyModel{nullptr};
    SKGSortFilterProxyModel *m_payeesProxyModel{nullptr};
    SKGSortFilterProxyModel *m_categoriesProxyModel{nullptr};
    SKGSortFilterProxyModel *m_unitsProxyModel{nullptr};
    SKGMReferenceProxyModel m_trackersModel;
    SKGMReferenceProxyModel m_payeesModel;
    SKGMReferenceProxyModel m_categoriesModel;
    SKGMReferenceProxyModel m_unitsModel;
    QString m_filePath;
    QString m_password;
    QString m_errorMessage;
    QString m_globalFilterText;
    QString m_globalAccountsTotalText;
    QStringList m_openAccountNames;
    QStringList m_openAccountBalanceTexts;
    QStringList m_openAccountBanks;
    QStringList m_openAccountAccountNames;
    QVector<int> m_openAccountIds;
    int m_selectedOpenAccountIndex{-1};
    QString m_selectedAccountBalanceText;
    QString m_selectedTrackerName;
    QString m_transactionFilterText;
    int m_transactionStatusFilter{2};
    bool m_passwordRequired{false};
    bool m_showClosedAccounts{false};
    bool m_showClosedTrackers{false};
    bool m_documentLoaded{false};
    bool m_dashboardDataLoaded{false};
    bool m_accountsDataLoaded{false};
    bool m_openAccountsDataLoaded{false};
    bool m_transactionsDataLoaded{false};
    bool m_subTransactionsDataLoaded{false};
    bool m_trackersDataLoaded{false};
    bool m_payeesDataLoaded{false};
    bool m_categoriesDataLoaded{false};
    bool m_unitsDataLoaded{false};
    QVariantList m_dashboardBankTotals;
    QString m_dashboardBankGrandTotalText;
    QString m_dashboardCurrentPeriodLabel;
    QString m_dashboardPreviousPeriodLabel;
    QString m_dashboardCurrentIncomeText;
    QString m_dashboardCurrentExpenseText;
    QString m_dashboardPreviousIncomeText;
    QString m_dashboardPreviousExpenseText;
    QString m_dashboardCurrentSavingsText;
    QString m_dashboardPreviousSavingsText;
    double m_dashboardCurrentSavings{0.0};
    double m_dashboardPreviousSavings{0.0};
    QString m_dashboardPersonalFinanceScoreText;
    QString m_dashboardPersonalFinanceScoreMessage;
    QString m_dashboardPersonalFinanceScoreColor{QStringLiteral("#666666")};
    QString m_dashboardNetWorthText;
    QString m_dashboardAnnualSpendingText;
};

#endif
